Greasy Fork

来自缓存

Greasy Fork is available in English.

点击变色

点击变色并在新窗口打开

当前为 2017-02-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         点击变色
// @version      0.21
// @description  点击变色并在新窗口打开
// @match        *://*/*
// @author       变异小僵尸
// @namespace http://greasyfork.icu/users/85375
// ==/UserScript==
(function() {
    'use strict';
    //变色
    var color = "red";
    var styles = '';
    //获取所有a标签
    var a = document.querySelectorAll('a');
    for (var i = 0; i < a.length; i++) {
        a[i].addEventListener('mousedown', function(e) {
            // e.preventDefault()
            var that = this;
            that.addEventListener('click', function(e) {
                // 判定a标签链接
                if (that.getAttribute('href') == "#" || that.getAttribute('href') == "javascript;;" || that.getAttribute('href') == "javascript:void(0);" || that.getAttribute('href') == "javascript" || that.getAttribute('href') == "javascript:void(0)") {
                    window.location.href = that.getAttribute('href');
                } else {
                    // 阻止默认点击
                    e.preventDefault()
                    // 再新窗口打开链接
                    window.open(that.getAttribute('href'));
                }
            })
            styles = that.getAttribute('style');
            if (styles != null) {
                styles += ';color:' + color + ';';
            } else {
                styles = 'color:' + color + ';';
            }
            //添加
            that.setAttribute('style', styles);
        })
    }
    //添加颜色
})();