diff --git a/README.md b/README.md index 56a2ebf..ba47eac 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ | :----: | :---- | :---- | :----: | | [](https://www.zhihu.com/people/xiu2) | **知乎 美化** | 宽屏显示、隐藏文章开头大图、调整图片最大高度、隐藏浏览... | **[安装](https://greasyfork.org/zh-CN/scripts/412212)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Zhihu-Beautification.user.js)** | | [](https://www.zhihu.com/people/xiu2) | **知乎 增强** | **移除登录弹窗**、一键收起回答、置顶显示时间、区分问题文章... | **[安装](https://greasyfork.org/zh-CN/scripts/419081)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Zhihu-Enhanced.user.js)** | +| [](https://www.v2ex.com/) | **V2ex 增强** | **自动签到**、自动无缝翻页、回到顶部 (右键点击两侧空白处... | **[安装](https://greasyfork.org/zh-CN/scripts/424246)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/V2ex-Enhanced.user.js)** | | [](https://github.com/XIU2) | **Github 增强** | **高速下载** Git Clone、Release、Raw、Code(ZIP) 等文件... | **[安装](https://greasyfork.org/zh-CN/scripts/412245)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/GithubEnhanced-High-Speed-Download.user.js)** | | [](https://github.com/XIU2) | **自动无缝翻页 \*** | 目前支持:423Down、Apphot、小众软件、PubMed... | **[安装](https://greasyfork.org/zh-CN/scripts/419215)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Autopage.user.js)** | | [](https://bbs.3dmgame.com) | **3DM论坛 美化** | 精简多余内容、样式优化 | **[安装](https://greasyfork.org/zh-CN/scripts/413593)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/3dm-Beautification.user.js)** | diff --git a/V2ex-Enhanced.user.js b/V2ex-Enhanced.user.js new file mode 100644 index 0000000..aeb4ec1 --- /dev/null +++ b/V2ex-Enhanced.user.js @@ -0,0 +1,374 @@ +// ==UserScript== +// @name V2ex 增强 +// @version 1.0.0 +// @author X.I.U +// @description 自动签到、自动无缝翻页、回到顶部(右键点击两侧空白处) +// @match *://v2ex.com/* +// @match *://*.v2ex.com/* +// @icon https://www.v2ex.com/static/favicon.ico +// @grant GM_xmlhttpRequest +// @grant GM_registerMenuCommand +// @grant GM_unregisterMenuCommand +// @grant GM_openInTab +// @grant GM_getValue +// @grant GM_setValue +// @grant GM_notification +// @license GPL-3.0 License +// @run-at document-end +// @namespace https://github.com/XIU2/UserScript +// ==/UserScript== + +(function() { + var menu_ALL = [ + ['menu_autoClockIn', '自动签到', '自动签到', true], + ['menu_pageLoading', '自动无缝翻页', '自动无缝翻页', true], + ['menu_pageLoading_reply', '帖子内自动翻页', '帖子内自动翻页', false], + ['menu_backToTop', '回到顶部(右键点击两侧空白处)', '回到顶部', true] + ], menu_ID = []; + for (let i=0;i menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单 + for (let i=0;i table', + scrollDelta: 600 + } + }, + notifications: { // 提醒消息页 + SiteTypeID: 2, + pager: { + type: 1, + nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]', + pageElement: 'css;#notifications > div', + HT_insert: ['css;#notifications', 2], + replaceE: 'css;#Main .box > .cell:not(.item) > table', + scrollDelta: 600 + } + }, + replies: { // 用户回复页 + SiteTypeID: 3, + pager: { + type: 1, + nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]', + 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]', + HT_insert: ['//*[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1], + replaceE: 'css;#Main .box .cell:not(.item) > table', + scrollDelta: 1000 + } + }, + go: { // 分类主题页 + SiteTypeID: 4, + pager: { + type: 1, + nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]', + pageElement: 'css;#TopicsNode > div', + HT_insert: ['css;#TopicsNode', 2], + replaceE: 'css;#Main .box > .cell:not(.item) > table', + scrollDelta: 700 + } + }, + reply: { // 帖子内容页 + SiteTypeID: 5, + pager: { + type: 1, + nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]', + pageElement: 'css;.cell[id^="r_"]', + HT_insert: ['//div[starts-with(@id, "r_")][last()]/following-sibling::div[@class="cell"][1]', 1], + replaceE: 'css;#Main .box > .cell:not(.normalUser) > table', + scrollDelta: 700 + } + }, + reply_positive: { // 帖子内容页(正序) + SiteTypeID: 6, + pager: { + type: 1, + nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]', + pageElement: 'css;.cell[id^="r_"]', + HT_insert: ['//div[starts-with(@id, "r_")][position()=1]', 1], + replaceE: 'css;#Main .box > .cell:not(.normalUser) > table', + scrollDelta: 700 + } + } + }; + + + switch (location.pathname) { + case "/recent": // 最近主题页 + curSite = DBSite.recent; + break; + case "/notifications": // 提醒消息页 + curSite = DBSite.notifications; + break; + case "/member/XIU2/replies": // 用户回复页 + curSite = DBSite.replies; + break; + default: + if (location.pathname.indexOf('/go/') > -1) { // 分类主题页 + curSite = DBSite.go; + } else if (location.pathname.indexOf('/t/') > -1) { // 帖子内容页 + if(menu_value('menu_pageLoading_reply'))curSite = DBSite.reply_positive; + } + } + + curSite.pageUrl = ""; // 下一页URL + if(menu_value('menu_autoClockIn'))setTimeout(qianDao, 1000) // 自动签到(后台),延迟 1 秒执行是为了兼容 [V2ex Plus] 扩展 + if(menu_value('menu_pageLoading'))pageLoading // 自动翻页(无缝) + if(menu_value('menu_backToTop'))backToTop(); // 回到顶部(右键点击空白处) + + + // 自动签到(后台) + function qianDao() { + let qiandao = document.querySelector('.box .inner a[href="/mission/daily"]'); + if (qiandao) { + let url = (location.origin + "/mission/daily/redeem?" + RegExp("once\\=(\\d+)").exec(document.querySelector('div#Top .tools').innerHTML)[0]); + GM_xmlhttpRequest({ + url: url, + method: "GET", + timeout: 5000, + onload: function (response) { + let html = ShowPager.createDocumentByString(response.responseText); + console.log(html) + if (html.querySelector('li.fa.fa-ok-sign')) { + html = html.getElementById('Main').innerText.match(/已连续登录 (\d+?) 天/)[0]; + qiandao.innerText = `自动签到成功!${html}`; + qiandao.href = '#'; + } else { + GM_notification({text: '自动签到失败!请联系作者解决!', timeout: 4000}); + } + } + }); + } else if (document.getElementById('gift_v2excellent')) { // 兼容 [V2ex Plus] 扩展 + document.getElementById('gift_v2excellent').click(); + } + } + + + // 回到顶部(右键点击空白处) + function backToTop() { + document.getElementById("Wrapper").oncontextmenu = function(event){ + if (event.target==this) { + event.preventDefault(); + window.scrollTo(0,0) + } + } + /*let backToTop = `
` + let backToTop_add = document.createElement('div'); + backToTop_add.innerHTML = `${backToTop}`; + document.body.appendChild(backToTop_add);*/ + } + + + // 自动无缝翻页 + function pageLoading() { + if (curSite.SiteTypeID > 0){ + windowScroll(function (direction, e) { + if (direction === "down") { // 下滑才准备翻页 + let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; + //console.log(document.documentElement.scrollHeight) + let scrollDelta = curSite.pager.scrollDelta; + if (document.documentElement.scrollHeight <= document.documentElement.clientHeight + scrollTop + scrollDelta) { + if (curSite.pager.type === 1) { + ShowPager.loadMorePage(); + }else{ + let autopbn = document.querySelector(curSite.pager.nextLink); + if (autopbn){ + autopbn.click(); + } + } + } + } + }); + } + } + + + // 滚动条事件 + function windowScroll(fn1) { + var beforeScrollTop = document.documentElement.scrollTop, + fn = fn1 || function () {}; + setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件 + window.addEventListener("scroll", function (e) { + var afterScrollTop = document.documentElement.scrollTop, + delta = afterScrollTop - beforeScrollTop; + if (delta == 0) return false; + fn(delta > 0 ? "down" : "up", e); + beforeScrollTop = afterScrollTop; + }, false); + }, 1000) + } + + + var ShowPager = { // 修改自 https://greasyfork.org/scripts/14178 + getFullHref: function (e) { + if(e == null) return ''; + "string" != typeof e && (e = e.getAttribute("href")); + var t = this.getFullHref.a; + return t || (this.getFullHref.a = t = document.createElement("a")), t.href = e, t.href; + }, + createDocumentByString: function (e) { + if (e) { + if ("HTML" !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, "application/xhtml+xml"); + var t; + try { + t = (new DOMParser).parseFromString(e, "text/html"); + } catch (e) { + } + if (t) return t; + if (document.implementation.createHTMLDocument) t = document.implementation.createHTMLDocument("ADocument"); else try { + (t = document.cloneNode(!1)).appendChild(t.importNode(document.documentElement, !1)), + t.documentElement.appendChild(t.createElement("head")), t.documentElement.appendChild(t.createElement("body")); + } catch (e) { + } + if (t) { + var r = document.createRange(); + r.selectNodeContents(document.body); + var n = r.createContextualFragment(e); + t.body.appendChild(n); + for (var a, o = { + TITLE: !0, + META: !0, + LINK: !0, + STYLE: !0, + BASE: !0 + }, i = t.body, s = i.childNodes, c = s.length - 1; c >= 0; c--) o[(a = s[c]).nodeName] && i.removeChild(a); + return t; + } + } else console.error("没有找到要转成DOM的字符串"); + }, + loadMorePage: function () { + if (curSite.pager) { + let curPageEle = getElementByXpath(curSite.pager.nextLink); + var url = this.getFullHref(curPageEle); + //console.log(`${url} ${curPageEle} ${curSite.pageUrl}`); + if(url === '') return; + if(curSite.pageUrl === url) return;// 不会重复加载相同的页面 + curSite.pageUrl = url; + // 读取下一页的数据 + curSite.pager.startFilter && curSite.pager.startFilter(); + GM_xmlhttpRequest({ + url: url, + method: "GET", + timeout: 5000, + onload: function (response) { + try { + var newBody = ShowPager.createDocumentByString(response.responseText); + let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody); + let toElement = getAllElements(curSite.pager.HT_insert[0])[0]; + if (pageElems.length >= 0) { + let addTo = "beforeend"; + if (curSite.pager.HT_insert[1] == 1) addTo = "beforebegin"; + // 插入新页面元素 + pageElems.forEach(function (one) { + toElement.insertAdjacentElement(addTo, one); + }); + // 替换待替换元素 + try { + let oriE = getAllElements(curSite.pager.replaceE); + let repE = getAllElements(curSite.pager.replaceE, newBody, newBody); + if (oriE.length === repE.length) { + for (var i = 0; i < oriE.length; i++) { + oriE[i].outerHTML = repE[i].outerHTML; + } + } + } catch (e) { + console.log(e); + } + } + } catch (e) { + console.log(e); + } + } + }); + } + }, + }; + + + function getElementByXpath(e, t, r) { + r = r || document, t = t || r; + try { + return r.evaluate(e, t, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; + } catch (t) { + return void console.error("无效的xpath"); + } + } + + + function getAllElements(e, t, r, n, o) { + let getAllElementsByXpath = function(e, t, r) { + return r = r || document, t = t || r, r.evaluate(e, t, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + } + + var i, s = []; + if (!e) return s; + 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) { + return (t || document).querySelectorAll(e); + }(e.slice(4), t) : getAllElementsByXpath(e, t, r); else { + if (!(i = e(r, n, o))) return s; + if (i.nodeType) return s[0] = i, s; + } + return function makeArray(e) { + var t, r, n, o = []; + if (e.pop) { + for (t = 0, r = e.length; t < r; t++) (n = e[t]) && (n.nodeType ? o.push(n) : o = o.concat(makeArray(n))); + return a()(o); + } + if (e.item) { + for (t = e.length; t;) o[--t] = e[t]; + return o; + } + if (e.iterateNext) { + for (t = e.snapshotLength; t;) o[--t] = e.snapshotItem(t); + return o; + } + }(i); + } +})(); \ No newline at end of file