Clean URL Query

Trim tracking query params from URL

Verze ze dne 11. 01. 2019. Zobrazit nejnovější verzi.

  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. // @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*://*.carousell.com/p/*
  13. // @match http*://*.computerworld.com/*
  14. // @match http*://*.networkworld.com/*
  15. // @match http*://*.infoworld.com/*
  16. // @run-at document-start
  17. // @grant GM_log
  18. // @grant GM_info
  19. // @noframes
  20. // ==/UserScript==
  21.  
  22. function log(level, text) {
  23. GM_log(level + ": " + text);
  24. }
  25.  
  26. function query2json(querystring) {
  27. // remove any preceding url and split
  28. var queries = querystring.substring(querystring.indexOf("?") + 1).split("&");
  29. var params = {},
  30. pair,
  31. d = decodeURIComponent;
  32. // match and parse
  33. for (var i = queries.length - 1; i >= 0; i--) {
  34. if (queries[i].length === 0) continue;
  35. pair = queries[i].split("=");
  36. params[d(pair[0])] = d(pair[1]);
  37. }
  38.  
  39. return params;
  40. }
  41.  
  42. function json2query(json) {
  43. var query = Object.keys(json)
  44. .map(function(key) {
  45. return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
  46. })
  47. .join("&");
  48.  
  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", "queries: " + 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. log("debug", "copy: " + JSON.stringify(copy));
  100.  
  101. // remove non-whitelisted queries
  102. if (Object.keys(copy).length) {
  103. var _q = {};
  104. WHITE_LIST.forEach(key => {
  105. if (queries[key]) _q[key] = queries[key];
  106. });
  107. log("debug", "_q: " + JSON.stringify(_q));
  108. location.replace(location.pathname + json2query(_q));
  109. }
  110. })();