Greasy Fork

来自缓存

Greasy Fork is available in English.

选中文本

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         选中文本
// @author       LceAn
// @version      0.0.3
// @namespace    http://greasyfork.icu/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 = /((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';
        }
    });
})();