Greasy Fork

选中文本

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

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

// ==UserScript==
// @name         选中文本
// @namespace    http://tampermonkey.net/
// @version      0.0.1
// @description  选中的文本后在浏览器右上角弹出菜单(位置固定),可以进行搜索,复制,识别其中的https/https网站。
// @author       LceAn
// @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 = 'Search';
    searchOption.style.display = 'block';
    searchOption.style.marginBottom = '5px';
    menu.appendChild(searchOption);

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

    // Create the link option
    const linkOption = document.createElement('a');
    linkOption.href = '#';
    linkOption.textContent = 'Link';
    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 = /((http|https):\/\/[^\s]+)/g;
        const selectedUrl = selectedText.match(urlRegex);
        if (selectedUrl) {
            window.open(selectedUrl[0], '_blank');
        }
        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';
        }
    });
})();