🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/22 09:22:46
🔄 下次更新时间:2026/01/22 10:22:46
手动刷新缓存

Open Links

Select links with Z key and open them in new tabs

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Open Links
// @namespace    https://github.com/mefengl
// @version      1.0.3
// @description  Select links with Z key and open them in new tabs
// @author       mefengl
// @match        *://*/*
// @grant        GM_openInTab
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  let selectionRectangle = null;
  let elementsInsideRectangle = [];
  let countLabel = null;
  let zKeyPressed = false;
  let startX, startY;

  function createRectangle(x, y) {
    let div = document.createElement('div');
    div.style.border = '1px dashed red';
    div.style.position = 'fixed';
    div.style.pointerEvents = 'none';
    return div;
  }

  function createCountLabel() {
    let div = document.createElement('div');
    div.style.position = 'absolute';
    div.style.bottom = '0px';
    div.style.right = '5px';
    div.style.backgroundColor = 'transparent';
    return div;
  }

  function detectElementsInRectangle() {
    elementsInsideRectangle = Array.from(document.querySelectorAll('a')).filter(el => {
      let rect = el.getBoundingClientRect();
      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));
    });

    elementsInsideRectangle.forEach(el => el.style.border = '1px solid red');

    countLabel.innerText = elementsInsideRectangle.length;
  }

  function removeBordersFromLinks() {
    document.querySelectorAll('a').forEach(el => el.style.border = '');
  }

  let mouseDown = false;
  document.addEventListener('mousedown', () => {
    mouseDown = true;
  });
  document.addEventListener('mouseup', () => {
    mouseDown = false;
  });

  document.addEventListener('keydown', (e) => {
    if (e.key.toLowerCase() === 'z' && mouseDown) {
      e.preventDefault();
    } else if (e.key.toLowerCase() === 'z') {
      zKeyPressed = true;
    }
  });

  document.addEventListener('keyup', (e) => {
    if (e.key.toLowerCase() === 'z') {
      zKeyPressed = false;
      openLinksAndClear();
    }
  });


  document.addEventListener('mousedown', (e) => {
    if (!zKeyPressed || e.buttons === 0) {
      clearSelection();
      return;
    }
    e.preventDefault();
    removeBordersFromLinks();
    startX = e.clientX;
    startY = e.clientY;
    selectionRectangle = createRectangle(startX, startY);
    document.body.appendChild(selectionRectangle);
    countLabel = createCountLabel();
    selectionRectangle.appendChild(countLabel);
  });

  document.addEventListener('mousemove', (e) => {
    if (!zKeyPressed || !selectionRectangle || e.buttons === 0) {
      clearSelection();
      return;
    }
    e.preventDefault();
    removeBordersFromLinks();
    selectionRectangle.style.left = Math.min(e.clientX, startX) + 'px';
    selectionRectangle.style.top = Math.min(e.clientY, startY) + 'px';
    selectionRectangle.style.width = Math.abs(e.clientX - startX) + 'px';
    selectionRectangle.style.height = Math.abs(e.clientY - startY) + 'px';
    detectElementsInRectangle();
  });

  function openLinksInBackground(urls) {
    urls.forEach((url) => {
      GM_openInTab(url, { active: false });
    });
  }

  // Clear selection and open links if mouse is released before 'z'
  document.addEventListener('mouseup', (e) => {
    if (!zKeyPressed || !selectionRectangle || e.buttons !== 0) {
      clearSelection();
      return;
    }
    e.preventDefault();
    openLinksAndClear();
  });

  function clearSelection() {
    if (selectionRectangle) {
      document.body.removeChild(selectionRectangle);
      selectionRectangle = null;
    }
    removeBordersFromLinks();
  }

  // To open links and clear selection
  function openLinksAndClear() {
    if (selectionRectangle) {
      const urlsToOpen = [...new Set(elementsInsideRectangle.map(el => el.href))]
      openLinksInBackground(urlsToOpen);
      clearSelection();
    }
  }
})();