Alt + 123... Searching Results Links List Batch Open

To quickly understand a field, press Alt+1 ...2,3,4...Alt+5 on the search page of Google or Bing to open the search results of the first 2 nth power items and copy the opened ones link. Currently supports: Google, Bing, Zhihu.

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

  1. // ==UserScript==
  2. // @name Alt + 123... Searching Results Links List Batch Open
  3. // @name:zh Alt + 123... 一键批量打开谷歌必应搜索的前2的n次方项搜索结果
  4. // @namespace snomiao@gmail.com
  5. // @version 1.0.1
  6. // @description To quickly understand a field, press Alt+1 ...2,3,4...Alt+5 on the search page of Google or Bing to open the search results of the first 2 nth power items and copy the opened ones link. Currently supports: Google, Bing, Zhihu.
  7. // @description:zh 快速了解一个领域用,在谷歌或必应的搜索页面 按 Alt+1 ...2,3,4... Alt+5 将会打开前2的n次方项的搜索结果,并复制打开的链接。目前支持:谷歌、必应、知乎。
  8. // @author snomiao
  9. // @match *://google.com/*
  10. // @match *://bing.com/*
  11. // @match *://youtube.com/*
  12. // @match *://zhihu.com/*
  13. // @match *://and-all-searching-results.com/*
  14. // @match *://*/*
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. const 最长共列输出 = (矩阵, a, b, x, y) =>
  19. !x || !y
  20. ? ''
  21. : a[x - 1] === b[y - 1]
  22. ? 最长共列输出(矩阵, a, b, x - 1, y - 1) + a[x - 1]
  23. : 矩阵[y][x - 1] > 矩阵[y - 1][x]
  24. ? 最长共列输出(矩阵, a, b, x - 1, y)
  25. : 最长共列输出(矩阵, a, b, x, y - 1)
  26. const 最长共列 = (a, b) => {
  27. const w = a.length, h = b.length
  28. const m = Array(1 + h).fill(0).map(() => Array(1 + w).fill(0))
  29. for (let y = 0; y < h; y++)
  30. for (let x = 0; x < w; x++)
  31. m[1 + y][1 + x] =
  32. a[x] === b[y]
  33. ? m[y][x] + 1
  34. : Math.max(m[y][1 + x], m[1 + y][x])
  35. false && console.table(m)
  36. return 最长共列输出(m, a, b, w, h)
  37. }
  38. const 元素特征列取 = (e) => [...e.querySelectorAll('*')]
  39. .map(e => e?.tagName + e?.className)
  40. const 元素列表强度 = e => e.textContent.length * (e.children.length - 1)
  41. * [...e.children]
  42. .filter(e => !'style script'.toUpperCase().split(' ').includes(e.tagName))
  43. .map(元素特征列取)
  44. .map((_, i, a) => 最长共列(a[i], a[i + 1] || []))
  45. .reduce((前, 后) => + 后.length, 0)
  46. const 列元素列提取 = () => [...document.querySelectorAll('div,dl,ul,ol,tbody,table,td')]
  47. .filter(e => e.children.length > 1)
  48. .map(e => [元素列表强度(e), e, [...e.children]
  49. .filter(e => !'style script span'.split(' ').includes(e.tagName))
  50. .map(元素特征列取)])
  51. .sort((a, b) => a[0] - b[0])
  52. .reverse()
  53. const 标链元素提取 = e =>
  54. (e
  55. ?.querySelector('dd,dt,h1,h2,h3,h4,h5,h6')
  56. ?.querySelector('a'))
  57. || ([...e?.querySelectorAll('a')]
  58. ?.filter(e => e.querySelector('dd,dt,h1,h2,h3,h4,h5,h6'))
  59. ?.[0])
  60. || e?.querySelector('a')
  61. const 标链提取 = (e) => e && [
  62. e?.textContent?.replace(/\s+/g, ' ').trim(),
  63. e?.href]
  64. const 页主标链列提取 = () => 列元素列提取()
  65. .filter((x, i, a) => a[i][0] > a[0][0] * 0.1)
  66. .sort((a, b) => a[1].offsetTop - b[1].offsetTop)
  67. .map(([r, e]) => e)
  68. .flatMap(e => [
  69. ...e?.children]
  70. ?.map?.(标链元素提取)
  71. ?.map?.(标链提取) || [])
  72. .filter(e => e)
  73. const 文本复制 = (内容) => {
  74. const input = document.createElement('textarea');
  75. input.setAttribute('readonly', 'readonly');
  76. input.setAttribute('value', 内容);
  77. input.innerHTML = (内容);
  78. input.setAttribute('style', 'position: fixed; top:0; left:0;z-index: 9999');
  79. document.body.appendChild(input);
  80. input.select();
  81. input.setSelectionRange(0, input.value.length);
  82. if (document.execCommand('copy')) {
  83. document.execCommand('copy');
  84. console.log(`长度为${内容.length}的内容已复制`);
  85. } else {
  86. // alert(`长度为${内容.length}的内容复制失败,请检查浏览器配置,或在页面上先按一下键盘任意键解除剪贴板保护`);
  87. }
  88. document.body.removeChild(input);
  89. };
  90. const 已打开过的链接 = {}
  91. const 链接打开 = (链接) => { if (!已打开过的链接[链接]) { window.open(链接) }; 已打开过的链接[链接] = 1 };
  92. const 链接列获取 = 数量 => 页主标链列提取()
  93. .map(([标题, 链接]) => ({ 标题, 链接 }))
  94. .slice(0, 数量)
  95. .reverse() //在 chrome 下需要反过来。。
  96. const 打开一定数量链接 = (数量) => {
  97. const 链接列 = 链接列获取(数量)
  98. 文本复制(链接列.map(({ 标题, 链接 }) => "#" + 标题 + '\n' + 链接).join('\n\n'))
  99. 链接列.map(({ 链接 }) => 链接).map(链接打开)
  100. }
  101. window.addEventListener("keydown", (e) => {
  102. if (!e.ctrlKey && !e.shiftKey && e.altKey && e.key.match(/[1-9]/)) {
  103. const 打开数量 = 2 ** parseInt(e.key)
  104. 打开一定数量链接(打开数量)
  105. }
  106. })
  107. console.log("[loaded]: 谷歌一键打开前N项搜索结果, Copy")