Clean URL Query

Trim tracking query params from URL

Fra 08.12.2023. Se den seneste versjonen.

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