Advertisement
teknoraver

Untitled

Apr 17th, 2020
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var args = [
  2.     {
  3.         domain: /^(.*\.)?facebook\.com$/,
  4.         args: ['__tn__', '__xts__[0]']
  5.     }, {
  6.         domain: /^(.*\.)?instagram\.com$/,
  7.         args: ['igshid']
  8.     }, {
  9.         domain: /^(.*\.)?youtube\.com$/,
  10.         args: ['feature']
  11.     }, {
  12.         domain: /^(.*\.)?repubblica\.it$/,
  13.         args: ['ref']
  14.     }, {
  15.         domain: /./,
  16.         args: ['fbclid', 'gclid',
  17.             'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',
  18.             'refresh_ce']
  19.     }
  20. ];
  21.  
  22. function sanitizeUrl(node, url, list) {
  23.     var changed;
  24.  
  25.     for (var i = 0; i < list.length; i++) {
  26.         if (url.searchParams.has(list[i])) {
  27.             changed = true;
  28.             url.searchParams.delete(list[i]);
  29.         }
  30.     }
  31.  
  32.     if (changed)
  33.         node.value = url.toString();
  34. }
  35.  
  36. function sanitizeNode(node) {
  37.     if (node.tagName != 'A')
  38.         return;
  39.  
  40.     var href = node.attributes['href'];
  41.     if (!href)
  42.         return;
  43.  
  44.     href = href.value;
  45.     url = new URL(href, location.origin);
  46.  
  47.     if (url.protocol != 'http:' && url.protocol != 'https:')
  48.         return;
  49.  
  50.     for (var i = 0; i < args.length; i++)
  51.         if (url.hostname.match(args[i].domain))
  52.             sanitizeUrl(node.attributes['href'], url, args[i].args);
  53. }
  54.  
  55. function walk(node) {
  56.     // thanks to: http://is.gd/mwZp7E
  57.     switch (node.nodeType) {
  58.         case  1: // Node.ELEMENT_NODE
  59.             sanitizeNode(node);
  60.         case  9: // Node.DOCUMENT_NODE
  61.         case 11: // Node.DOCUMENT_FRAGMENT_NODE
  62.             var child = node.firstChild;
  63.             while (child) {
  64.                 var next = child.nextSibling;
  65.                 walk(child);
  66.                 child = next;
  67.             }
  68.             break;
  69.     }
  70. }
  71.  
  72. // register a MutationObserver to handle DOM edits by client-side JS.
  73. new MutationObserver(function (mlist) {
  74.     for (var i = 0; i < mlist.length; i++)
  75.         walk(mlist[i].target);
  76. }).observe(document, {
  77.     childList: true,
  78.     subtree: true,
  79.     attributes: true,
  80.     attributeFilter: ['href'],
  81. })
  82.  
  83. walk(document.body);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement