V2EX 增强

自动签到、自动无缝翻页、回到顶部(右键点击两侧空白处)、标签页伪装为 Github(摸鱼)

当前为 2021-04-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name V2EX 增强
  3. // @version 1.0.4
  4. // @author X.I.U
  5. // @description 自动签到、自动无缝翻页、回到顶部(右键点击两侧空白处)、标签页伪装为 Github(摸鱼)
  6. // @match *://v2ex.com/*
  7. // @match *://*.v2ex.com/*
  8. // @icon https://www.v2ex.com/static/favicon.ico
  9. // @grant GM_xmlhttpRequest
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_unregisterMenuCommand
  12. // @grant GM_openInTab
  13. // @grant GM_getValue
  14. // @grant GM_setValue
  15. // @grant GM_notification
  16. // @license GPL-3.0 License
  17. // @run-at document-end
  18. // @namespace https://github.com/XIU2/UserScript
  19. // ==/UserScript==
  20.  
  21. (function() {
  22. var menu_ALL = [
  23. ['menu_autoClockIn', '自动签到', '自动签到', true],
  24. ['menu_pageLoading', '自动无缝翻页', '自动无缝翻页', true],
  25. ['menu_pageLoading_reply', '帖子内自动翻页', '帖子内自动翻页', false],
  26. ['menu_backToTop', '回到顶部(右键点击两侧空白处)', '回到顶部', true],
  27. ['menu_fish', '标签页伪装为 Github(摸鱼)', '标签页伪装为 Github', false]
  28. ], menu_ID = [];
  29. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  30. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  31. }
  32. registerMenuCommand();
  33.  
  34. // 注册脚本菜单
  35. function registerMenuCommand() {
  36. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  37. for (let i=0;i<menu_ID.length;i++){
  38. GM_unregisterMenuCommand(menu_ID[i]);
  39. }
  40. }
  41. for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
  42. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  43. menu_ID[i] = GM_registerMenuCommand(`[ ${menu_ALL[i][3]?'√':'×'} ] ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
  44. }
  45. menu_ID[menu_ID.length] = GM_registerMenuCommand('反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/424246/feedback', {active: true,insert: true,setParent: true});});
  46. }
  47.  
  48. // 菜单开关
  49. function menu_switch(menu_status, Name, Tips) {
  50. if (menu_status == 'true'){
  51. GM_setValue(`${Name}`, false);
  52. GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3000});
  53. }else{
  54. GM_setValue(`${Name}`, true);
  55. GM_notification({text: `已开启 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3000});
  56. }
  57. registerMenuCommand(); // 重新注册脚本菜单
  58. };
  59.  
  60. // 返回菜单值
  61. function menu_value(menuName) {
  62. for (let menu of menu_ALL) {
  63. if (menu[0] == menuName) {
  64. return menu[3]
  65. }
  66. }
  67. }
  68.  
  69.  
  70. // 默认 ID 为 0
  71. var curSite = {SiteTypeID: 0};
  72.  
  73. // 自动翻页规则
  74. // HT_insert:1 = 插入该元素的前面;2 = 插入该元素内的最后面
  75. // scrollDelta:数值越大,滚动条触发点越靠上(越早开始翻页),一般是访问网页速度越慢,该值就需要越大
  76. let DBSite = {
  77. recent: { // 最近主题页
  78. SiteTypeID: 1,
  79. pager: {
  80. type: 1,
  81. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  82. pageElement: 'css;.cell.item',
  83. HT_insert: ['//*[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1],
  84. replaceE: 'css;#Main .box .cell:not(.item) > table',
  85. scrollDelta: 600
  86. }
  87. },
  88. notifications: { // 提醒消息页
  89. SiteTypeID: 2,
  90. pager: {
  91. type: 1,
  92. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  93. pageElement: 'css;#notifications > div',
  94. HT_insert: ['css;#notifications', 2],
  95. replaceE: 'css;#Main .box > .cell:not(.item) > table',
  96. scrollDelta: 600
  97. }
  98. },
  99. replies: { // 用户回复页
  100. SiteTypeID: 3,
  101. pager: {
  102. type: 1,
  103. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  104. pageElement: '//*[@id="Main"]//div[@class="box"]//div[@class="dock_area"] | //*[@id="Main"]//div[@class="box"]//div[@class="inner"] | //*[@id="Main"]//div[@class="box"]//div[@class="dock_area"][last()]/following-sibling::div[@class="cell"][1]',
  105. HT_insert: ['//*[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1],
  106. replaceE: 'css;#Main .box .cell:not(.item) > table',
  107. scrollDelta: 1000
  108. }
  109. },
  110. go: { // 分类主题页
  111. SiteTypeID: 4,
  112. pager: {
  113. type: 1,
  114. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  115. pageElement: 'css;#TopicsNode > div',
  116. HT_insert: ['css;#TopicsNode', 2],
  117. replaceE: 'css;#Main .box > .cell:not(.item) > table',
  118. scrollDelta: 700
  119. }
  120. },
  121. reply: { // 帖子内容页
  122. SiteTypeID: 5,
  123. pager: {
  124. type: 1,
  125. nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]',
  126. pageElement: 'css;.cell[id^="r_"]',
  127. HT_insert: ['//div[starts-with(@id, "r_")][last()]/following-sibling::div[@class="cell"][1]', 1],
  128. replaceE: 'css;#Main .box > .cell:not(.normalUser) > table',
  129. scrollDelta: 700
  130. }
  131. },
  132. reply_positive: { // 帖子内容页(正序)
  133. SiteTypeID: 6,
  134. pager: {
  135. type: 1,
  136. nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]',
  137. pageElement: 'css;.cell[id^="r_"]',
  138. HT_insert: ['//div[starts-with(@id, "r_")][position()=1]', 1],
  139. replaceE: 'css;#Main .box > .cell:not(.normalUser) > table',
  140. scrollDelta: 700
  141. }
  142. }
  143. };
  144.  
  145.  
  146. switch (location.pathname) {
  147. case "/recent": // 最近主题页
  148. curSite = DBSite.recent;
  149. break;
  150. case "/notifications": // 提醒消息页
  151. curSite = DBSite.notifications;
  152. break;
  153. default:
  154. if (location.pathname.indexOf('/go/') > -1) { // 分类主题页
  155. curSite = DBSite.go;
  156. } else if (location.pathname.indexOf('/t/') > -1) { // 帖子内容页
  157. if(menu_value('menu_pageLoading_reply'))curSite = DBSite.reply_positive; // 帖子内自动无缝翻页
  158. } else if (location.pathname.indexOf('/replies') > -1) { // 用户回复页
  159. curSite = DBSite.replies;
  160. }
  161. }
  162.  
  163. curSite.pageUrl = ""; // 下一页URL
  164. if(menu_value('menu_fish'))fish() // 标签页伪装为 Github(摸鱼)
  165. if(menu_value('menu_autoClockIn'))setTimeout(qianDao, 1000) // 自动签到(后台),延迟 1 秒执行是为了兼容 [V2ex Plus] 扩展
  166. if(menu_value('menu_pageLoading'))pageLoading(); // 自动翻页(无缝)
  167. if(menu_value('menu_backToTop'))backToTop(); // 回到顶部(右键点击空白处)
  168.  
  169.  
  170. // 自动签到(后台)
  171. function qianDao() {
  172. let qiandao = document.querySelector('.box .inner a[href="/mission/daily"]');
  173. if (qiandao) {
  174. let url = (location.origin + "/mission/daily/redeem?" + RegExp("once\\=(\\d+)").exec(document.querySelector('div#Top .tools').innerHTML)[0]);
  175. GM_xmlhttpRequest({
  176. url: url,
  177. method: "GET",
  178. timeout: 5000,
  179. onload: function (response) {
  180. let html = ShowPager.createDocumentByString(response.responseText);
  181. console.log(html)
  182. if (html.querySelector('li.fa.fa-ok-sign')) {
  183. html = html.getElementById('Main').innerText.match(/已连续登录 (\d+?) 天/)[0];
  184. qiandao.innerText = `自动签到成功!${html}`;
  185. qiandao.href = '#';
  186. } else {
  187. GM_notification({text: '自动签到失败!请联系作者解决!', timeout: 4000, onclick() {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/424246/feedback', {active: true,insert: true,setParent: true});}});
  188. qiandao.innerText = '自动签到失败!请尝试手动签到!';
  189. }
  190. }
  191. });
  192. } else if (document.getElementById('gift_v2excellent')) { // 兼容 [V2ex Plus] 扩展
  193. document.getElementById('gift_v2excellent').click();
  194. }
  195. }
  196.  
  197.  
  198. // 回到顶部(右键点击空白处)
  199. function backToTop() {
  200. document.getElementById("Wrapper").oncontextmenu = document.querySelector("#Wrapper > .content").oncontextmenu = function(event){
  201. if (event.target==this) {
  202. event.preventDefault();
  203. window.scrollTo(0,0)
  204. }
  205. }
  206. }
  207.  
  208.  
  209. // 标签页伪装为 Github(摸鱼)
  210. function fish() {
  211. window.document.title = 'GitHub'
  212. if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  213. document.querySelector("link[rel*='shortcut icon']").href = 'https://github.githubassets.com/favicons/favicon-dark.png'
  214. } else {
  215. document.querySelector("link[rel*='shortcut icon']").href = 'https://github.githubassets.com/favicons/favicon.png'
  216. }
  217. }
  218.  
  219.  
  220. // 自动无缝翻页
  221. function pageLoading() {
  222. if (curSite.SiteTypeID > 0){
  223. windowScroll(function (direction, e) {
  224. if (direction === "down") { // 下滑才准备翻页
  225. let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  226. //console.log(document.documentElement.scrollHeight)
  227. let scrollDelta = curSite.pager.scrollDelta;
  228. if (document.documentElement.scrollHeight <= document.documentElement.clientHeight + scrollTop + scrollDelta) {
  229. if (curSite.pager.type === 1) {
  230. ShowPager.loadMorePage();
  231. }else{
  232. let autopbn = document.querySelector(curSite.pager.nextLink);
  233. if (autopbn){
  234. autopbn.click();
  235. }
  236. }
  237. }
  238. }
  239. });
  240. }
  241. }
  242.  
  243.  
  244. // 滚动条事件
  245. function windowScroll(fn1) {
  246. var beforeScrollTop = document.documentElement.scrollTop,
  247. fn = fn1 || function () {};
  248. setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件
  249. window.addEventListener("scroll", function (e) {
  250. var afterScrollTop = document.documentElement.scrollTop,
  251. delta = afterScrollTop - beforeScrollTop;
  252. if (delta == 0) return false;
  253. fn(delta > 0 ? "down" : "up", e);
  254. beforeScrollTop = afterScrollTop;
  255. }, false);
  256. }, 1000)
  257. }
  258.  
  259.  
  260. var ShowPager = { // 修改自 https://greasyfork.org/scripts/14178
  261. getFullHref: function (e) {
  262. if(e == null) return '';
  263. "string" != typeof e && (e = e.getAttribute("href"));
  264. var t = this.getFullHref.a;
  265. return t || (this.getFullHref.a = t = document.createElement("a")), t.href = e, t.href;
  266. },
  267. createDocumentByString: function (e) {
  268. if (e) {
  269. if ("HTML" !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, "application/xhtml+xml");
  270. var t;
  271. try {
  272. t = (new DOMParser).parseFromString(e, "text/html");
  273. } catch (e) {
  274. }
  275. if (t) return t;
  276. if (document.implementation.createHTMLDocument) t = document.implementation.createHTMLDocument("ADocument"); else try {
  277. (t = document.cloneNode(!1)).appendChild(t.importNode(document.documentElement, !1)),
  278. t.documentElement.appendChild(t.createElement("head")), t.documentElement.appendChild(t.createElement("body"));
  279. } catch (e) {
  280. }
  281. if (t) {
  282. var r = document.createRange();
  283. r.selectNodeContents(document.body);
  284. var n = r.createContextualFragment(e);
  285. t.body.appendChild(n);
  286. for (var a, o = {
  287. TITLE: !0,
  288. META: !0,
  289. LINK: !0,
  290. STYLE: !0,
  291. BASE: !0
  292. }, i = t.body, s = i.childNodes, c = s.length - 1; c >= 0; c--) o[(a = s[c]).nodeName] && i.removeChild(a);
  293. return t;
  294. }
  295. } else console.error("没有找到要转成DOM的字符串");
  296. },
  297. loadMorePage: function () {
  298. if (curSite.pager) {
  299. let curPageEle = getElementByXpath(curSite.pager.nextLink);
  300. var url = this.getFullHref(curPageEle);
  301. //console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
  302. if(url === '') return;
  303. if(curSite.pageUrl === url) return;// 不会重复加载相同的页面
  304. curSite.pageUrl = url;
  305. // 读取下一页的数据
  306. curSite.pager.startFilter && curSite.pager.startFilter();
  307. GM_xmlhttpRequest({
  308. url: url,
  309. method: "GET",
  310. timeout: 5000,
  311. onload: function (response) {
  312. try {
  313. var newBody = ShowPager.createDocumentByString(response.responseText);
  314. let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody);
  315. let toElement = getAllElements(curSite.pager.HT_insert[0])[0];
  316. if (pageElems.length >= 0) {
  317. let addTo = "beforeend";
  318. if (curSite.pager.HT_insert[1] == 1) addTo = "beforebegin";
  319. // 插入新页面元素
  320. pageElems.forEach(function (one) {
  321. toElement.insertAdjacentElement(addTo, one);
  322. });
  323. // 替换待替换元素
  324. try {
  325. let oriE = getAllElements(curSite.pager.replaceE);
  326. let repE = getAllElements(curSite.pager.replaceE, newBody, newBody);
  327. if (oriE.length === repE.length) {
  328. for (var i = 0; i < oriE.length; i++) {
  329. oriE[i].outerHTML = repE[i].outerHTML;
  330. }
  331. }
  332. } catch (e) {
  333. console.log(e);
  334. }
  335. }
  336. } catch (e) {
  337. console.log(e);
  338. }
  339. }
  340. });
  341. }
  342. },
  343. };
  344.  
  345.  
  346. function getElementByXpath(e, t, r) {
  347. r = r || document, t = t || r;
  348. try {
  349. return r.evaluate(e, t, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  350. } catch (t) {
  351. return void console.error("无效的xpath");
  352. }
  353. }
  354.  
  355.  
  356. function getAllElements(e, t, r, n, o) {
  357. let getAllElementsByXpath = function(e, t, r) {
  358. return r = r || document, t = t || r, r.evaluate(e, t, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  359. }
  360.  
  361. var i, s = [];
  362. if (!e) return s;
  363. if (r = r || document, n = n || window, o = o || void 0, t = t || r, "string" == typeof e) i = 0 === e.search(/^css;/i) ? function getAllElementsByCSS(e, t) {
  364. return (t || document).querySelectorAll(e);
  365. }(e.slice(4), t) : getAllElementsByXpath(e, t, r); else {
  366. if (!(i = e(r, n, o))) return s;
  367. if (i.nodeType) return s[0] = i, s;
  368. }
  369. return function makeArray(e) {
  370. var t, r, n, o = [];
  371. if (e.pop) {
  372. for (t = 0, r = e.length; t < r; t++) (n = e[t]) && (n.nodeType ? o.push(n) : o = o.concat(makeArray(n)));
  373. return a()(o);
  374. }
  375. if (e.item) {
  376. for (t = e.length; t;) o[--t] = e[t];
  377. return o;
  378. }
  379. if (e.iterateNext) {
  380. for (t = e.snapshotLength; t;) o[--t] = e.snapshotItem(t);
  381. return o;
  382. }
  383. }(i);
  384. }
  385. })();