Greasy Fork

选中文本

选中的文本后在浏览器右上角弹出菜单(位置固定),可以进行搜索,复制,识别其中的https/https网站。

当前为 2023-02-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         选中文本
// @author       LceAn
// @version      1.0.1
// @namespace    https://greasyfork.org/zh-CN/users/858044

// @description  选中的文本后在浏览器右上角弹出菜单(位置固定),可以进行搜索,复制,识别其中的https/https网站。

// @match        http://*/*
// @include         http://*
// @include         https://*
// @grant        GM_setClipboard
// @encoding         utf-8
// @license          GPL-3.0 License
// ==/UserScript==

(function() {
    'use strict';

    // Create the menu element
    const menu = document.createElement('div');
    menu.id = 'text-selection-menu';
    menu.style.position = 'fixed';
    menu.style.top = '20px';
    menu.style.right = '20px';
    menu.style.border = '1px solid #ccc';
    menu.style.background = '#fff';
    menu.style.padding = '10px';
    menu.style.borderRadius = '5px';
    menu.style.boxShadow = '2px 2px 5px #ccc';
    menu.style.display = 'none';
    document.body.appendChild(menu);

    // Create the search option
    const searchOption = document.createElement('a');
    searchOption.href = '#';
    searchOption.textContent = '搜索';
    searchOption.style.display = 'block';
    searchOption.style.marginBottom = '5px';
    menu.appendChild(searchOption);

    // Create the copy option
    const copyOption = document.createElement('a');
    copyOption.href = '#';
    copyOption.textContent = '复制';
    copyOption.style.display = 'block';
    copyOption.style.marginBottom = '5px';
    menu.appendChild(copyOption);

    // Create the link option
    const linkOption = document.createElement('a');
    linkOption.href = '#';
    linkOption.textContent = '链接';
    linkOption.style.display = 'block';
    menu.appendChild(linkOption);

    // Add click event listeners to the options
    searchOption.addEventListener('click', function(event) {
        event.preventDefault();
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            window.open('https://www.google.com/search?q=' + selectedText, '_blank');
        }
        menu.style.display = 'none';
    });

    copyOption.addEventListener('click', function(event) {
        event.preventDefault();
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            GM_setClipboard(selectedText);
        }
        menu.style.display = 'none';
    });
    
    linkOption.addEventListener('click', function(event) {
      event.preventDefault();
      const selectedText = window.getSelection().toString();
      const urlRegex = /((https?:\/\/|www\.)[\x21-\x7e]+[\w\/=]|\w([\w._-])+@\w[\w\._-]+\.(com|cn|org|net|info|tv|cc|gov|edu)|(\w[\w._-]+\.(com|cn|org|net|info|tv|cc|gov|edu))(\/[\x21-\x7e]*[\w\/])?|ed2k:\/\/[\x21-\x7e]+\|\/|thunder:\/\/[\x21-\x7e]+=)/gi;
      const selectedUrl = selectedText.match(urlRegex);
      if (selectedUrl) {
        window.open(selectedUrl[0], '_blank');
      } else {
        const messageBox = document.createElement('div');
        messageBox.textContent = '未匹配到链接,请重试';
        messageBox.style.position = 'fixed';
        messageBox.style.top = '0';
        messageBox.style.left = '0';
        messageBox.style.right = '0';
        messageBox.style.backgroundColor = 'rgba(255, 255, 255, 0.8)';
        messageBox.style.color = 'black';
        messageBox.style.padding = '10px';
        messageBox.style.fontSize = '18px';
        messageBox.style.fontWeight = 'bold';
        messageBox.style.textAlign = 'center';
        messageBox.style.zIndex = '9999';
        document.body.appendChild(messageBox);
        setTimeout(function() {
          messageBox.style.display = 'none';
        }, 1000); // 1 秒后隐藏提示框
      }
      menu.style.display = 'none';
    });

    // Add a listener for text selection
    document.addEventListener('mouseup', function(event) {
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            menu.style.display = 'block';
        } else {
            menu.style.display = 'none';
        }
    });
})();