Greasy Fork

阳光宽频网显示评论-简单版

阳光宽频网显示评论,作者关注,支持点赞

目前为 2017-10-24 提交的版本。查看 最新版本

// ==UserScript==
// @name         阳光宽频网显示评论-简单版
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  阳光宽频网显示评论,作者关注,支持点赞
// @author       http://hunao.me
// @match        http://www.365yg.com/group/*
// @connect      toutiao.com
// @grant        GM_xmlhttpRequest
// @require      https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';
    var bui_img_pre             = "http://7xjccj.com1.z0.glb.clouddn.com/bui_pre.png";
    var bui_img_after           = "http://7xjccj.com1.z0.glb.clouddn.com/bui_after.png";
    var login_api               = "https://sso.toutiao.com/login/";                         //登录地址
    var comment_list_api        = "http://www.toutiao.com/api/comment/list/";               //评论列表
    var comment_replay_list_api = "http://www.toutiao.com/api/comment/get_reply/";          //评论回复列表
    var comment_digg_api        = "http://www.toutiao.com/api/comment/digg/";               //点赞
    var comment_reply_digg_api  = "http://www.toutiao.com/api/comment/reply_digg/";         //回复点赞
    var follow_api              = "http://www.toutiao.com/c/user/follow/";                  //关注
    var unfollow_api            = "http://www.toutiao.com/c/user/unfollow/";                //取消关注
    var login_status            = false;                                                    //登录状态
    //加载css样式
    //
    //$(".player_inner").hide();
    var sss = document.createElement('style');
    sss.innerText = `.c-digg{color: #777;cursor: pointer;font-size: 14px;float: right;}
.no-more-tips{display:block;width:300px;margin:0 auto 10px;text-align:center;font-size:14px;line-height:30px;}
.bui_after_class{background:url(`+bui_img_after+`) no-repeat right center;}
.bui_pre_class{background:url(`+bui_img_pre+`) no-repeat right center;}`;
    document.body.appendChild(sss);
    //创建关注按钮
    $(".abs-attention span").append('<a id="follow_author" data-is_follow="0" href="javascript:void(0)" style="cursor:pointer;color: #fff;border-radius: 4px;background: #2a90d7;width: 60px;height: 26px;text-align: center;line-height: 26px;">+关注</a>');
    //创建评论div
    $("div[riot-tag='abstract']").after('<div riot-tag="commentList"></div>');
    //获取评论列表数据
    getCommentList(0,20);
    //检查登录状态
    checkLoginStatus();
    //获取关注状态
    getFollowStatus();
    /**
     * 获取评论(头条限制,起始位置在15后不返回数据,为了推广手机app)
     * @param  {[int]} offset [起始位置]
     * @param  {[int]} count  [加载条数]
     * @return
     */
    function getCommentList(offset,count){
        GM_xmlhttpRequest({
            method: "GET",
            url: comment_list_api+"?group_id=" + player.group_id + "&item_id=" + player.group_id + "&offset="+offset+"&count="+count,
            onload: function(res) {
                var obj = eval('(' + res.responseText + ')');
                console.log("拉取评论:",obj);
                var obj_comment = obj.data.comments;
                var comment_item = offset == 0 ? `<div class="detail-comment">
                                <a id="comment_area" href="#"></a>
                                <div id="comment"><div class="c-header"><em>`
                                +obj.data.total+
                                `</em>条评论</div><ul>` : "";
                for(var i in obj_comment){
                    var bui_img = obj_comment[i].user_digg?bui_img_after:bui_img_pre;
                    comment_item += `<li class="c-item">
                                <a href="http://www.toutiao.com/c/user/`
                                +obj_comment[i].user.user_id+
                                `/" target="_blank" class="avatar-wrap">
                                <img src="`
                                +obj_comment[i].user.avatar_url+
                                `" alt=""></a>
                                <div class="c-content">
                                <div class="c-user-info">
                                <a href="http://toutiao.com/c/user/`
                                +obj_comment[i].user.user_id+
                                `/" target="_blank" class="c-user-name">`
                                +obj_comment[i].user.name+
                                `</a>
                                <span class="c-create-time">`
                                +formatTimeAgo(obj_comment[i].create_time)+
                                `</span>
                                </div><p>`
                                +obj_comment[i].text+
                                `</p>
                                <div class="c-footer-action">
                                <span ga_event="click_expand_reply" class="c-reply-count" data-comment_id="`
                                +obj_comment[i].id+
                                `" data-input_count="`
                                +i+
                                `">`
                                +obj_comment[i].reply_count+
                                `条回复
                                <i class="bui-icon icon-arrow_down" style="font-size: 14px; color: rgb(64, 101, 153);"></i>
                                </span><span title="举报" class="bui-right c-report">
                                <i class="bui-icon icon-report" style="font-size: 14px; color: rgb(202, 202, 202);"></i></span>
                                <span style="height: 19px;line-height:21px;"
                                data-user_digg = "`+obj_comment[i].user_digg+`"
                                data-dongtai_id = "`+obj_comment[i].dongtai_id+`"
                                data-comment_id="`+obj_comment[i].id+`" data-type="1" title="点赞" ga_event="click_good_comment" class="bui-right c-digg `+getBuiIcon(obj_comment[i].user_digg)+`"><span class="lalala" style="padding-right: 12px;">`
                                +formatBuiNum(obj_comment[i].digg_count)+
                                `&nbsp;</span></span></div><div class="J_input_all J_input_`
                                +i+
                                `" style="display:none;" >
                                </li>`;
                }
                if(offset == 0){
                    comment_item += `</ul>
<a class="no-more-tips" style="color:#406599;" href="//app.toutiao.com/news_article/" target="_blank">以上为热门评论,头条 App 还有更多内容</a>
                                </div></div>`;
                    $("div[riot-tag='abstract']").after($(comment_item));
                }else{
                    $("#comment ul").after($(comment_item));
                }
                if(obj.data.has_more == false){
                    $("#loadmore_comment").remove();
                }
            }
        });
    }

    //获取回复
    $(".left").delegate(".detail-comment .c-reply-count","click",function(){
        var comment_id = $(this).data("comment_id");
        var input_count = $(this).data("input_count");
        //判断是隐藏还是加载
        if($('.J_input_'+input_count).nextAll().length){
            $('.J_input_'+input_count).nextAll().remove();
        }else{
            GM_xmlhttpRequest({
                method: "GET",
                url: comment_replay_list_api+"?comment_id=" + comment_id + "&dongtai_id=" + comment_id + "&offset=0&count=20",
                onload: function(res) {
                    var obj = eval('(' + res.responseText + ')');
                    console.log("展开回复:",obj);
                    var obj_reply = obj.data.data;
                    var reply_item = '';
                    for(var i in obj_reply){
                        reply_item += `<div class="c-reply-comment">
                            <a href="http://www.toutiao.com/c/user/`
                            +obj_reply[i].user.user_id+
                            `/" target="_blank" class="avatar-wrap">
                            <img src="`+obj_reply[i].user.avatar_url+`" alt=""></a>
                            <div class="c-content"><div class="c-user-info">
                            <a href="http://www.toutiao.com/c/user/`
                            +obj_reply[i].user.user_id+
                            `/" target="_blank" class="c-user-name">`
                            +obj_reply[i].user.name+
                            `</a> <span class="c-create-time">`+formatTimeAgo(obj_reply[i].create_time)+`</span></div><p>`
                            +obj_reply[i].text+
                            `</p> <div class="c-footer-action">
                            <span title="点赞" data-type="2" data-user_digg = "`+obj_reply[i].user_digg+`"
                                data-dongtai_id = "`+obj_reply[i].dongtai_id+`"
                                data-comment_id="`+obj_reply[i].id+`"  ga_event="click_good_comment" class="bui-right c-digg `+getBuiIcon(obj_reply[i].user_digg)+`" style="height: 19px;line-height:21px;"><span class="lalala" style="padding-right: 12px;">`
                            +formatBuiNum(obj_reply[i].digg_count)+
                            `&nbsp;</span></span></div>
                            <div class="J_input_all J_input_`
                            +input_count+
                            `_`
                            +i+
                            `" style="display:none;" >
                           </div></div>`;
                    }

                    $(".J_input_"+input_count).after(reply_item);


                }
            });
        }
    });

    //对评论点赞
    $(".left").delegate(".detail-comment .c-digg","click",function(){

        var comment_id = $(this).data("comment_id");
        var dongtai_id = $(this).data("dongtai_id");
        var group_id   = player.group_id;
        var item_id    = group_id;
        var user_digg  = $(this).data("user_digg");
        var type       = $(this).data("type");
        var thisSpan   = $(this);
        if(login_status == false){
            window.location.href = login_api;
            return;
        }
        console.log(type == 1?comment_digg_api:comment_reply_digg_api);
        console.log("comment_id="+comment_id+"&dongtai_id="+dongtai_id+"&group_id="+group_id+"&item_id="+item_id+"&action=digg");
        if(user_digg == 0){//未赞过
            GM_xmlhttpRequest({
                method: "POST",
                url: type == 1?comment_digg_api:comment_reply_digg_api,
                data:"comment_id="+comment_id+"&dongtai_id="+dongtai_id+"&group_id="+group_id+"&item_id="+item_id+"&action=digg",
                onload: function(res) {
                    var obj = eval('(' + res.responseText + ')');
                    console.log("点赞结果:",obj);
                    if (obj.message == "success") {
                        thisSpan.data("user_digg",1);
                        thisSpan.find('.lalala').html(obj.data.digg_count+"&nbsp;");
                        thisSpan.css("background","url("+bui_img_after+") no-repeat right center");
                    }
                }
            });
        }
    });

    //关注&取消关注作者
    $(".abs-attention").delegate("#follow_author","click",function(){
        var is_follow = $("#follow_author").data("is_follow");
        if(is_follow != 1){
        GM_xmlhttpRequest({
                method: "POST",
                url: follow_api,
                data:"user_id="+abstract.creator_id,
                onload: function(res) {
                    var obj = eval('(' + res.responseText + ')');
                    console.log("关注结果:",obj);
                    if (obj.message == "success") {
                        $("#follow_author").text("已关注");
                        $("#follow_author").css("background","#999");
                        $("#follow_author").data("is_follow",1);
                    }
                }
         });
        }else{
            GM_xmlhttpRequest({
                method: "POST",
                url: unfollow_api,
                data:"user_id="+abstract.creator_id,
                onload: function(res) {
                    var obj = eval('(' + res.responseText + ')');
                    console.log("取消关注结果:",obj);
                    if (obj.message == "success") {
                        $("#follow_author").text("+关注");
                        $("#follow_author").css("background","#2a90d7");
                        $("#follow_author").data("is_follow",0);
                    }
                }
         });
        }

    });

    //获取关注状态
    function getFollowStatus(){
            var user_homepage = "http://www.toutiao.com/c/user/"+abstract.creator_id+"/";
            $(".abs-attention span .name,.atar").attr("target","_blank");
            $(".abs-attention span .name,.atar").attr("href",user_homepage);
            $(".abs-attention span .name,.atar").css({"cursor":"pointer","color":"#222"});
            GM_xmlhttpRequest({
                method: "GET",
                url: user_homepage,
                data:"user_id="+abstract.creator_id,
                onload: function(res) {
                    var text = res.responseText;
                    var like_arr = text.match(/like:\w{4,5}/);
                    if(like_arr[0] == "like:true"){
                        $("#follow_author").text("已关注");
                        $("#follow_author").css("background","#999");
                        $("#follow_author").data("is_follow",1);
                    }
                }
            });
    }

    //格式化时间
    function formatTimeAgo(time) {
    if (time != +time) return "";
    time = new Date(1e3 * time);
    var e, i = Math.floor((new Date - time) / 1e3),
    a = Math.floor(i / 31536e3);
    return a >= 1 ? e = "年" : (a = Math.floor(i / 2592e3), a >= 1 ? e = "月" :
    (a = Math.floor(i / 86400), a >=1 ? e = "天" :
    (a = Math.floor(i / 3600), a >= 1 ? e = "小时" :
    (a = Math.floor(i / 60), a >= 1 ? e = "分钟" :
    (a = i, e = "秒"))))), "秒" === e ? "刚刚" :
    a + e + "前";
    }

    //格式化点赞数
    function formatBuiNum(t) {
        if (t != +t) return "";
        var e = Math.pow(10, 9),
            i = Math.pow(10, 8),
            a = Math.pow(10, 7),
            n = Math.pow(10, 5),
            r = Math.pow(10, 4),
            o = Math.pow(10, 3),
            s = "",
            l = void 0;
        return t - e >= 0 ? (s = "亿", l = Math.floor(t / i)) : t - i >= 0 ? (s = "亿", l = (Number(Math.floor(t / a) /
                10).toFixed(1) + "").replace(/\.0$/, "")) : t - n > 0 ? (s = "万", l = Math.floor(t / r)) : t - r >= 0 ?
                (s = "万", l = (Number(Math.floor(t / o) / 10).toFixed(1) + "").replace(/\.0$/, "")) : l = t, l + s;
    }

    //检测登录状态
    function checkLoginStatus(){
        /*
        GM_xmlhttpRequest({
            method: "HEAD",
            url: login_api,
            synchronous: true,
            onload: function(res) {
               if(res.finalUrl != login_api){
                    login_status = true;
               }
            }
        });
        */

        GM_xmlhttpRequest({
            method: "GET",
            url: "http://www.toutiao.com/user/info/",
            onload: function(res) {
               var obj = eval('(' + res.responseText + ')');
               if(obj.message != "error"){
                   console.log("用户已登录!");
                    login_status = true;
               }else{
                   console.log("用户未登录!");
               }
               return;
            }
        });
    }

    //根据点赞状态获取class
    function getBuiIcon(is_digg){
        return is_digg == 1 ? "bui_after_class" : "bui_pre_class";
    }
})();