Clean URL Query

Trim tracking query params from URL

As of 2018-10-26. See the latest version.

  1. // ==UserScript==
  2. // @name Clean URL Query
  3. // @namespace https://github.com/leesei/userscripts
  4. // @version 1.3
  5. // @description Trim tracking query params from URL
  6. // @author leesei@gmail.com
  7. // @supportURL https://github.com/leesei/userscripts/issues
  8. // @match http*://detail.tmall.com/item.htm*
  9. // @match http*://*.tmall.com/shop/view_shop.htm
  10. // @match http*://item.taobao.com/item.htm*
  11. // @match http*://*.taobao.com/shop/view_shop.htm
  12. // @match http*://*.computerworld.com/*
  13. // @match http*://*.networkworld.com/*
  14. // @match http*://*.infoworld.com/*
  15. // @run-at document-start
  16. // @grant GM_log
  17. // @grant GM_info
  18. // @noframes
  19. // ==/UserScript==
  20.  
  21. function log(level, text) {
  22. GM_log(level + ": " + text);
  23. }
  24.  
  25. function query2json(querystring) {
  26. // remove any preceding url and split
  27. var queries = querystring.substring(querystring.indexOf("?") + 1).split("&");
  28. var params = {},
  29. pair,
  30. d = decodeURIComponent;
  31. // match and parse
  32. for (var i = queries.length - 1; i >= 0; i--) {
  33. if (queries[i].length === 0) continue;
  34. pair = queries[i].split("=");
  35. params[d(pair[0])] = d(pair[1]);
  36. }
  37.  
  38. return params;
  39. }
  40.  
  41. function json2query(json) {
  42. var query = Object.keys(json)
  43. .map(function(key) {
  44. return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
  45. })
  46. .join("&");
  47.  
  48. // log("debug", "output query: " + query);
  49. return query ? "?" + query : "";
  50. }
  51.  
  52. // Convert sensible strings to Boolean, useful for parsing URL queries
  53. function string2Boolean(string, defaultTrue) {
  54. // console.log('2bool:', String(string).toLowerCase());
  55. switch (String(string).toLowerCase()) {
  56. case "":
  57. return defaultTrue === undefined ? false : defaultTrue;
  58. case "true":
  59. case "1":
  60. case "yes":
  61. case "y":
  62. return true;
  63. case "false":
  64. case "0":
  65. case "no":
  66. case "n":
  67. return false;
  68. default:
  69. // you could throw an error, but 'undefined' seems a more logical value
  70. return undefined;
  71. }
  72. }
  73.  
  74. (function() {
  75. "use strict";
  76.  
  77. log(
  78. "info",
  79. ">>> [" + GM_info.script.namespace + "] " + GM_info.script.name + " <<<"
  80. );
  81.  
  82. var queries = query2json(location.search);
  83. log("debug", "query: " + JSON.stringify(queries));
  84.  
  85. if (
  86. queries._skip_clean !== undefined &&
  87. string2Boolean(queries._skip_clean)
  88. ) {
  89. return;
  90. }
  91.  
  92. // retain these query params
  93. var WHITE_LIST = [
  94. "id", // for taobao items
  95. "page" // for article pages
  96. ];
  97. var copy = Object.assign(queries);
  98. WHITE_LIST.forEach(key => delete copy[key]);
  99.  
  100. // remove non-whitelisted queries
  101. if (Object.keys(copy).length) {
  102. var _q = {};
  103. WHITE_LIST.forEach(key => (_q[key] = queries[key]));
  104. location.replace(location.pathname + json2query(_q));
  105. }
  106. })();