Facebook - remove tracking url

Remove the tracking parameter in links

  1. // ==UserScript==
  2. // @name Facebook - remove tracking url
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.1
  5. // @description Remove the tracking parameter in links
  6. // @author Achernar
  7. // @match https://*.facebook.com/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14. var t;
  15.  
  16. function fix(u) {
  17. return u.replace('?', '?&').replace(/&(__cft__\[0\]|__tn__)=[^&#]*/g, '').replace('?&', '?').replace(/\?$/, '');
  18. }
  19.  
  20. if (t=location.search) {
  21. let s=fix(t);
  22. if (s!=t) history.replaceState(null, null, (s || location.pathname)+location.hash);
  23. }
  24.  
  25. function fixL(L) {
  26. let i=0, a, j=0;
  27. for (;a=L[j];j++) {
  28. if (a.LnkFixed || !a.href) continue;
  29. if (a.protocol && a.protocol.startsWith('http') && a.host.includes('facebook.com') && a.search) {
  30. let s=fix(a.search);
  31. if (a.search != s) a.search=s;
  32. }
  33. a.LnkFixed=1;
  34. }
  35. }
  36.  
  37. function init() {
  38. new MutationObserver(function(mutL){
  39. for (let m of mutL) {
  40. if (m.addedNodes) {
  41. let e=m.target.querySelectorAll(':scope a');
  42. if (e.length) fixL(e);
  43. }
  44. }
  45. }).observe(document, {childList:true, subtree:true});
  46.  
  47. fixL(document.links);
  48. }
  49.  
  50. window.addEventListener('load', function(){fixL(document.links)});
  51. if (document.readyState != 'loading') init();
  52. else document.addEventListener('DOMContentLoaded', init);
  53.  
  54. })();