rBlock

Removes redirects of some sites, such as google, zhihu...

目前為 2016-06-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         rBlock
// @description  Removes redirects of some sites, such as google, zhihu...
// @namespace    https://greasyfork.org/zh-CN/scripts/20524-redirect-blocker
// @author       nonoroazoro
// @include      /^https?\:\/\/(www|encrypted|news)\.google\./
// @version      1.0
// @grant        none
// ==/UserScript==

if (typeof unsafeWindow == "undefined")
{
    unsafeWindow = window;
}

// block redirects of google
function _blockGoogle()
{
    // 1. all/videos/news/apps
    let elems = document.querySelectorAll(`a[onmousedown^="return rwt("]`);
    for (let e of elems)
    {
        e.removeAttribute("onmousedown");
    }

    // 2. images
    elems = document.querySelectorAll(`#irc_cc a[class^="irc_"], #irc_cc a[class*=" irc_"]`);
    for (let e of elems)
    {
        _removeListeners(e, "mousedown");
    }

    // 3. news
    elems = document.querySelectorAll(`a.article`);
    for (let e of elems)
    {
        _removeListeners(e, "click mousedown");
    }

    // 4. general
    elems = document.querySelectorAll(`a[href^="/url?"]`);
    const regex = /(?:\/url\?.*url\=)(http.*?)$/i;
    for (let e of elems)
    {
        // reveal url
        const m = e.href.match(regex);
        if (m && m[1])
        {
            e.href = decodeURIComponent(m[1]);
        }

        // open in new tab
        if (!e.target)
        {
            e.target = "_blank";
        }
    }

    // 5. expose cached links
    let menuLink;
    let cacheLink;
    elems = document.querySelectorAll(`a[href^="http://webcache.googleusercontent."], a[href^="https://webcache.googleusercontent."]`);
    for (let e of elems)
    {
        e.style.display = "inline";
        menuLink = e.closest("div.action-menu.ab_ctl");

        cacheLink = document.createElement("a");
        cacheLink.href = e.href.replace(/^http\:/, "https:");
        cacheLink.target = "_blank";
        cacheLink.innerText = " Cached ";

        menuLink.parentNode.insertBefore(cacheLink, menuLink);
    }
}

function _removeListeners(p_element, p_events)
{
    if (p_element instanceof EventTarget && typeof p_events === "string")
    {
        const eventList = p_events.split(/\W+/) || [];
        for (let event of eventList)
        {
            p_element.removeEventListener(event, _preventDefaultAction, true);
            p_element.addEventListener(event, _preventDefaultAction, true);
        }
    }
}

function _preventDefaultAction(e)
{
    e.stopPropagation();
}

// start blocker
unsafeWindow.addEventListener("load", () =>
{
    _blockGoogle();

    const observer = new unsafeWindow.MutationObserver((p_mutations) =>
    {
        _blockGoogle();
    });

    observer.observe(document.body,
        {
            childList: true,
            subtree: true
        }
    );
});