划词新菜单

由 酷安@达蒙山 的划词菜单修改而来

当前为 2023-12-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         划词新菜单
// @namespace    https://viayoo.com/
// @version      0.4
// @description  由 酷安@达蒙山 的划词菜单修改而来
// @author       呆毛飘啊飘2171802813
// @run-at       document-start
// @match        *
// @grant        none
// ==/UserScript==

! function() {
if(document.getElementById("hckj")){
document.getElementById('hckj').remove();
}
  window.addEventListener('contextmenu', function(e){  
      e.preventDefault();  
  });
    var ssyq = [{
        name: "复制",
        url: "viek://copytext/"
    }, {
        name: "全选",
        url: "viek://share/"
    }, {
        name: "搜索",
        url: "viek://search/"
    }, {
        name: "翻译",
        url: "https://fanyi.baidu.com/?tpltype=sigmah#en/zh/"
    }],
        hcTimer,
        ljurl,
        text;

    function hccd() {
        text = window.getSelection()
            .toString()
            .trim();
        text ? (document.getElementById("hckj")
            .style.top = sy, document.getElementById("hckj")
            .style.display = "block", zdcd()) : document.getElementById("hckj")
            .style.display = "none";
    }

    function tzurl(a, b) {
        b = b || text;
        if (typeof(window.viek) == "undefined" || window.viek == null) {
            if (a == "viek://share/") {
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();var r=document.createRange();r.selectNode(document.documentElement);window.getSelection().addRange(r);
            } else if (a == "viek://copytext/") {
                copyText(b);
            } else if (a == "viek://search/") {
                if (typeof(window.via) == "undefined" || window.via == null) {
                    ljurl = 'https://m.baidu.com/s?word=' + b;
                    window.open(ljurl);
                } else {
                    via.searchText(b);
                }
            } else {
                ljurl = a + b;
                window.open(ljurl);
            }
        } else {
            ljurl = a + b;
            window.open(ljurl);
        }
    }

    function zdcd() {
        document.getElementById("hckj")
            .style.left = (document.documentElement.clientWidth - document.getElementById("hckj")
            .offsetWidth) / 2 + "px";
        var zdurl = text.match(/(https?:\/\/(\w[\w-]*\.)+[A-Za-z]{2,4}(?!\w)(:\d+)?(\/([\x21-\x7e]*[\w\/=])?)?|(\w[\w-]*\.)+(com|cn|org|net|info|tv|cc|gov|edu)(?!\w)(:\d+)?(\/([\x21-\x7e]*[\w\/=])?)?)/i)[0];
        if (zdurl) {
            var tzlj = document.createElement("span");
            tzlj.id = "zdan";
            tzlj.innerHTML = "转到";
            tzlj.addEventListener("click",

            function() {
                zdurl.indexOf("http") < 0 ? tzurl("http://", zdurl) : tzurl("", zdurl);
            });
            document.getElementById("hckj")
                .appendChild(tzlj);
        };
        document.getElementById("hckj")
            .style.left = (document.documentElement.clientWidth - document.getElementById("hckj")
            .offsetWidth) / 2 + "px";
    }

    if (!document.getElementById("cdkj")) {
        var cddiv = document.createElement("div");
        cddiv.id = "cdkj";
        cddiv.style.cssText = "width:100%;display:block!important;position:fixed;top:0px;z-index:99999999;text-align:center;-webkit-tap-highlight-color:rgba(0,0,0,0);";
        document.body.appendChild(cddiv);
        var cdstyle = document.createElement("style");
        cdstyle.type = "text/css";
        cdstyle.innerHTML = "#cdkj span{user-select: none;display:inline-block;border:1px solid #c5c5c5;background:#6a6a6a;color:#fff;font-size:12px;line-height:16px;padding:4px 6px 4px 6px;}";
        document.body.appendChild(cdstyle);
    }
    var hcdiv = document.createElement("div");
    hcdiv.id = "hckj";
    hcdiv.style.cssText = "border:1px solid #c5c5c5;background:#c5c5c5;position: absolute;display:none";
    document.getElementById("cdkj")
        .appendChild(hcdiv);

    for (var i = 0; i < ssyq.length; i++) {
        var jksp = document.createElement("span");
        jksp.innerHTML = ssyq[i].name;
        jksp.setAttribute("jkdz", ssyq[i].url);
        jksp.onclick = function() {
            tzurl(this.getAttribute("jkdz"));
        };
        document.getElementById("hckj")
            .appendChild(jksp);
    }

    document.addEventListener("selectionchange", function() {
        clearTimeout(hcTimer);
        hcTimer = setTimeout(hccd, 75);
        if (document.getElementById("zdan")) {
            document.getElementById("zdan")
                .parentNode.removeChild(document.getElementById("zdan"));
        }
    });

    document.addEventListener('touchstart', function(e) {
        sy = e.touches[0].clientY + 20;
        sm = sy + 25;
        if (document.documentElement.clientHeight < sm) {
            sy = document.documentElement.clientHeight - 100;
        }
        sy = sy + "px"
    });


    function copyText(txt) {
        var x = document.body.scrollTop;
        var y = document.documentElement.scrollTop;
        const ta = document.createElement('textarea');
        ta.value = txt;
        ta.style.position = 'absolute';
        ta.style.opacity = '0';
        ta.style.left = '-999999px';
        ta.style.top = '-999999px';
        document.body.appendChild(ta);
        ta.focus();
        ta.select();
        document.execCommand('copy');
        document.body.removeChild(ta);
        window.scrollTo(x, y);
    };
}();