-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbase.js
More file actions
81 lines (71 loc) · 2.85 KB
/
base.js
File metadata and controls
81 lines (71 loc) · 2.85 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Base.js, included on all pages
* Blocks some third party scripts and sets all dynamically added scripts to use defer
*/
// Third party injected scripts that are not wanted
const blockedScripts = [
// /hs-banner.com/ HubSpot cookie banner that's loaded even if not used
]
function needsToBeBlocked(src) {
return blockedScripts.some((blockedScript) => blockedScript.test(src))
}
// Patch document.createElement to allow blocking of unwanted scripts injected externally
const createElementBackup = document.createElement
document.createElement = function(...args) {
// If this is not a script tag, bypass
if(args[0].toLowerCase() !== 'script') {
// Binding to document is essential
return createElementBackup.bind(document)(...args)
}
const scriptElt = createElementBackup.bind(document)(...args)
const originalSetAttribute = scriptElt.setAttribute.bind(scriptElt)
// Always set the defer flag
originalSetAttribute('defer', 'defer')
// Define getters / setters to ensure that the script type is properly set
Object.defineProperties(scriptElt, {
'src': {
get() {
return scriptElt.getAttribute('src')
},
set(value) {
if(needsToBeBlocked(value, scriptElt.type)) {
originalSetAttribute('type', 'javascript/blocked')
}
originalSetAttribute('src', value)
return true
}
},
'type': {
set(value) {
const typeValue =
needsToBeBlocked(scriptElt.src, scriptElt.type) ?
'javascript/blocked' :
value
originalSetAttribute('type', typeValue)
return true
}
}
})
// Monkey patch the setAttribute function so that the setter is called instead.
scriptElt.setAttribute = function(name, value) {
if(name === 'type' || name === 'src')
scriptElt[name] = value
else
HTMLScriptElement.prototype.setAttribute.call(scriptElt, name, value)
}
return scriptElt
}
function hsFallback (element) {
if (element && element.parentNode) {
const errorSection = document.createElement('section');
errorSection.classList.add('text-center', 'border', 'border-indigo-300', 'rounded-lg', 'bg-white', 'px-4');
errorSection.innerHTML = `
<p style="color: #6366f1;"><strong>Hmm… there was supposed to be a form here.</strong></p>
<p>
If you’re using strict privacy settings or navigating in private mode, it might be blocked.
Try adjusting your settings or switching browsers to continue.
</p>
`;
element.parentNode.insertBefore(errorSection, element.nextSibling);
}
}