V2EX 增强

自动签到、链接转图片、自动无缝翻页、回到顶部(右键点击两侧空白处)、快速回复(左键双击两侧空白处)、标签页伪装为 Github(摸鱼)

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

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