Greasy Fork

Greasy Fork is available in English.

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

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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.15
// @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.*$/,
        REDIRECT_QUERY_PARAM_Q = 'q',
        REDIRECT_QUERY_PARAM_URL = 'url'

    var WEBLIGHT_REGEXP = /^[^:]*:\/\/www.google.*\/url.*(?=q=)q=http:\/\/googleweblight.com\/%3Flite_url%3D.*/

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

    var NULL_RESULT = {matchedUrlPattern: null}

    function getUrlQueryParam (url, name) {
        return new URL(url).searchParams.get(name) || null
    }

    function baseCase (a) {
        return {
            a: a,
            matchedUrlPattern: URL_REGEXPS.filter(function(r) {
                var queryIndex = a.href.indexOf('?');
                var urlNoQuery = queryIndex === -1 ? a.href : a.href.slice(0, queryIndex)
                return r.test(urlNoQuery);
            }).pop() || null,
            url: a.href,
        }
    }

    function redirectionCase (redirectUrlQueryParamName, a) {
        try {
            var redirectUrl = getUrlQueryParam(a.href, redirectUrlQueryParamName) || '';
            var isRedirectUrl = REDIRECT_URL_REGEXP.test(a.href) && redirectUrl !== '' && !WEBLIGHT_REGEXP.test(a.href);
            
            return {
                a: a,
                url: redirectUrl,
                matchedUrlPattern: isRedirectUrl ? URL_REGEXPS.filter(function(r) {
                    return r.test(redirectUrl);
                }).pop() : null
            }
        } catch {
            return NULL_RESULT
        }
    }

    function weblightCase(a) {
        try {
            var isWebLightUrl = WEBLIGHT_REGEXP.test(a.href)
            var level1Url = isWebLightUrl ? getUrlQueryParam(a.href, 'q') : '';
            var level2Url = isWebLightUrl ? decodeURIComponent(getUrlQueryParam(level1Url, 'lite_url')) : '';
    
            return {
                a: a,
                url: level2Url,
                matchedUrlPattern: isWebLightUrl ? URL_REGEXPS.filter(function(r) {
                    return r.test(level2Url);
                }).pop() : null
            }
        } catch {
            return NULL_RESULT
        }
    }

    [   baseCase, 
        redirectionCase.bind(null, REDIRECT_QUERY_PARAM_Q),
        redirectionCase.bind(null, REDIRECT_QUERY_PARAM_URL),
        weblightCase
    ].forEach((function (fn) {
        [].slice.call(document.getElementsByTagName('a')).map(fn).forEach(function (res) {
            if (res.matchedUrlPattern) {
                res.a.href = res.url.replace(res.matchedUrlPattern, REPLACE_BY).replace(HTTP_PROC_REGEXP, HTTPS_PROC);
            }
        })
    }))
})();