V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
openroc
V2EX  ›  分享发现

闲来无事,做个针对 V2EX 的 dotjs 的脚本,

  •  
  •   openroc · 2015-06-07 13:46:20 +08:00 · 3061 次点击
    这是一个创建于 3544 天前的主题,其中的信息可能已经有所发展或是发生改变。
    无意间发现一个chrome extension => dotjs, 正好符合我的某些网站,加入自己脚本的想法。

    这个脚本功能很简单,就是针对不想跳转到具体topic里面的时候,可以看快速看一下,内容和评论。

    /*
    v2ex.com.js for chrome extension dotjs, have fun, :)
    */
    var map = {};
    var comment_template = [
    '<div id="r_2127958" class="cell">',
    '<table cellpadding="0" cellspacing="0" border="0" width="100%">',
    '<tbody><tr>',
    '<td width="48" valign="top" align="center"><img src="_{{_avatar_normal_}}_" class="avatar" border="0" align="default"></td>',
    '<td width="10" valign="top"></td>',
    '<td width="auto" valign="top" align="left"><div class="fr"><span class="no">_{{_seq_}}_</span></div>',
    '<div class="sep3"></div>',
    '<strong><a href="/member/_{{_username_}}_" class="dark">_{{_username_}}_</a></strong>&nbsp; &nbsp;<span class="fade small">_{{_dt_}}_</span> <span class="small fade"></span>',
    '<div class="sep5"></div>',
    '<div class="reply_content">_{{_content_rendered_}}_</div>',
    '</td>',
    '</tr>',
    '</tbody></table>',
    '</div>',
    ].join('');
    function replace(src, map) {
    var rc = src;
    for(var key in map) {
    rc = rc.replace(new RegExp("_{{_"+key+"_}}_","g"), map[key]);
    }
    return rc;
    }
    function getTopicId(url) {
    return (/\/t\/(\d+)/i.test(url)? RegExp.$1 : undefined);
    }
    function getTS(s) {
    var rc = '';
    if(s<60) {
    rc = Math.floor(s) + '秒前';
    } else if( s>=60 && s<3600) {
    rc = Math.floor(s/60) + '分钟前';
    } else if( s>=3600 && s<3600*24) {
    rc = Math.floor(s/3600) + '小时前';
    } else if( s>=3600*24) {
    rc = Math.floor(s/(3600*24)) + '天前';
    }
    return rc;
    }
    function closeContent(host, tid) {
    map[tid] = {status:0, host: host};
    host.find('.injection').remove();
    $('html, body').animate({scrollTop: host.offset().top}, 50);
    }
    function showContent(host, tid) {
    for(var id in map) {
    if(map[id].status == 1) closeContent(map[id].host, id);
    }
    $('html, body').animate({scrollTop: host.offset().top}, 50);
    map[tid] = {status:1, host: host};
    var wrapper = $('<div class="injection" style=""></div>');
    var content = $('<div class="topic_content markdown_body" style="margin:10px 0;">loading....</div>');
    var comments = $('<div class="" style="border-top:1px solid #ccc; background-color:#f9f9f9; margin-left:50px;"></div>');
    wrapper.append(content);
    wrapper.append(comments);
    host.append(wrapper);
    $.get('/api/topics/show.json?id='+tid, function(data){
    content.html(data[0].content_rendered);
    });
    $.get('/api/replies/show.json?topic_id='+tid, function(data){
    for(var n=0, len=data.length; n<len; ++n) {
    var kv = {
    content_rendered: data[n].content_rendered,
    username: data[n].member.username,
    avatar_normal: data[n].member.avatar_normal,
    cid: data[n].id,
    uid: data[n].member.id,
    dt: getTS(Date.now()/1000-data[n].created),
    seq: n+1,
    };
    comments.append(replace(comment_template, kv));
    host.find('.imgly').css('max-width', '530px');
    }
    });
    }
    function onItemClick(self) {
    var tid = getTopicId(self.find('.item_title a').attr('href'));
    if(map[tid] && map[tid].status == 1) {
    closeContent(self, tid);
    } else {
    showContent(self, tid);
    }
    }
    // for dynamic url, such as /?tab=tech
    $('div.cell.item').find('table').click(function(){
    onItemClick($(this).parent());
    });
    // for static url, such as /go/share
    $('#TopicsNode .cell').find('table').click(function(){
    onItemClick($(this).parent());
    });
    view raw v2ex.com.js hosted with ❤ by GitHub



    传送门: dotjs ----> https://github.com/defunkt/dotjs

    have fun。 :)
    第 1 条附言  ·  2015-06-07 15:57:09 +08:00
    已知问题:
    1. 无法显示gist,主要是在document load后,gist脚本不能插入内容。
    2. 无回复,无感谢。
    第 2 条附言  ·  2015-06-07 16:46:27 +08:00
    v2ex api有限制,

    {
    "status" : "error",
    "message" : "Rate Limit Exceeded",
    "rate_limit" : {
    "used" : 60,
    "hourly_quota" : 60,
    "hourly_remaining" : 0
    }
    }

    这个限制每分钟一次? 这个太low了吧, 呵呵
    17 条回复    2015-06-07 17:26:39 +08:00
    h2sky
        1
    h2sky  
       2015-06-07 14:36:07 +08:00
    ChiangDi
        2
    ChiangDi  
       2015-06-07 16:04:15 +08:00
    ~/.js — No longer maintained, sorry. ???
    openroc
        3
    openroc  
    OP
       2015-06-07 16:06:30 +08:00
    @ChiangDi 你用的是dotjs/bin下的那个https service(rake install)吗?
    openroc
        4
    openroc  
    OP
       2015-06-07 16:10:45 +08:00
    @ChiangDi 源码相当的少,git clone一下看看就知道了,无需maintain。
    icedx
        5
    icedx  
       2015-06-07 16:23:12 +08:00
    为啥不用油猴子
    openroc
        6
    openroc  
    OP
       2015-06-07 16:33:40 +08:00
    @icedx Tampermonkey? 太heavy了,用个简单的。自己玩。:)
    openroc
        7
    openroc  
    OP
       2015-06-07 16:34:50 +08:00
    @icedx

    dotjs,源码只有这点,

    $.ajax({
    url: 'https://localhost:3131/'+location.hostname.replace(/^www\./,'')+'.js',
    dataType: 'text',
    success: function(d){
    $(function(){ eval(d) })
    },
    error: function(){
    console.log('no dotjs server found at localhost:3131')
    }
    })
    fzinfz
        8
    fzinfz  
       2015-06-07 16:37:53 +08:00
    感谢楼主~~可惜用的是win,貌似不兼容,用了一次就挂了。。。


    使用: https://github.com/p3lim/dotjs-universal

    另外帮LZ贴个截图~~
    icedx
        9
    icedx  
       2015-06-07 16:38:38 +08:00
    @openroc
    这个dotjs 好像是个半成品
    首先对Windows 用户不友好
    然后安装后疯狂的访问我的3131 端口 而且URL 很奇怪...
    openroc
        10
    openroc  
    OP
       2015-06-07 16:43:30 +08:00
    @fzinfz
    @icedx

    哦。我在mac上用,还没在win上用过。
    icedx
        11
    icedx  
       2015-06-07 16:44:18 +08:00
    @openroc 而且这玩意访问的是
    https://127.0.0.1:3131/
    是https 诶
    闹哪样
    icedx
        12
    icedx  
       2015-06-07 16:48:59 +08:00   ❤️ 1
    openroc
        13
    openroc  
    OP
       2015-06-07 16:49:21 +08:00
    @icedx 哈哈,为了,主站是https的可以访问到脚本,起始,他做的服务,完全可以用本地的nginx代替。 :)
    openroc
        14
    openroc  
    OP
       2015-06-07 16:51:06 +08:00   ❤️ 1
    @icedx 哈哈。good share~, 5 up~
    icedx
        15
    icedx  
       2015-06-07 16:53:07 +08:00
    @openroc 不过 在Windows 编译OpenSSL 又是一个恶梦(捂脸
    cyio
        16
    cyio  
       2015-06-07 17:23:29 +08:00
    openroc
        17
    openroc  
    OP
       2015-06-07 17:26:39 +08:00
    @cyio looks better~. :)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2311 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:04 · PVG 00:04 · LAX 08:04 · JFK 11:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.