Linkify·改

文本转超链接

// ==UserScript==
// @name              Linkify·改
// @version           1.1.3.1
// @author            极品小猫
// @description        文本转超链接
// @en:description      Looks for things in the page that look like URLs but aren't hyperlinked, and converts them to clickable links.
// @include           *://*/*
// @exclude           http*://www.baidu.com/*
// @exclude           http*://www.google.com/*
// @exclude           http*://note.sdo.com/*
// @require         https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
// @run-at            document-idle
// @grant             GM_addStyle
// @grant             GM_notification
// @grant             GM_registerMenuCommand
// @grant             unsafeWindow
// @namespace https://greasyfork.org/users/3128
// ==/UserScript==

function Linkify() {
    GM_addStyle('.newhref{border:2px dashed red!important;display:inline-block!important;padding:5px!important;margin:5px 0!important;}.newhref:hover{border-color:#E9aa2f!important;}');

    //const urlRegex = /\b(https?:\/\/[^\s+\"\'\<\>,]+|ed2k:\/\/\|file\|[^\|]+\|\d{9}\|\w{32}\|h=\w{32}\|\/)/ig;
    //const urlRegex = /\b(https?:\/\/[^\s+\"\\'<\>,]+|magnet:\?xt=urn:btih:\w{40}|ed2k:\/\/\|file\|[^\|]+\|\d+\|\w{32}\|(?:h=\w{32}\|)?\/)/ig;
    var urlRegex=new RegExp(//协议规则,正则模式,\需要转义\\
        '\\b('
        +
        'https?:\\/\\/[^\\s+"\\\'<>,\\[\\]()\:\\*\\u4E00-\\u9FFF]+' //https?:\/\/[^\s+"\\'<>,\[\]()\:\*\u4E00-\u9FFF]+
        +'|'+
        'https?:\\/\\/mega.nz\\/#!\w+![^\\s+"\'<>,\\]]+' //mega 专用规则
        +'|'+
        'ed2k:\\/\\/\\|file\\|[^\\|]+\\|\\d+\\|\\w{32}\\|(?:h=\\w{32}\\|)?\\/' //ed2k专用规则
        +'|'+
        'magnet:\\?xt=urn:btih:\\w{40}(&[\\w\\s]+)?' //magnet 专用规则
        +')','ig');

    //console.log(urlRegex);
    //ed2k://|file|mura-041616_381.mp4|1874185231|E7D60A7A854CF7AE0FF77AC306808760|h=SYGOH7KKTWHJNA4OBVAEOANW27FT5RJE|/

    // tags we will scan looking for un-hyperlinked urls
    var allowedParents = [	//允许处理的文本的父标签
        "body",
        "code","blockquote",
        "abbr", "acronym", "address", "applet", "b", "bdo", "big",
        "caption", "center", "cite",  "dd", "del", "div", "dfn", "dt", "em",
        "fieldset", "font", "form",  "i", "iframe",
        "ins", "kdb", "li", "object", "pre", "p", "q", "samp", "small", "span", "strike", "font",
        "s", "strong", "sub", "sup", "td", "th", "tt", "u", "var",
        "h1", "h2", "h3", "h4", "h5", "h6",
        'url','img'
    ];

    /*
    var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ") and " +
                "contains(translate(., 'HTTP', 'http'), 'http')]";
	*/
    var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ") and " +
        "(contains(translate(., 'HTTP', 'http'), 'http')"+" or " +
        "contains(translate(., 'MAGNET', 'magnet'), 'magnet')"+" or " +
        "contains(translate(., 'ED2K', 'ed2k'), 'ed2k'))]";
    //console.log(xpath);

    var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

    var t0 = new Date().getTime();
    for (var cand = null, i = 0; (cand = candidates.snapshotItem(i)); i++) {
        if (urlRegex.test(cand.nodeValue)) {
            var span = document.createElement("span");	//为超链接创建span标签作为容器
            var source = cand.nodeValue;
            //console.log(cand);

            cand.parentNode.replaceChild(span, cand);

            urlRegex.lastIndex = 0;
            for (var match = null, lastLastIndex = 0; (match = urlRegex.exec(source)); )
            {
                span.appendChild(document.createTextNode(source.substring(lastLastIndex, match.index)));
                //console.log(match);

                if(/\.(?:gif|jpg|png|bmp)(?:\?.+?)?$/.test(match[0])){//如果是图片链接,则创建有图片地址的连接
                    span.innerHTML='<img src="'+match[0]+'" class="newhref"/><br>';
                }

                var Mtxt=match[0];	//取得链接
                var Rtxt=/paco-\d{6}_\d{3}/i.test(Mtxt)?Mtxt.replace(/paco-(\d{6}_\d{3})/i,'pacopacomama $1'):Mtxt;	//替换链接内容

                var a = document.createElement("a");
                a.href=Rtxt;
                a.className='newhref';
                a.target="blank";
                a.appendChild(document.createTextNode(decodeURIComponent(Rtxt)));
                span.appendChild(a);

                lastLastIndex = urlRegex.lastIndex;
            }

            span.appendChild(document.createTextNode(source.substring(lastLastIndex)));
            span.normalize();
        }
    }
    var t1 = new Date().getTime();
    //alert((t1 - t0) / 1000);
}

function ViewIMG(){
    var oA=document.getElementsByTagName('a');
    for(var i in oA){
        console.log(oA[i].href);
        if(/\.(?:jpg|png|gif)$/i.test(oA[i].href)){
            oA[i].innerHTML+='<img src="'+oA[i].href+'">';
        }
    }
}

function ViewVideo(){
    $('a[href*=".mp4"], a[href*=".m3u8"]').each(function(){
        $('<video controls preload>').attr({'src':this.href}).insertAfter(this);
    });
}

var Mother={
    historyUrl:function(){
        window.history.pushState('state','', urls.replace(location.search,''));
    }
};


var Host=location.hostname.replace(/^www\./i,'').toLowerCase();
var paths=location.pathname;
var search=location.search;

if(localStorage['Linkify']){
    Linkify();
} else if(/(\/thread-\d+-)/i.test(paths)||/mod=viewthread/i.test(search)) {
    //DZ论坛监听
    Linkify();
    window.addEventListener('load', function(){
        addMutationObserver('#postlist', Linkify);
    });
}

function addMutationObserver(selector, callback, Kill) {
    var watch = document.querySelector(selector);

    if (!watch) return;
    var observer = new MutationObserver(function(mutations){
        var nodeAdded = mutations.some(function(x){ return x.addedNodes.length > 0;});
        if (nodeAdded) {
            callback(mutations);
        }
    });
    observer.observe(watch, {childList: true, subtree: true});
}
function setLinkify(){
    if(localStorage['Linkify']) {
        delete localStorage['Linkify'];
        dmsCLNotification('已关闭【自动】文本转超链接功能,\n请刷新页面。');
    } else {
        localStorage['Linkify']=true;
        dmsCLNotification('已打开【自动】文本转超链接功能');
        Linkify();
    }
}
var dmsCLNotification = function(text){
  GM_notification(text, 'Linkify·改', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAASyElEQVR4nO2de3AU1Z7HPz0BhgQkkSAQEcjdiQFCEp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M+oHDtXX1/fza6JGivEuPjfAK9LknTBtI5WDAAgy/JVwH+gcfSsk8PDGBf/NLBCkqQGALM6Wu6xt1XgDVTCjUCIkJaWFtaDmEeMGHEmxsU/C6xUxLeC5RZAQZblfsDTgOoBi7W1tb2mTZt2r6mXGeDDDz/0ZmRkWJqZjHAU8dt9qBxrARTaKrQCjQWIjIyMRp/PV1RQUBCSNGkPPvhgrc/nK4px8esQzb5tLaptLYBCW5/gCdrOptWiqKio/+LFi8e1tLSYNqHb7b74xhtvVNxzzz225jqMUI4Db2k1+451AjUq4wbmAqP0lOX1epPXrl2btmfPnkGB7nO73Rdvu+22k3PmzPmHXbn9o4QDwHuSJGnmIogoAwDIsuwC7gPuMVLu2rVrr6uuru579uzZnj///HOP5OTk31JSUprGjx//w9SpU1U7mjHONmCrJEkB07GE3QAGyAVmE+ajaWKAZsT0bkh3VIfDACCOp5+POLK8i+CcAtZgMX2fHsJlABBRRfcBE4HfSy5eo8jAXxFLumHZexlOAygMB2ahEV72O+YMsB44HM6XOmEAEK3BVOAuwHCqmRjjEmJl9UPC9Kn3xykDKPQH/h0Y6WQlHOQroBAxr+8IThtAIQPII8jkUQxxHChBbLxxlEgxgEI24qthqNMVCRHfAh8hduxExH98pBlAYRhwN5BJ9I8YZETCje2A4zkSOxKpBlC4BhgP3AyE7ByfEHEe2I1IuROxM5iRbgCFOGAEYlbxBiDB2epo0oSYt9+LyMaluqcykogWA/jTDUhHjBwy0YhECiN1iCb+K0QWDl0HVUQK0WiAjiQBnrafIQhDXBWidzUgBD8B+Np+6kP0rrAQCwZQIwEYgDDDAERfog/QG+iFmIjqBvRou/8C4pN7EWhEHKZ5HvHdfRoh+mkM5FKOFv4f4BVr5irhOlwAAAAASUVORK5CYII=');
}

GM_registerMenuCommand('开/关【自动】文本转超链接', setLinkify);
GM_registerMenuCommand('文本转链接', Linkify);
GM_registerMenuCommand('文本转链接并显示图片', ViewIMG);
GM_registerMenuCommand('文本转链接并显示视频', ViewVideo);
GM_registerMenuCommand('移除location.Search信息', Mother.historyUrl);