选中文本

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

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

// ==UserScript==
// @name         选中文本
// @author       aeae------LceAn(www.lcean.cf)
// @version      1.0.7
// @namespace    https://gf.qytechs.cn/zh-CN/users/858044

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

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

(function() {
    'use strict';


    //-------------------------------菜单部分--------------------------------------

    // 创建一个名为“menu”的div元素
    const menu = document.createElement('div');
    // 设置div元素的ID为"text-selection-menu"
    menu.id = 'text-selection-menu';
    // 设置div元素的样式为固定定位
    menu.style.position = 'fixed';
    // 设置div元素距离顶部的位置为55像素
    menu.style.top = '55px';


    //// 设置div元素距离右侧的位置为20像素
    //menu.style.right = '20px';
    //设置div元素距离左侧的位置为20像素
    menu.style.left = '20px';


    // 设置div元素的边框样式为1像素的实线边框,颜色为#ccc
    menu.style.border = '1px solid #ccc';
    // 设置div元素的背景颜色为白色
    menu.style.background = '#fff';
    // 设置div元素的内边距为10像素
    menu.style.padding = '10px';
    // 设置div元素的圆角半径为5像素
    menu.style.borderRadius = '5px';
    // 设置透明
    menu.style.opacity = "0.6";
    // 设置元素处于最上层,不会被其他覆盖
    menu.style.zIndex = '99999';
    // 设置div元素的阴影效果
    menu.style.boxShadow = '2px 2px 5px #ccc';
    // 设置div元素的显示状态为不显示
    menu.style.display = 'none';
    // 将div元素添加到文档的body元素中
    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';
    // 设置鼠标悬停状态
    searchOption.addEventListener('mouseover', function() {
        searchOption.style.opacity = '1';
    });
    // 设置鼠标离开状态
    searchOption.addEventListener('mouseout', function() {
        searchOption.style.opacity = '0.5';
    });
    menu.appendChild(searchOption);

    // Create the copy option
    const copyOption = document.createElement('a');
    copyOption.href = '#';
    copyOption.textContent = '复制';
    copyOption.style.display = 'block';
    copyOption.style.marginBottom = '5px';
    // 设置鼠标悬停状态
    copyOption.addEventListener('mouseover', function() {
        copyOption.style.opacity = '1';
    });
    // 设置鼠标离开状态
    copyOption.addEventListener('mouseout', function() {
        copyOption.style.opacity = '0.5';
    });
    menu.appendChild(copyOption);

    // Create the link option
    const linkOption = document.createElement('a');
    linkOption.href = '#';
    linkOption.textContent = '链接';
    linkOption.style.display = 'block';
    linkOption.style.marginBottom = '5px';
    // 设置鼠标悬停状态
    linkOption.addEventListener('mouseover', function() {
        linkOption.style.opacity = '1';
    });
    // 设置鼠标离开状态
    linkOption.addEventListener('mouseout', function() {
        linkOption.style.opacity = '0.5';
    });
    menu.appendChild(linkOption);

    const imageOption = document.createElement('a');
    imageOption.href = '#';
    imageOption.textContent = '搜图';
    imageOption.style.display = 'block';
    imageOption.style.marginBottom = '5px';
    // 设置鼠标悬停状态
    imageOption.addEventListener('mouseover', function() {
        imageOption.style.opacity = '1';
    });
    // 设置鼠标离开状态
    imageOption.addEventListener('mouseout', function() {
        imageOption.style.opacity = '0.5';
    });
    menu.appendChild(imageOption);

    const translateOption = document.createElement('a');
    translateOption.href = '#';
    translateOption.textContent = '翻译';
    translateOption.style.display = 'block';
    // 设置鼠标悬停状态
    translateOption.addEventListener('mouseover', function() {
        translateOption.style.opacity = '1';
    });
    // 设置鼠标离开状态
    translateOption.addEventListener('mouseout', function() {
        translateOption.style.opacity = '0.5';
    });
    menu.appendChild(translateOption);




    //-------------------------------功能部分--------------------------------------

    // 搜索函数---------------------
    searchOption.addEventListener('click', function(event) {
        event.preventDefault();
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            //需要更改引擎,请将默认引擎使用//在开头进行注释后,删除自己需要的引擎前面的注释“//”以此实现更换引擎

            window.open('https://www.baidu.com/s?wd=' + selectedText, '_blank');          //百度引擎
            //window.open('https://www.google.com/search?q=' + selectedText, '_blank');   //谷歌引擎
            //window.open('https://www.bing.com/search?q=' + selectedText, '_blank');     //Bing引擎
        }
        menu.style.display = 'none';
    });


    // 复制函数---------------------
    copyOption.addEventListener('click', function(event) {
        event.preventDefault();
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            GM_setClipboard(selectedText);
        }
        showMessage('Copied The Text: '+ selectedText , 1000);
        menu.style.display = 'none';
    });

    // 链接函数---------------------
    // 链接无法匹配消息对话框样式函数
    function showMessage(message, timeout) {
      const messageBox = document.createElement('div');
      messageBox.textContent = message;
      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';
      }, timeout);
    }
    
    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) {
      const url = selectedUrl[0];
      if (!/^https?:\/\//i.test(url)) {
        window.open(`http://${url}`, '_blank');
      } else {
        window.open(url, '_blank');
      }
    }else {
        showMessage('未匹配到链接!', 1000);
      }
      menu.style.display = 'none';
    });

    // 图片函数---------------------
    imageOption.addEventListener('click', function(event) {
    event.preventDefault();
    const selectedText = window.getSelection().toString();
    if (selectedText) {
        const searchUrl = `https://www.google.com/search?tbm=isch&q=${encodeURIComponent(selectedText)}`;
        window.open(searchUrl, '_blank');
    } else {
        window.open('https://www.google.com/imghp', '_blank');
    }
    });

    // 翻译函数---------------------
    translateOption.addEventListener('click', function(event) {
    event.preventDefault();
    const selectedText = window.getSelection().toString();
    if (selectedText) {
        const translateUrl = `https://translate.google.com/?sl=auto&tl=en&text=${encodeURIComponent(selectedText)}&op=translate`;
        window.open(translateUrl, '_blank');
    } else {
        window.open('https://translate.google.com/', '_blank');
    }
    });




    //-------------------------------事件部分--------------------------------------
    // 监听鼠标事件,关闭菜单
    document.addEventListener('mouseup', function(event) {
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            menu.style.display = 'block';
        } else {
            menu.style.display = 'none';
        }
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址