|
| 1 | +// ==UserScript== |
| 2 | +// @name CSDN博客自动点赞评论助手 |
| 3 | +// @namespace http://tampermonkey.net/ |
| 4 | +// @version 1.0 |
| 5 | +// @description 打开任意一个CSDN博客页面,就可以进行自动点赞评论,可以自定义策略与评论内容,高度可定制化 |
| 6 | +// @author ZainCheung |
| 7 | +// @match *://blog.csdn.net/*/article/details/* |
| 8 | +// @grant GM.xmlHttpRequest |
| 9 | +// @connect csdn.net |
| 10 | +// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js |
| 11 | +// @resource html https://cdn.jsdelivr.net/gh/superBoyJack/CSDN-helper/dist/[email protected] |
| 12 | +// @resource css https://cdn.jsdelivr.net/gh/superBoyJack/CSDN-helper/dist/[email protected] |
| 13 | +// @resource js https://cdn.jsdelivr.net/gh/superBoyJack/CSDN-helper/dist/[email protected] |
| 14 | +// @grant GM_getValue |
| 15 | +// @grant GM_setValue |
| 16 | +// @grant GM.getValue |
| 17 | +// @grant GM.setValue |
| 18 | +// @grant unsafeWindow |
| 19 | +// @grant GM_getResourceText |
| 20 | +// ==/UserScript== |
| 21 | + |
| 22 | + // 获取网页的网址 |
| 23 | + var articalUrl = window.location.href; |
| 24 | + var host = articalUrl.split("article/details/")[0]; // 域名 |
| 25 | + var artId = articalUrl.split("article/details/")[1].split("?")[0]; |
| 26 | + // 从页面获取评论列表,但是只能获取第一页 |
| 27 | + var commentList = document.querySelectorAll('#mainBox > main > div.comment-box > div.comment-list-container > div.comment-list-box > ul'); |
| 28 | + let commentListLength = commentList.length; |
| 29 | + // 组装评论请求 |
| 30 | + var getCommentUrl = host + "phoenix/comment/list/" + artId + "?page=1&size=1000&tree_type=1"; |
| 31 | + // 当前登陆用户的账号 |
| 32 | + var myName = document.querySelector("#csdn_container_tool > div > ul > li:nth-child(5) > div.userControl > div:nth-child(2) > div:nth-child(1) > a").href.split("blog.csdn.net/")[1]; |
| 33 | + // 文章的作者账号 |
| 34 | + var UserName = document.querySelector("#mainBox > main > div.blog-content-box > div > div > div.article-info-box > div.article-bar-top > a.follow-nickName").href.split("csdn.net/")[1]; |
| 35 | + // 文章全文字数 |
| 36 | + var blogLength = document.querySelector("#article_content").innerText.replace(/\ +/g,"").replace(/[\r\n]/g,"").length; |
| 37 | + // 文章阅读量 |
| 38 | + var blogRead = document.querySelector("#mainBox > main > div.blog-content-box > div > div > div.article-info-box > div.article-bar-top > span.read-count").innerText.slice(4); |
| 39 | + // 文章最后发布日期 |
| 40 | + var blogDate = document.querySelector("#mainBox > main > div.blog-content-box > div > div > div.article-info-box > div.article-bar-top > span.time").innerText.slice(5,15); |
| 41 | + // 评论文本框 |
| 42 | + var commentArea = document.querySelector("#comment_content"); |
| 43 | + // 评论按钮 |
| 44 | + var commentBtn = document.querySelector("#commentform > div > div.right-box > a > input"); |
| 45 | + // 是否评论,默认评论 |
| 46 | + var toComment = true; |
| 47 | + var text = "欢迎你回访我的博客哦"; |
| 48 | + |
| 49 | + function init(){ |
| 50 | + // 新的一天会自动打开点赞开关 |
| 51 | + if(localStorage.today){ |
| 52 | + var today = new Date(); |
| 53 | + if(localStorage.today != today.getDate()){ |
| 54 | + localStorage.likeSwitch = 100; |
| 55 | + localStorage.likeCustom = 100; |
| 56 | + localStorage.today = today.getDate(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if(localStorage.likeSwitch == 100){ |
| 61 | + if(localStorage.likeCustom == 100){ |
| 62 | + console.log("blogLength:" + blogLength); |
| 63 | + console.log("blogRead:" + blogRead); |
| 64 | + console.log("localStorage.likeWordNumber:" + localStorage.likeWordNumber); |
| 65 | + console.log("localStorage.likeReadNumber:" + localStorage.likeReadNumber); |
| 66 | + if((parseInt(blogLength) >= parseInt(localStorage.likeWordNumber)) && (parseInt(blogRead) >= parseInt(localStorage.likeReadNumber))){ |
| 67 | + if((new Date(blogDate).getTime()) >= (new Date(localStorage.likeDate).getTime())){ |
| 68 | + likeEvent(); |
| 69 | + } |
| 70 | + } |
| 71 | + }else{ |
| 72 | + likeEvent(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if(localStorage.commentSwitch == 100){ |
| 77 | + if(localStorage.commentCustom == 100){ |
| 78 | + if((parseInt(blogLength) >= parseInt(localStorage.commentWordNumber)) && (parseInt(blogRead) >= parseInt(localStorage.commentReadNumber))){ |
| 79 | + if((new Date(blogDate).getTime()) >= (new Date(localStorage.commentDate).getTime())){ |
| 80 | + commentEvent(); |
| 81 | + } |
| 82 | + } |
| 83 | + }else{ |
| 84 | + commentEvent(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + // 点赞事件 |
| 90 | + function likeEvent(){ |
| 91 | + //没有点赞才自动点赞并且延迟100毫秒后进行评论 |
| 92 | + var zhan = document.querySelector("body > div.tool-box.vertical > ul > li.btn-like-box > button"); |
| 93 | + if(zhan.getAttribute("class").search("liked") == -1){ |
| 94 | + zhan.click(); |
| 95 | + setTimeout(function(){ |
| 96 | + if(document.querySelector("body > div.skin-boxshadow").style.display == "block"){localStorage.likeSwitch = 0} |
| 97 | + console.log(document.querySelector("body > div.skin-boxshadow").style.display); |
| 98 | + },500); |
| 99 | + }else{ |
| 100 | + console.warn("已经点过赞了"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // 评论事件 |
| 105 | + function commentEvent(){ |
| 106 | + var commentTextList = JSON.parse(localStorage.commentList); |
| 107 | + var commentText; |
| 108 | + if(localStorage.commentRangeSwitch==0){ |
| 109 | + commentText = commentTextList.R[Math.floor(Math.random()*commentTextList.R.length)].text; |
| 110 | + }else{ |
| 111 | + var count_S = localStorage.commentRange.split(",")[0]; |
| 112 | + var count_L = localStorage.commentRange.split(",")[1]; |
| 113 | + if(blogLength<count_S){ |
| 114 | + commentText = commentTextList.S[Math.floor(Math.random()*commentTextList.S.length)].text; |
| 115 | + }else if(blogLength>count_L){ |
| 116 | + commentText = commentTextList.L[Math.floor(Math.random()*commentTextList.L.length)].text; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + setTimeout(function(){ |
| 121 | + var commentList = document.querySelectorAll('#mainBox > main > div.comment-box > div.comment-list-container > div.comment-list-box > ul'); |
| 122 | + for (let i=0; i<commentList.length; i++){ |
| 123 | + console.log("开始循环第"+ i + "次"); |
| 124 | + // 在页面的评论列表找到当前登陆用户账号 |
| 125 | + if(commentList[i].querySelector("li > a").href.split("csdn.net/")[1]==myName){ |
| 126 | + toComment = false; |
| 127 | + console.log("已经评论过,无需重复评论"); |
| 128 | + } |
| 129 | + if(i==(commentList.length-1)){ |
| 130 | + if(toComment && myName!=UserName){ |
| 131 | + console.log(commentText); |
| 132 | + document.querySelector("#comment_content").value= commentText; |
| 133 | + document.querySelector("#commentform > div > div.right-box > a > input").click(); |
| 134 | + console.log("评论成功!"); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + },500); |
| 139 | + } |
| 140 | + |
| 141 | +(function() { |
| 142 | + init(); |
| 143 | +})(); |
| 144 | + |
| 145 | +// 添加Link |
| 146 | +function addGlobalLink(rel,href) { |
| 147 | + var head, link; |
| 148 | + head = document.getElementsByTagName('head')[0]; |
| 149 | + if (!head) { return; } |
| 150 | + link = document.createElement('link'); |
| 151 | + link.rel = rel; |
| 152 | + link.href = href; |
| 153 | + head.appendChild(link); |
| 154 | +} |
| 155 | +// 添加Script |
| 156 | +function addGlobalScript(src) { |
| 157 | + var body, script; |
| 158 | + body = document.getElementsByTagName('body')[0]; |
| 159 | + if (!body) { return; } |
| 160 | + script = document.createElement('script'); |
| 161 | + script.src = src; |
| 162 | + body.appendChild(script); |
| 163 | +} |
| 164 | + |
| 165 | +addGlobalLink('stylesheet','https://cdn.jsdelivr.net/gh/superBoyJack/CDN/css/element-ui-index.css'); |
| 166 | +addGlobalLink('stylesheet','https://at.alicdn.com/t/font_1776564_biuc05tmt1k.css'); |
| 167 | +addGlobalLink('stylesheet','https://cdn.jsdelivr.net/gh/superBoyJack/CSDN-helper/dist/[email protected]'); |
| 168 | +addGlobalScript("https://cdn.jsdelivr.net/gh/superBoyJack/CDN/js/element-ui-index.js"); |
| 169 | +addGlobalScript("https://cdn.jsdelivr.net/npm/vue/dist/vue.js"); |
| 170 | +addGlobalScript("https://unpkg.com/axios/dist/axios.min.js"); |
| 171 | + |
| 172 | + window.addEventListener( |
| 173 | + 'load', |
| 174 | + function(){ |
| 175 | + var appMenu = document.createElement("div"); |
| 176 | + var vueScript = document.createElement("script"); |
| 177 | + appMenu.innerHTML = GM_getResourceText("html"); |
| 178 | + vueScript.innerHTML = GM_getResourceText("js"); |
| 179 | + var navbar,body; |
| 180 | + navbar = document.getElementById('csdn-toolbar'); |
| 181 | + body = document.getElementsByTagName('body')[0]; |
| 182 | + if (navbar && body) { |
| 183 | + navbar.parentNode.insertBefore(appMenu, navbar.nextSibling); |
| 184 | + body.parentNode.insertBefore(vueScript, body.nextSibling); |
| 185 | + } |
| 186 | + },true); |
0 commit comments