Greasy Fork

Greasy Fork is available in English.

Google谷歌搜索页面dq.tieba.com贴吧链接替换

最近访问dq.tieba.com老是出现 “您好,该页面正在维护中。” 于是就写了这个。为什么要在Google搜索页面替换呢?这是因为当你按dq.tieba.com时服务器就给你的浏览器发个HTTP 302重定向到维护页面,脚本都来不及执行

目前为 2019-10-03 提交的版本,查看 最新版本

// ==UserScript==
// @name                 Google谷歌搜索页面dq.tieba.com贴吧链接替换 dq.tieba.com to tieba.baidu.com for Google results
// @name:zh-CN           Google谷歌搜索页面dq.tieba.com贴吧链接替换
// @name:zh-TW           Google谷歌搜索頁面dq.tieba.com貼吧鏈接替換
// @version              0.14
// @include              *://www.google.*
// @description          最近访问dq.tieba.com老是出现 “您好,该页面正在维护中。” 于是就写了这个。为什么要在Google搜索页面替换呢?这是因为当你按dq.tieba.com时服务器就给你的浏览器发个HTTP 302重定向到维护页面,脚本都来不及执行
// @description:zh-CN    最近访问dq.tieba.com老是出现 “您好,该页面正在维护中。” 于是就写了这个。为什么要在Google搜索页面替换呢?这是因为当你按dq.tieba.com时服务器就给你的浏览器发个HTTP 302重定向到维护页面,脚本都来不及执行
// @description:zh-TW    最近訪問dq.tieba.com老是出現 “您好,该页面正在维护中。” 於是就寫了這個。為什麼要在Google搜索頁面替換呢?這是因為當你按dq.tieba.com時服務器就給你的瀏覽器發個HTTP 302轉址到維護頁面,脚本都來不及執行
// @author               Jiaxing Peng
// @namespace            bjxpen
// @grant                none
// ==/UserScript==

(function() {
    'use strict';

    var URL_PATTERNS = [
        'jump*.bdimg.com',      // matches jump.bdimg.com, jump2.bdimg.com, ...
        'wapp.baidu.*/mo/q',    // matches wapp.baidu.com/mo/q, wapp.baidu.cn/mo/q, ...
        'wefan.baidu.*',
        'c.tieba.baidu.*',
        'tieba.baidu.*',
        'dq.tieba.*',
        'post.baidu.*',
        'xingqu.baidu.*',
        'www.tieba.*'
    ];

    var REPLACE_BY = 'tieba.baidu.com';

    var URL_REGEXPS = URL_PATTERNS.map(function(p) {
        return new RegExp(p.replace(/[\/.]/g, '\\$&').replace(/\*/g, '[^.\\/]*'));
    });

    var REDIRECT_URL_REGEXP = /^([^:]*:\/\/www.google.*\/url.*(?=url=)url=)([^:]+:\/\/)([^&\/]+)(\/[^&]+)?(.*)$/,
        PROC_INDEX = 2,
        HOST_INDEX = 3,
        PATH_INDEX = 4,
        TRAILING_INDEX = 5;

    var HTTP_PROC_REGEXP = /^http:\/\//, 
        HTTPS_PROC = 'https://';

    [].slice.call(document.getElementsByTagName('a')).forEach(function(a) {
        var matchedUrlPattern = URL_REGEXPS.filter(function(r) {
            return r.test(a.href);
        }).pop() || null;

        var url = a.href;

        var redirectUrlRegExpResults = REDIRECT_URL_REGEXP.exec(a.href);

        var isRedirectUrl = redirectUrlRegExpResults && redirectUrlRegExpResults[HOST_INDEX];

        var redirectUrl = isRedirectUrl ? decodeURIComponent(
            redirectUrlRegExpResults.slice(
                PROC_INDEX,
                redirectUrlRegExpResults[TRAILING_INDEX] ? TRAILING_INDEX : PATH_INDEX
            ).join('')
        ) : '';

        var matchedRedirectUrlPattern = isRedirectUrl ? URL_REGEXPS.filter(function(r) {
            return r.test(redirectUrl);
        }).pop() : null;

        url = redirectUrl || url;
        matchedUrlPattern = matchedRedirectUrlPattern || matchedUrlPattern;

        if (matchedUrlPattern) {
            a.href = url.replace(matchedUrlPattern, REPLACE_BY).replace(HTTP_PROC_REGEXP, HTTPS_PROC);
        }
    })
})();