Clean URL Query

Trim tracking query params from URL

目前为 2018-10-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         Clean URL Query
// @namespace    https://github.com/leesei/userscripts
// @version      1.2
// @description  Trim tracking query params from URL
// @author       [email protected]
// @supportURL   https://github.com/leesei/userscripts/issues
// @match        http*://detail.tmall.com/item.htm*
// @match        http*://*.tmall.com/shop/view_shop.htm
// @match        http*://item.taobao.com/item.htm*
// @match        http*://*.taobao.com/shop/view_shop.htm
// @match        http*://*.computerworld.com/*
// @match        http*://*.networkworld.com/*
// @match        http*://*.infoworld.com/*
// @run-at       document-start
// @grant        GM_log
// @grant        GM_info
// @noframes
// ==/UserScript==

function log(level, text) {
  GM_log(level + ": " + text);
}

function query2json(querystring) {
  // remove any preceding url and split
  var queries = querystring.substring(querystring.indexOf("?") + 1).split("&");
  var params = {},
    pair,
    d = decodeURIComponent;
  // match and parse
  for (var i = queries.length - 1; i >= 0; i--) {
    if (queries[i].length === 0) continue;
    pair = queries[i].split("=");
    params[d(pair[0])] = d(pair[1]);
  }

  return params;
}

function json2query(json) {
  var query = Object.keys(json)
    .map(function(key) {
      return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
    })
    .join("&");

  log("debug", "output query: " + query);
  return query ? "?" + query : "";
}

// Convert sensible strings to Boolean, useful for parsing URL queries
function string2Boolean(string, defaultTrue) {
  // console.log('2bool:', String(string).toLowerCase());
  switch (String(string).toLowerCase()) {
    case "":
      return defaultTrue === undefined ? false : defaultTrue;
    case "true":
    case "1":
    case "yes":
    case "y":
      return true;
    case "false":
    case "0":
    case "no":
    case "n":
      return false;
    default:
      // you could throw an error, but 'undefined' seems a more logical value
      return undefined;
  }
}

(function() {
  "use strict";

  log(
    "info",
    ">>> [" + GM_info.script.namespace + "] " + GM_info.script.name + " <<<"
  );

  var queries = query2json(location.search);
  log("debug", "query: " + JSON.stringify(queries));

  if (Object.keys(queries).length) {
    if (
      queries._skip_clean !== undefined &&
      string2Boolean(queries._skip_clean)
    ) {
      return;
    }

    var _q = {};
    // retain these query params
    if (queries.id) _q.id = queries.id; // for taobao items
    if (queries.page) _q.page = queries.page; // for article pages
    log("debug", "filtered query: " + JSON.stringify(_q));
    location.replace(location.pathname + json2query(_q));
  }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址