xiefucai / 下载链接高亮

// ==UserScript==
// @name         下载链接高亮
// @namespace    xiefucai
// @version      1.0
// @description  高亮可下载链接、去网页广告
// @author       xiefucai
// @license      MIT
// @homepageURL  https://github.com/xiefucai
// @include      *
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var inited = false;
var domain = document.domain;
    var regstr = "((thunder|magnet|ed2k|ftp):[^'\"]+|[^'\"]+\.(mp4|mkv|mov|avi|rmvb|torrent)(\\?.*)?)";
    var urlReg = new RegExp("^" + regstr + "$","gi");
    var isWhite = /\b(baidu|xunlei|bing|sogou|so|google|yahoo)\b\.(com|cn|net)(\.[a-z]+)?$/.test(domain);
    var _store = {};
    var layerStyle = 'position:fixed;bottom:0;left:0;background: rgba(216, 210, 218, 0.74);z-index: 9999;padding: 0.3em 1em;border-radius: 0 5px 0 0;color: #9e9696;font-size: 12px;';
    var storage = window.localStorage || {
        getItem: function(n) {
            return _store[n];
        },
        setItem: function(n, s) {
            _store[n] = s;
        },
        removeItem: function(n) {
            delete _store[n];
        }
    };
    var getStyle = function(e, s) {
        return window.getComputedStyle(e, null).getPropertyValue(s);
    };
    var isFloat = function(e) {
        return /^(absolute|fixed)$/.test(getStyle(e, 'position'));
    };
    var isTrans = function(e) {
        return getStyle(e, 'opacity') === 0;
    };
    var isOutSite = function(url) {
        var rdm = getDomain(url, true);
        return rdm && (domain.indexOf(rdm) < 0) && ['pan.baidu.com'].indexOf(getDomain(url)) < 0;
    };
    var hideElement = function(a) {
        !a.nohide && (a.style.display = 'none')
    };
    var getDomain = function(url, isRoot) {
        var m = /^https?:/.test(url) && url.match(/^https?:\/\/([\w\-\.]+)/)[1] || null;
        if (isRoot) {
            return m && m.match(/([\w\-]+\.\w+)$/)[1];
        }
        return m;
    };
    var hasOutSiteLink = function(e) {
        var links;
        urlReg.lastIndex = 0;
        if (e.tagName === "A" && isOutSite(e.href) && !urlReg.test(e.href)) {
            return true;
        }
        links = [].slice.call(e.getElementsByTagName('a'));
        for (var i = 0, k = links.length; i < k; i++) {
            if (isOutSite(links[i].href)) {
                return true;
            }
        }
        return /(广告|adv|window\.open|cpc163\.com|<object|<embed)/.test(e.outerHTML);
    };
    /*
       var showDownloadViewer = function(a,o) {
         var e = document.createElement('div');
         var s = '';
         var t = document.documentElement.scrollTop;
         a.offsetParent.appendChild(e);
         for(var n in o) {
             s += '<dt style="margin:0;padding:0;">'+n+'=</dt>';
             s += '<dd style="margin:0;padding:0;"><a href="'+o[n]+'" style="font-size:0.5em;color: red;background: #fff94a;">'+o[n]+'</a></dd>';
         }
         e.setAttribute('style','position:relative;background-color: #fff;color: black;font-size:2em;padding: 1em;white-space: pre-line;word-break: break-all;box-sizing: border-box;border: 1px solid #ccc;')
         e.innerHTML='<a href="javascript:;" onclick="var e=this.parentNode;e.targetLink.isshow=false;e.parentNode.removeChild(e);return false;" style="color: #fff;position:absolute;top: 0;right: 0;width: 2em;padding: 0.5em 1em;background: #607D8B;">'+unescape('%u5173%u95ED')+'</a><dl style="margin:0;padding:0;">'+s+'<dl>';
         e.targetLink = a;
         a.isshow=true;
       };
       var onClickLink = function(e) {
         var a = e.target;
         var attrs = a.attributes;
         var attr;
         var name;
         var value;
         var o={};
         e.preventDefault();
         for(var k=attrs.length,i=k-1;i>=0;i--){
           attr=attrs[i];
           name = attr.name;
           value = attr.value;
           if (urlReg.test(value)) {
               o[name] = value;
           }
         }
         if (urlReg.test(a.innerText || a.textContent)) {
             o.text = a.innerText || a.textContent;
         }
         !a.isshow && showDownloadViewer(a,o);
       };*/
    var checkLink = function(a, i) {
        var attrs = a.attributes;
        var href = a.href;
        var thref;
        var doit = function(o, src) {
            o.href = src;
            o.style.color = 'red';
            o.style.backgroundColor = 'yellow';
            o.style.fontSize = '2em';
            o.style.lineHeight = '1.25';
            o.removeAttribute('onclick');
            o.removeAttribute('oncontextmenu');
            o.removeAttribute('ontouchstart');
            o.removeAttribute('ontouchend');
            [].slice.call(o.getElementsByTagName('font')).forEach(function(oo) {
                oo.removeAttribute('style');
                oo.removeAttribute('color');
            });
        };
        if (a.target === '_blank') {
            a.target = '_self';
        }

        for (var k = attrs.length, ii = k - 1, attr, attrname, attrvalue; i >= 0; i--) {
            attr = a.attributes[ii];
            attrname = attr.name;
            attrvalue = attr.value;
            urlReg.lastIndex = 0;
            if (urlReg.test(attrvalue)) {
                thref = attrvalue;
                //a.addEventListener('click',onClickLink,false);
                break;
            }
        }
        urlReg.lastIndex = 0;
        if (thref) {
            doit(a, thref);
        } else if (urlReg.test(href)) {
            doit(a, href);
        }
    };
    var removeAllFloatElems = function() {
        var es = [];
        [].slice.call(document.body.getElementsByTagName("*")).forEach(function(e, i) {
            if ((isFloat(e) && hasOutSiteLink(e)) || isTrans(e)) {
                hideElement(e);
            }
        });
    };
    var removeAllAdvLinks = function() {
        [].slice.call(document.links).forEach(function(e) {
            if (hasOutSiteLink(e) || /\b(xitao3)\b\.com/.test(e.href)) {
                e.parentNode.removeChild(e);
            }
        });
    };
    var removeFramesOfoutsite = function() {
        [].slice.call(document.getElementsByTagName('iframe')).forEach(function(e, i) {
            if (isOutSite(e.src)) {
                hideElement(e);
            } else if (e.src === 'about:blank' && /^BAIDU_SSP/.test(e.getAttribute('onload'))) {
                hideElement(e);
            }
        });
    };
    var showDisableBtn = function() {
        var e = document.createElement('a');
        document.body.appendChild(e);
        e.innerHTML = unescape('%u5E7F%u544A%u5DF2%u9690%u85CF');
        e.setAttribute('style', layerStyle);
        e.addEventListener('click', function() {
            storage.setItem('disable-blockadv', 1);
            location.reload();
            /*
            if (window.confirm(unescape('%u4E0D%u9690%u85CF%u672C%u7AD9%u5E7F%u544A?'))) {
                storage.setItem('disable-blockadv', 1);
                location.reload();
            }*/
        }, false);
        e.nohide = true;
        inited = true;
    };
    var showEnableBtn = function() {
        var e = document.createElement('a');
        document.body.appendChild(e);
        e.innerHTML = unescape('%u4E0D%u9690%u85CF%u672C%u7AD9%u5E7F%u544A');
        e.setAttribute('style', layerStyle);
        e.addEventListener('click', function() {
            storage.removeItem('disable-blockadv', 1);
            location.reload();
        }, false);
        e.nohide = true;
        inited = true;
    };
    var init = function() {
        document.body && document.body.addEventListener("mouseup", function(event) {
            event.stopPropagation();
        }, true);
        [].slice.call(document.getElementsByTagName('base')).forEach(function(elem, i) {
            if (elem.target === '_blank') {
                elem.target = '_self';
            }
        });
        if (!storage.getItem("disable-blockadv")) {
            !isWhite && removeAllAdvLinks();
            !isWhite && removeAllFloatElems();
            !isWhite && removeFramesOfoutsite();
            [].slice.call(document.links).forEach(checkLink);
            !inited && showDisableBtn();
        } else {
            !inited && showEnableBtn();
        }
        if (getDomain(location.href, true) === 'txzqw.cc') {
            var srcReg = new RegExp(regstr,'gi');
            try {
                [].slice.call(document.scripts).forEach(function(s, i) {
                    if (!s.src) {
                        var urls = s.textContent.match(srcReg);
                        if (urls) {
                            urls.forEach(function(url) {
                                var div = document.createElement('DIV');
                                var a = document.createElement('A');
                                a.href = url;
                                document.body.appendChild(div);
                                div.appendChild(a);
                                a.textContent = url;
                            });
                        }
                    }
                });
            } catch (e) {
            }

        }

        setTimeout(init, 1000);
    };
    //alert(location.href);
    try {
        init();
    } catch (e) {
        //console.error(e);
    }
    // Your code here...
})();