LinksListBatchOpen

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-03-24 提交的版本,查看 最新版本

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