V2EX 增强

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

当前为 2021-08-18 提交的版本,查看 最新版本

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