百度谷歌必应链接缩短

将 Baidu、Google、Bing 搜索引擎的冗长链接缩短,变干净。

当前为 2023-02-11 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Baidu Google Bing URL Shorten
// @name:zh-CN   百度谷歌必应链接缩短
// @namespace    https://www.runningcheese.com
// @version      0.3
// @description         Mark Baidu、Google、Bing URL Shortest.
// @description:zh-CN   将 Baidu、Google、Bing 搜索引擎的冗长链接缩短,变干净。
// @author       RunningCheese
// @include      /^https?://www\.baidu\.com.*$/
// @include      /^https?://www\.google\.com.*$/
// @include      /^https?://www\.bing\.com.*$/
// @include      /^https?://www2\.bing\.com.*$/
// @include      /^https?://cn\.bing\.com.*$/
// @icon         https://t1.gstatic.cn/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://www.google.com
// @grant        none
// @license      MIT
// ==/UserScript==

// main function
(function() {
    'use strict';
    sturl();
    window.addEventListener('locationchange', function (){
        sturl();
    })
})();





// shorten url
function sturl() {
    // url
    var url = window.location.href;
    // new url
    var nurl = window.location.href;
    // query string need to be removed
    var qs = [
        //Baidu
        'rsp','prefixsug','fr','bsst','f','inputT','usm','rsv_page','rqlang','rsv_t','oq','rsv_pq','rsv_spt', 'ie', 'rsv_enter','rsv_sug1', 'rsv_sug7','rsv_sug2','rsv_sug3','rsv_iqid', 'rsv_bp', 'rsv_btype', 'rsv_idx', 'rsv_dl', 'issp', 'cshid', 'tn','rsv_sug4',
        //Google
        'tbas','ved', 'uact', 'ei', 'ie', 'oq', 'sclient', 'cshid', 'dpr','iflsig', 'aqs', 'gs_lcp', 'source', 'sourceid', 'sxsrf', 'pccc', 'sa', 'biw', 'bih', 'hl', 'newwindow',
        //Bing
        'tsc','sp','FORM','form','pq','sc','qs','sk','cvid'
    ];
    // query string need to be removed if equal to something
    var qseq = [['start', '0']];

    // remove not necessary query string
    nurl = rmqs(nurl, qs);
    // remove not necessary query string if equal to something
    nurl = rmqseq(nurl, qseq);

    // do nothing if new url is the same as url
    if (url == nurl){
        return false;
    }

    // update url in address bar to new url
    window.history.replaceState(null, null, nurl);

    // update url in address bar to new url(deprecated)
    //window.location.replace(nurl)
}

// remove not necessary query string
function rmqs(url, qs) {
    url = new URL(url);
    qs.forEach(function(i){
        url.searchParams.delete(i);
    });
    return url.toString();
}

// remove not necessary query string if equal to something
function rmqseq(url, qseq) {
    url = new URL(url);
    qseq.forEach(function(i){
        if (url.searchParams.get(i[0]) == i[1]){
            url.searchParams.delete(i[0]);
        }
    });
    return url.toString();
}

/*----force listen to locationchange work start----*/
history.pushState = ( f => function pushState(){
    var ret = f.apply(this, arguments);
    window.dispatchEvent(new Event('pushstate'));
    window.dispatchEvent(new Event('locationchange'));
    return ret;
})(history.pushState);

history.replaceState = ( f => function replaceState(){
    var ret = f.apply(this, arguments);
    window.dispatchEvent(new Event('replacestate'));
    window.dispatchEvent(new Event('locationchange'));
    return ret;
})(history.replaceState);

window.addEventListener('popstate',()=>{
    window.dispatchEvent(new Event('locationchange'))
});
/*----force listen to locationchange work end----*/