blob: 3edb0c892adacecf2eea0bda2901468a80534ab2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
window.isURL = function(url) {
// ensure the URL starts with HTTP or HTTPS; otherwise we might be vulnerable
// to XSS attacks
return (url && (url.startsWith("https://") || url.startsWith("http://")));
};
/**
* Search a window and all IFrames within it for a query selector, then return a
* list of all the elements in any frame that match.
*/
window.findInAllFrames = function(query) {
let out = [];
document.querySelectorAll(query).forEach((node) => {
out.push(node);
});
Array.from(document.getElementsByTagName('iframe')).forEach((iframe) => {
try {
iframe.contentDocument.querySelectorAll(query).forEach((node) => {
out.push(node);
});
} catch (e) {
// pass on cross origin iframe errors
}
});
return out;
};
/**
* Listen for mutations on a page. If any nodes that match `selector` are added
* to the page, execute the function `callback` on them.
* Used by first-party scripts to listen for new links being added to the page
* and strip them of tracking code immediately.
*/
window.observeMutations = function(selector, callback) {
// Check all new nodes added by a mutation for tracking links and unwrap them
function onMutation(mutation) {
if (!mutation.addedNodes.length) {
return;
}
for (let node of mutation.addedNodes) {
// Only act on element nodes, otherwise querySelectorAll won't work
if (node.nodeType != Node.ELEMENT_NODE) {
continue;
}
// check all child nodes against the selector first
node.querySelectorAll(selector).forEach((element) => {
callback(element);
});
// then check the node itself
if (node.matches(selector)) {
callback(node);
}
}
}
// Set up a mutation observer with the constructed callback
new MutationObserver(function(mutations) {
mutations.forEach(onMutation);
}).observe(document, {childList: true, subtree: true, attributes: false, characterData: false});
};
|