1/** 2 * Monkey-patch for "querySelectorAll". 3 * 4 * The goal is to temporarily replace the behaviour of "querySelectorAll". 5 * This global function is used inside the "www.googletagmanager.com/gtag" script to scan through all the 6 * elements on the page, and use a regex to find emails (its end goal is to find external emails). 7 * But since this is TOO COSTLY, we hijack the behaviour to just return the elements that are holding external email info. 8 * So we need to tag some relevant elements with a class "has-external-email". 9 * 10 * NOTE: We don't need to use it for all pages. We can only use it on the page where gtag is slow for some reasons. 11 * But if we do use it, we have to add the "has-external-email" classes in the right places. 12 */ 13(function () { 14 const originalQSA = Element.prototype.querySelectorAll; 15 let qsaPatched = false; 16 17 /* 18 * Delay in ms. 19 * Put something like 1500 or 2000 if you want to just hijack the call that would happen on page load. 20 * Or put -1 -> "no restoring", ie: the patch stays applied 21 */ 22 const DELAY_BEFORE_RESTORE = -1; 23 24 function patchQSA() { 25 if (qsaPatched) return; 26 qsaPatched = true; 27 28 Element.prototype.querySelectorAll = function (selector) { 29 // Detect Google's full-DOM scan 30 if (this === document.body && selector === "*") { 31 // Set for uniqueness 32 const elementsSet = new Set(); 33 34 // Grab only return the elements YOU want Google to see 35 // -> root element is marked with class has-external-email 36 const roots = originalQSA.call(document.body, ".has-external-email"); 37 roots.forEach(function (root) { 38 elementsSet.add(root); 39 40 // Include all descendants 41 root.querySelectorAll("*") 42 .forEach(function (descendant) { 43 elementsSet.add(descendant); 44 }); 45 }); 46 47 return Array.from(elementsSet); 48 } 49 return originalQSA.call(this, selector); 50 }; 51 } 52 53 function restoreQSA() { 54 if (!qsaPatched) return; 55 qsaPatched = false; 56 57 Element.prototype.querySelectorAll = originalQSA; 58 } 59 60 // Install the patch immediately 61 patchQSA(); 62 63 // Restore original behaviour after GTM/gtag email scanning is definitely done. 64 if (DELAY_BEFORE_RESTORE >= 0) { 65 window.addEventListener("load", () => { 66 setTimeout(() => { 67 restoreQSA(); 68 }, DELAY_BEFORE_RESTORE); 69 }); 70 } 71})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.