Open Links

Select links with Z key and open them in new tabs

当前为 2023-07-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Open Links
  3. // @namespace https://github.com/mefengl
  4. // @version 0.0.1
  5. // @description Select links with Z key and open them in new tabs
  6. // @author mefengl
  7. // @include *
  8. // @grant GM.openInTab
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. let selectionRectangle = null;
  15. let elementsInsideRectangle = [];
  16. let countLabel = null;
  17. let zKeyPressed = false;
  18. let startX, startY;
  19.  
  20. // Function to create the selection rectangle
  21. function createRectangle(x, y) {
  22. let div = document.createElement('div');
  23. div.style.border = '1px dashed red';
  24. div.style.position = 'fixed';
  25. div.style.pointerEvents = 'none';
  26. return div;
  27. }
  28.  
  29. // Function to create the count label
  30. function createCountLabel() {
  31. let div = document.createElement('div');
  32. div.style.position = 'absolute';
  33. div.style.bottom = '0px';
  34. div.style.right = '5px';
  35. div.style.backgroundColor = 'transparent';
  36. return div;
  37. }
  38.  
  39. // Function to detect <a> elements inside the rectangle
  40. function detectElementsInRectangle() {
  41. elementsInsideRectangle = Array.from(document.querySelectorAll('a')).filter(el => {
  42. let rect = el.getBoundingClientRect();
  43. return rect.top >= parseInt(selectionRectangle.style.top) && rect.left >= parseInt(selectionRectangle.style.left) && rect.bottom <= (parseInt(selectionRectangle.style.top) + parseInt(selectionRectangle.style.height)) && rect.right <= (parseInt(selectionRectangle.style.left) + parseInt(selectionRectangle.style.width));
  44. });
  45.  
  46. elementsInsideRectangle.forEach(el => el.style.border = '1px solid red'); // Add red border to <a> elements
  47.  
  48. // Update the count label
  49. countLabel.innerText = elementsInsideRectangle.length;
  50. }
  51.  
  52. // Function to remove the border from <a> elements no longer inside the rectangle
  53. function removeBordersFromLinks() {
  54. document.querySelectorAll('a').forEach(el => el.style.border = ''); // Remove border
  55. }
  56.  
  57. // Key down event
  58. document.addEventListener('keydown', (e) => {
  59. if (e.key.toLowerCase() === 'z') {
  60. zKeyPressed = true;
  61. }
  62. });
  63.  
  64. // Key up event
  65. document.addEventListener('keyup', (e) => {
  66. if (e.key.toLowerCase() === 'z') {
  67. zKeyPressed = false;
  68. }
  69. });
  70.  
  71. // Mouse down event
  72. document.addEventListener('mousedown', (e) => {
  73. if (!zKeyPressed) return;
  74. e.preventDefault(); // Prevent text selection
  75. removeBordersFromLinks();
  76. startX = e.clientX;
  77. startY = e.clientY;
  78. selectionRectangle = createRectangle(startX, startY);
  79. document.body.appendChild(selectionRectangle);
  80. countLabel = createCountLabel();
  81. selectionRectangle.appendChild(countLabel);
  82. });
  83.  
  84. // Mouse move event
  85. document.addEventListener('mousemove', (e) => {
  86. if (!zKeyPressed || !selectionRectangle) return;
  87. e.preventDefault(); // Prevent text selection
  88. removeBordersFromLinks();
  89. selectionRectangle.style.left = Math.min(e.clientX, startX) + 'px';
  90. selectionRectangle.style.top = Math.min(e.clientY, startY) + 'px';
  91. selectionRectangle.style.width = Math.abs(e.clientX - startX) + 'px';
  92. selectionRectangle.style.height = Math.abs(e.clientY - startY) + 'px';
  93. detectElementsInRectangle();
  94. });
  95.  
  96. function openLinksInBackground(urls) {
  97. urls.forEach((url) => {
  98. GM.openInTab(url, true);
  99. });
  100. }
  101.  
  102. // Mouse up event
  103. document.addEventListener('mouseup', (e) => {
  104. if (!zKeyPressed || !selectionRectangle) return;
  105. e.preventDefault(); // Prevent text selection
  106. let urlsToOpen = elementsInsideRectangle.map(el => el.href);
  107. openLinksInBackground(urlsToOpen);
  108. document.body.removeChild(selectionRectangle);
  109. removeBordersFromLinks();
  110. selectionRectangle = null;
  111. });
  112.  
  113. })();