点击变色

点击变色并在新窗口打开

目前為 2017-02-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         点击变色
// @version      0.33
// @description  点击变色并在新窗口打开
// @match        *://*/*
// @author       变异小僵尸
// @namespace https://greasyfork.org/users/85375
// ==/UserScript==
(function() {
    'use strict';
    //变色
    var color = "red";
    var style = 'a:visited{color:'+color+'}';
    addStyle(style);
    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);
        });
    }
    //创建style
    function addStyle(string) {
        var style = document.createElement("style");
        style.setAttribute("type", "text/css");
        if (style.styleSheet) { // IE
            style.styleSheet.cssText = string;
        } else { // w3c
            var cssText = document.createTextNode(string);
            style.appendChild(cssText);
        }
        var heads = document.querySelectorAll("head");
        if (heads.length){
          heads[0].appendChild(style);
        }
        else{
          document.documentElement.appendChild(style);
        }
    }
})();