// ==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 // @downloadURL none // ==/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('+关注'); //创建评论div $("div[riot-tag='abstract']").after('
'); //获取评论列表数据 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 ? `
` +obj.data.total+ `条评论
以上为热门评论,头条 App 还有更多内容
`; $("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 += `

` +obj_reply[i].text+ `

`; } $(".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+" "); 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"; } })();