连接转换transformation to links

将网页的链接转换为可点击跳转的标签,免去手动复制,如页面有其他元素显示异常请手动排除

目前为 2022-06-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         连接转换transformation to links
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  将网页的链接转换为可点击跳转的标签,免去手动复制,如页面有其他元素显示异常请手动排除
// @author       code200
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @license      GPL License
// ==/UserScript==

(function() {
    const reg=/(^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$)|(http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)/g;
    GM_registerMenuCommand('已转换:' + (GM_getValue('transformation_count')==undefined?0:GM_getValue('transformation_count')) + '次(点击重置)', () => {
               GM_setValue('transformation_count', 0)
                        history.go(0);
            });
    let exclude=new Map;
    exclude.set('A',1);
    exclude.set('SCRIPT',1);
    function getNode(ele){
        var array = ele.childNodes;
        for(var i = 0;i < array.length;i++){
            var childNode = array[i];
            if(childNode.nodeType == 1){
                getNode(childNode);
            }else if(childNode.nodeType == 3){
                var textContent=childNode.textContent;
                if(reg.test(String(textContent))){
                    //if(exclude.get(childNode.parentElement.tagName)===1){
                    //    return;
                    //}
                    let newTextContent=textContent.replace(reg,function(arg1,arg2,arg3,arg4){
                        let textContent='<a href="'+arg1+'" style="color: red !important;border-bottom: 1px solid red !important;text-shadow: 0px 2px 10px #000 !important;" target="_blank">'+arg1+'</a>'
                        return textContent;
                    })
                    childNode.parentElement.innerHTML=newTextContent;
                    if(GM_getValue('transformation_count')==undefined){
                        GM_setValue('transformation_count', 1);
                    }else{
                        GM_setValue('transformation_count', GM_getValue('transformation_count')+1);
                    }

                }
            }
        }
    }
    setTimeout(()=>{
        var nodes = document.body;
        getNode(nodes);
    },1000);



})();