V2EX 增强

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

当前为 2022-07-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name V2EX 增强
  3. // @version 1.1.7
  4. // @author X.I.U
  5. // @description 自动签到、链接转图片、自动无缝翻页、使用 SOV2EX 搜索、回到顶部(右键点击两侧空白处)、快速回复(左键双击两侧空白处)、新标签页打开链接、标签页伪装为 Github(摸鱼)
  6. // @match *://v2ex.com/*
  7. // @match *://*.v2ex.com/*
  8. // @match *://www.sov2ex.com/*
  9. // @icon https://www.v2ex.com/static/favicon.ico
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_unregisterMenuCommand
  13. // @grant GM_openInTab
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_notification
  17. // @license GPL-3.0 License
  18. // @run-at document-end
  19. // @namespace https://github.com/XIU2/UserScript
  20. // @supportURL https://github.com/XIU2/UserScript
  21. // @homepageURL https://github.com/XIU2/UserScript
  22. // ==/UserScript==
  23.  
  24. (function() {
  25. 'use strict';
  26. var menu_ALL = [
  27. ['menu_autoClockIn', '自动签到', '自动签到', true],
  28. ['menu_linksToImgs', '链接转图片', '链接转图片', true],
  29. ['menu_pageLoading', '自动无缝翻页', '自动无缝翻页', true],
  30. ['menu_pageLoading_reply', '帖子内自动翻页', '帖子内自动翻页', false],
  31. ['menu_backToTop', '回到顶部(右键点击两侧空白处)', '回到顶部', true],
  32. ['menu_quickReply', '快速回复(左键双击两侧空白处)', '快速回复', true],
  33. ['menu_linksBlank', '新标签页打开链接', '新标签页打开链接', true],
  34. ['menu_sov2ex', '使用 SOV2EX 搜索', '使用 SOV2EX 搜索', false],
  35. ['menu_fish', '标签页伪装为 Github(摸鱼)', '标签页伪装为 Github', false]
  36. ], menu_ID = [], pausePage = true;
  37. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  38. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  39. }
  40.  
  41. registerMenuCommand();
  42.  
  43. // 注册脚本菜单
  44. function registerMenuCommand() {
  45. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  46. for (let i=0;i<menu_ID.length;i++){
  47. GM_unregisterMenuCommand(menu_ID[i]);
  48. }
  49. }
  50. for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
  51. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  52. 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]}`)});
  53. }
  54. 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});});
  55. }
  56.  
  57. // 菜单开关
  58. function menu_switch(menu_status, Name, Tips) {
  59. if (menu_status == 'true'){
  60. GM_setValue(`${Name}`, false);
  61. GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  62. }else{
  63. GM_setValue(`${Name}`, true);
  64. GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  65. }
  66. registerMenuCommand(); // 重新注册脚本菜单
  67. };
  68.  
  69. // 返回菜单值
  70. function menu_value(menuName) {
  71. for (let menu of menu_ALL) {
  72. if (menu[0] == menuName) {
  73. return menu[3]
  74. }
  75. }
  76. }
  77.  
  78.  
  79. // 默认 ID 为 0
  80. var curSite = {SiteTypeID: 0};
  81.  
  82. // 自动翻页规则
  83. let DBSite = {
  84. recent: { // 最近主题页
  85. SiteTypeID: 1,
  86. pager: {
  87. type: 1,
  88. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  89. pageElement: 'css;.cell.item',
  90. HT_insert: ['//div[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1],
  91. replaceE: 'css;#Main > .box > .cell[style]:not(.item) > table',
  92. scrollDelta: 1500
  93. }
  94. },
  95. notifications: { // 提醒消息页
  96. SiteTypeID: 2,
  97. pager: {
  98. type: 1,
  99. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  100. pageElement: 'css;#notifications > div',
  101. HT_insert: ['css;#notifications', 3],
  102. replaceE: 'css;#Main > .box > .cell[style] > table',
  103. scrollDelta: 1500
  104. }
  105. },
  106. replies: { // 用户回复页
  107. SiteTypeID: 3,
  108. pager: {
  109. type: 1,
  110. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  111. 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]',
  112. HT_insert: ['//div[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1],
  113. replaceE: 'css;#Main > .box > .cell[style] > table',
  114. scrollDelta: 1500
  115. }
  116. },
  117. go: { // 分类主题页
  118. SiteTypeID: 4,
  119. pager: {
  120. type: 1,
  121. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  122. pageElement: 'css;#TopicsNode > div',
  123. HT_insert: ['css;#TopicsNode', 3],
  124. replaceE: 'css;#Main > .box > .cell[style] > table',
  125. scrollDelta: 1500
  126. }
  127. },
  128. reply: { // 帖子内容页
  129. SiteTypeID: 5,
  130. pager: {
  131. type: 1,
  132. nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]',
  133. pageElement: 'css;.cell[id^="r_"]',
  134. HT_insert: ['//div[starts-with(@id, "r_")][last()]/following-sibling::div[@class="cell"][1]', 1],
  135. replaceE: 'css;#Main > .box > .cell[style] > table',
  136. scrollDelta: 1500
  137. }
  138. },
  139. reply_positive: { // 帖子内容页(正序)
  140. SiteTypeID: 6,
  141. pager: {
  142. type: 1,
  143. nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]',
  144. pageElement: 'css;.cell[id^="r_"]',
  145. HT_insert: ['//div[starts-with(@id, "r_")][1]', 1],
  146. replaceE: 'css;#Main > .box > .cell[style] > table',
  147. scrollDelta: 1500
  148. }
  149. },
  150. balance: { // 账户余额页
  151. SiteTypeID: 7,
  152. pager: {
  153. type: 1,
  154. nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
  155. pageElement: 'css;#Main .box > div:not(.cell) > table > tbody > tr:not(:first-child)',
  156. HT_insert: ['css;#Main .box > div:not(.cell) > table > tbody', 3],
  157. replaceE: 'css;#Main > .box > .cell[style] > table',
  158. scrollDelta: 1000
  159. }
  160. },
  161. sov2ex: { // sov2ex
  162. SiteTypeID: 8,
  163. pager: {
  164. nextLink: '.paging>a',
  165. scrollDelta: 1000
  166. }
  167. }
  168. };
  169.  
  170. if (location.hostname === 'www.sov2ex.com') {
  171. curSite = DBSite.sov2ex;
  172. pageLoading();
  173. } else {
  174. switch (location.pathname) {
  175. case '/': // 首页
  176. addChangesLink();
  177. break;
  178. case '/recent': // 最近主题页
  179. curSite = DBSite.recent;
  180. break;
  181. case '/notifications': // 提醒消息页
  182. curSite = DBSite.notifications;
  183. break;
  184. case '/balance': // 账户余额页
  185. curSite = DBSite.balance;
  186. break;
  187. default:
  188. if (location.pathname.indexOf('/go/') > -1) { // 分类主题页
  189. curSite = DBSite.go;
  190. } else if (location.pathname.indexOf('/t/') > -1) { // 帖子内容页
  191. if(menu_value('menu_pageLoading_reply'))curSite = DBSite.reply_positive; // 帖子内自动无缝翻页
  192. if(menu_value('menu_quickReply'))quickReply(); // 快速回复(双击左右两侧空白处)
  193. } else if (location.pathname.indexOf('/replies') > -1) { // 用户回复页
  194. curSite = DBSite.replies;
  195. }
  196. }
  197.  
  198. curSite.pageUrl = ''; // 下一页URL
  199. if(menu_value('menu_linksBlank')) linksBlank(); // 新标签页打开链接
  200. if(menu_value('menu_fish')) fish(); // 标签页伪装为 Github(摸鱼)
  201. if(menu_value('menu_autoClockIn')) setTimeout(qianDao, 1000); // 自动签到(后台),延迟 1 秒执行是为了兼容 [V2ex Plus] 扩展
  202. if(menu_value('menu_pageLoading')) pageLoading(); // 自动翻页(无缝)
  203. if(menu_value('menu_backToTop')) backToTop(); // 回到顶部(右键点击左右两侧空白处)
  204. if(menu_value('menu_linksToImgs')) linksToImgs(); // 链接转图片
  205. if(menu_value('menu_sov2ex')) setTimeout(soV2ex, 1000); // 替换为 sov2ex 搜索
  206. }
  207.  
  208. // 自动签到(后台)
  209. function qianDao() {
  210. let timeNow = new Date().getUTCFullYear() + '/' + (new Date().getUTCMonth() + 1) + '/' + new Date().getUTCDate() // 当前 UTC-0 时间(V2EX 按这个时间的)
  211. if (location.pathname == '/') { // 在首页
  212. let qiandao = document.querySelector('.box .inner a[href="/mission/daily"]');
  213. if (qiandao) { // 如果找到了签到提示
  214. qianDao_(qiandao, timeNow); // 后台签到
  215. } else if (document.getElementById('gift_v2excellent')) { // 兼容 [V2ex Plus] 扩展
  216. document.getElementById('gift_v2excellent').click();
  217. GM_setValue('menu_clockInTime', timeNow); // 写入签到时间以供后续比较
  218. console.info('[V2EX 增强] 自动签到完成!')
  219. } else { // 都没有找到,说明已经签过到了
  220. console.info('[V2EX 增强] 已经签过到了。')
  221. }
  222. } else { // 不在首页
  223. let timeOld = GM_getValue('menu_clockInTime')
  224. if (!timeOld || timeOld != timeNow) {
  225. qianDaoStatus_(timeNow) // 后台获取签到状态(并判断是否需要签到)
  226. }/* else { // 新旧签到时间一致
  227. console.info('[V2EX 增强] 已经签过到了。')
  228. }*/
  229. }
  230. }
  231.  
  232.  
  233. // 后台签到
  234. function qianDao_(qiandao, timeNow) {
  235. let url = (location.origin + "/mission/daily/redeem?" + RegExp("once\\=(\\d+)").exec(document.querySelector('div#Top .tools, #menu-body').innerHTML)[0]);
  236. GM_xmlhttpRequest({
  237. url: url,
  238. method: 'GET',
  239. timeout: 5000,
  240. onload: function (response) {
  241. let html = ShowPager.createDocumentByString(response.responseText);
  242. //console.log(html)
  243. if (html.querySelector('li.fa.fa-ok-sign')) {
  244. html = html.getElementById('Main').textContent.match(/已连续登录 (\d+?) 天/)[0];
  245. GM_setValue('menu_clockInTime', timeNow); // 写入签到时间以供后续比较
  246. console.info('[V2EX 增强] 自动签到完成!')
  247. if (qiandao) {
  248. qiandao.textContent = `自动签到完成!${html}`;
  249. qiandao.href = 'javascript:void(0);';
  250. }
  251. } else {
  252. GM_notification({text: '自动签到失败!请访问 V2EX 首页试试。\n如果持续几天都签到失败,请联系作者解决!', 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});}});
  253. console.warn('[V2EX 增强] 自动签到失败!请访问 V2EX 首页试试。如果持续几天都签到失败,请联系作者解决!')
  254. if (qiandao) qiandao.textContent = '自动签到失败!请尝试手动签到!';
  255. }
  256. }
  257. });
  258. }
  259.  
  260.  
  261. // 后台获取签到状态(并判断是否需要签到)
  262. function qianDaoStatus_(timeNow) {
  263. GM_xmlhttpRequest({
  264. url: 'https://www.v2ex.com/mission/daily',
  265. method: 'GET',
  266. timeout: 5000,
  267. onload: function (response) {
  268. let html = ShowPager.createDocumentByString(response.responseText);
  269. if (html.querySelector('input[value^="领取"]')) { // 还没有签到...
  270. qianDao_(null, timeNow); // 后台签到
  271. } else { // 已经签到了...
  272. console.info('[V2EX 增强] 已经签过到了。')
  273. GM_setValue('menu_clockInTime', timeNow); // 写入签到时间以供后续比较
  274. }
  275. }
  276. });
  277. }
  278.  
  279.  
  280. // 替换为 sov2ex 搜索,代码来自 v2ex-plus 扩展:https://github.com/sciooga/v2ex-plus (懒得重复造轮子了~)
  281. function soV2ex() {
  282. document.body.appendChild(document.createElement('script')).textContent = `
  283. var $search = $('#search')
  284. var searchEvents = $._data($search[0], "events" )
  285. var oKeydownEvent = searchEvents['keydown'][0]['handler']
  286. var oInputEvent = searchEvents['input'][0]['handler']
  287. $search.attr("placeholder","sov2ex")
  288. $search.unbind('keydown', oKeydownEvent)
  289. $search.unbind('input', oInputEvent)
  290. $search.on('input', function(e) {
  291. oInputEvent(e)
  292. $('.search-item:last').attr('href', 'https://www.sov2ex.com/?q=' + $search.val()).text('sov2ex ' +$search.val());
  293. })
  294. $search.keydown(function(e) {
  295. if (e.code == 'Enter' || e.code == 'NumpadEnter' || e.keyCode === 13) {
  296. if ($('.search-item:last').is('.active')) {
  297. $(this).val($(this).val().replace(/[#%&]/g,""));//用户输入不能包含特殊字符#%&
  298. window.open("https://www.sov2ex.com/?q=" + $(this).val());
  299. return 0
  300. }
  301. }
  302. oKeydownEvent(e)
  303. })
  304. `;
  305. }
  306.  
  307.  
  308. // 回到顶部(右键左右两侧空白处)
  309. function backToTop() {
  310. document.getElementById('Wrapper').oncontextmenu = document.querySelector('#Wrapper > .content').oncontextmenu = function(event){
  311. if (event.target == this) {
  312. event.preventDefault();
  313. window.scrollTo(0,0)
  314. }
  315. }
  316. }
  317.  
  318.  
  319. // 标签页伪装为 Github(摸鱼)
  320. function fish() {
  321. window.document.title = 'GitHub'
  322. if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  323. document.querySelector("link[rel*='shortcut icon']").href = 'https://github.githubassets.com/favicons/favicon-dark.png'
  324. } else {
  325. document.querySelector("link[rel*='shortcut icon']").href = 'https://github.githubassets.com/favicons/favicon.png'
  326. }
  327. }
  328.  
  329.  
  330. // 链接转图片,修改自:https://greasyfork.org/scripts/14182
  331. function linksToImgs() {
  332. let links = document.links;
  333. Array.from(links).forEach(function (_this) {
  334. if (/^https.*\.(?:jpg|jpeg|jpe|bmp|png|gif)/i.test(_this.href) && !(/<img\s/i.test(_this.innerHTML))) {
  335. _this.innerHTML = `<img src="${_this.href}" style="max-width: 100%!important;" />`;
  336. }
  337. });
  338. }
  339.  
  340.  
  341. // 快速回复(双击左右两侧空白处)
  342. function quickReply() {
  343. document.getElementById('Wrapper').ondblclick = document.querySelector('#Wrapper > .content').ondblclick = function(event){
  344. if (event.target==this) {
  345. if (document.querySelector('.box.reply-box-sticky')) {
  346. document.getElementById('undock-button').click();
  347. } else {
  348. let _top = document.body.scrollTop + document.documentElement.scrollTop;
  349. document.getElementById('reply_content').focus();
  350. window.scrollTo(0,_top);console.log(_top);
  351. }
  352. }
  353. }
  354. }
  355.  
  356.  
  357. // 新标签页打开链接
  358. function linksBlank() {
  359. if (location.pathname.indexOf('/settings') > -1) return
  360. document.head.appendChild(document.createElement('base')).target = '_blank'; // 让所有链接默认以新标签页打开
  361. Array.from(document.links).forEach(function (_this) {
  362. if (_this.onclick || _this.href.slice(0,4) != 'http' || _this.href.indexOf('#;') > -1 || _this.href.indexOf('night/toggle') > -1 || _this.href.indexOf('/favorite') > -1 || _this.href.indexOf('/?tab=') > -1) {
  363. _this.target = '_self'
  364. }
  365. })
  366. document.querySelectorAll('form').forEach(function (_this) {
  367. if (!_this.target) {_this.target = '_self'}
  368. });
  369.  
  370. const callback = (mutationsList, observer) => {
  371. for (const mutation of mutationsList) {
  372. for (const target of mutation.addedNodes) {
  373. if (target.nodeType != 1) return
  374. if (target.tagName === 'A') {
  375. if (target.onclick || target.href.slice(0,4) != 'http' || target.href.indexOf('#;') > -1 || target.href.indexOf('night/toggle') > -1 || target.href.indexOf('/favorite') > -1) {
  376. target.target = '_self'
  377. }
  378. } else {
  379. document.querySelectorAll('a').forEach(function (_this) {
  380. if (_this.onclick || _this.href.slice(0,4) != 'http' || _this.href.indexOf('#;') > -1 || _this.href.indexOf('night/toggle') > -1 || _this.href.indexOf('/favorite') > -1) {
  381. _this.target = '_self'
  382. }
  383. });
  384. }
  385. }
  386. }
  387. };
  388. const observer = new MutationObserver(callback);
  389. observer.observe(document, { childList: true, subtree: true });
  390. }
  391.  
  392.  
  393. // 添加全站最近更新主题链接
  394. function addChangesLink() {
  395. let links = document.querySelector('#Main .box .inner:last-child');if (!links) return
  396. links.innerHTML = `<div style="float: left;"><span class="chevron">»</span> &nbsp;<a href="/recent" target="_blank">更多新主题</a></div><div style="text-align: right;"><a href="/changes" target="_blank" style="text-align: right;">全站最近更新主题</a> &nbsp;<span class="chevron">«</span></div>`
  397. }
  398.  
  399.  
  400. // 自动无缝翻页
  401. function pageLoading() {
  402. if (curSite.SiteTypeID > 0){
  403. windowScroll(function (direction, e) {
  404. // 下滑 且 未暂停翻页 且 SiteTypeID > 0 时,才准备翻页
  405. if (direction != 'down' || !pausePage) return
  406. let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop,
  407. scrollHeight = window.innerHeight || document.documentElement.clientHeight,
  408. scrollDelta = curSite.pager.scrollDelta;
  409. if (document.documentElement.scrollHeight <= scrollHeight + scrollTop + scrollDelta) {
  410. if (curSite.pager.type === 1) {
  411. ShowPager.loadMorePage();
  412. }else{
  413. let autopbn = document.querySelector(curSite.pager.nextLink);
  414. if (autopbn){
  415. autopbn.click();
  416. pausePage = false
  417. setTimeout(function(){pausePage = true;}, 500)
  418. }
  419. }
  420. }
  421. });
  422. }
  423. }
  424.  
  425.  
  426. // 滚动条事件
  427. function windowScroll(fn1) {
  428. var beforeScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
  429. fn = fn1 || function () {};
  430. setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件
  431. window.addEventListener('scroll', function (e) {
  432. var afterScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
  433. delta = afterScrollTop - beforeScrollTop;
  434. if (delta == 0) return false;
  435. fn(delta > 0 ? 'down' : 'up', e);
  436. beforeScrollTop = afterScrollTop;
  437. }, false);
  438. }, 1000)
  439. }
  440.  
  441.  
  442. // 修改自 https://greasyfork.org/scripts/14178 , https://github.com/machsix/Super-preloader
  443. var ShowPager = {
  444. getFullHref: function (e) {
  445. if (e != null && e.nodeType === 1 && e.href && e.href.slice(0,4) === 'http') return e.href;
  446. return '';
  447. },
  448. createDocumentByString: function (e) {
  449. if (e) {
  450. if ('HTML' !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, 'application/xhtml+xml');
  451. var t;
  452. try { t = (new DOMParser).parseFromString(e, 'text/html');} catch (e) {}
  453. if (t) return t;
  454. if (document.implementation.createHTMLDocument) {
  455. t = document.implementation.createHTMLDocument('ADocument');
  456. } else {
  457. 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) {}
  458. }
  459. if (t) {
  460. var r = document.createRange(),
  461. n = r.createContextualFragment(e);
  462. r.selectNodeContents(document.body);
  463. t.body.appendChild(n);
  464. 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);
  465. return t;
  466. }
  467. } else console.error('没有找到要转成 DOM 的字符串');
  468. },
  469. loadMorePage: function () {
  470. if (curSite.pager) {
  471. let curPageEle = getElementByXpath(curSite.pager.nextLink);
  472. var url = this.getFullHref(curPageEle);
  473. console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
  474. if(url === '') return;
  475. if(curSite.pageUrl === url) return;// 不会重复加载相同的页面
  476. curSite.pageUrl = url;
  477. // 读取下一页的数据
  478. curSite.pager.startFilter && curSite.pager.startFilter();
  479. GM_xmlhttpRequest({
  480. url: url,
  481. method: "GET",
  482. timeout: 5000,
  483. onload: function (response) {
  484. try {
  485. var newBody = ShowPager.createDocumentByString(response.responseText);
  486. let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody);
  487. let toElement = getAllElements(curSite.pager.HT_insert[0])[0];
  488. if (pageElems.length >= 0) {
  489. // 如果有插入前函数就执行函数
  490. if (curSite.function && curSite.function.before) {
  491. if (curSite.function.parameter) { // 如果指定了参数
  492. pageElems = curSite.function.before(curSite.function.parameter);
  493. }else{
  494. pageElems = curSite.function.before(pageElems);
  495. }
  496. }
  497. // 插入位置
  498. let addTo;
  499. switch (curSite.pager.HT_insert[1]) {
  500. case 1:
  501. addTo = "beforebegin"
  502. break;
  503. case 2:
  504. addTo = "afterbegin"
  505. break;
  506. case 3:
  507. addTo = "beforeend"
  508. break;
  509. case 4:
  510. addTo = "afterend"
  511. break;
  512. }
  513. // 插入新页面元素
  514. pageElems.forEach(function (one) {
  515. toElement.insertAdjacentElement(addTo, one);
  516. });
  517. // 替换待替换元素
  518. try {
  519. let oriE = getAllElements(curSite.pager.replaceE);
  520. let repE = getAllElements(curSite.pager.replaceE, newBody, newBody);
  521. if (oriE.length === repE.length) {
  522. for (var i = 0; i < oriE.length; i++) {
  523. oriE[i].outerHTML = repE[i].outerHTML;
  524. }
  525. }
  526. } catch (e) {
  527. console.log(e);
  528. }
  529. // 如果有插入后函数就执行函数
  530. if (curSite.function && curSite.function.after) {
  531. if (curSite.function.parameter) { // 如果指定了参数
  532. curSite.function.after(curSite.function.parameter);
  533. }else{
  534. curSite.function.after();
  535. }
  536. }
  537. }
  538. } catch (e) {
  539. console.log(e);
  540. }
  541. }
  542. });
  543. }
  544. },
  545. };
  546. function getElementByCSS(css, contextNode = document) {
  547. return contextNode.querySelector(css);
  548. }
  549. function getAllElementsByCSS(css, contextNode = document) {
  550. return [].slice.call(contextNode.querySelectorAll(css));
  551. }
  552. function getElementByXpath(xpath, contextNode, doc = document) {
  553. contextNode = contextNode || doc;
  554. try {
  555. const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  556. // 应该总是返回一个元素节点
  557. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  558. } catch (err) {
  559. throw new Error(`Invalid xpath: ${xpath}`);
  560. }
  561. }
  562. function getAllElementsByXpath(xpath, contextNode, doc = document) {
  563. contextNode = contextNode || doc;
  564. const result = [];
  565. try {
  566. const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  567. for (let i = 0; i < query.snapshotLength; i++) {
  568. const node = query.snapshotItem(i);
  569. // 如果是 Element 节点
  570. if (node.nodeType === 1) result.push(node);
  571. }
  572. } catch (err) {
  573. throw new Error(`无效 Xpath: ${xpath}`);
  574. }
  575. return result;
  576. }
  577. function getAllElements(selector, contextNode = undefined, doc = document, win = window, _cplink = undefined) {
  578. if (!selector) return [];
  579. contextNode = contextNode || doc;
  580. if (typeof selector === 'string') {
  581. if (selector.search(/^css;/i) === 0) {
  582. return getAllElementsByCSS(selector.slice(4), contextNode);
  583. } else {
  584. return getAllElementsByXpath(selector, contextNode, doc);
  585. }
  586. } else {
  587. const query = selector(doc, win, _cplink);
  588. if (!Array.isArray(query)) {
  589. throw new Error('getAllElements 返回错误类型');
  590. } else {
  591. return query;
  592. }
  593. }
  594. }
  595. })();