|
98 | 98 | // Mozilla has already parsed the <head> element, we must take extra steps... |
99 | 99 |
|
100 | 100 | debug("Early parsing: preemptively suppressing events and script execution."); |
| 101 | + |
101 | 102 | { |
102 | 103 | let eventTypes = []; |
103 | 104 | for (let p in document.documentElement) if (p.startsWith("on")) eventTypes.push(p.substring(2)); |
104 | 105 | let eventSuppressor = e => { |
105 | 106 | if (!ns.canScript) { |
106 | | - e.preventDefault(); |
107 | 107 | e.stopImmediatePropagation(); |
108 | | - e.stopPropagation(); |
109 | | - if (e.type === "load") debug(`Suppressing ${e.type} on `, e.target); |
110 | | - } else { |
111 | | - debug("Stopping suppression"); |
112 | | - for (let et of eventTypes) document.removeEventListener(et, eventSuppressor, true); |
| 108 | + debug(`Suppressing ${e.type} on `, e.target); // DEV_ONLY |
113 | 109 | } |
114 | 110 | } |
| 111 | + debug("Starting event suppression"); |
115 | 112 | for (let et of eventTypes) document.addEventListener(et, eventSuppressor, true); |
| 113 | + |
| 114 | + ns.on("capabilities", () => { |
| 115 | + if (!ns.canScript) { |
| 116 | + try { |
| 117 | + for (node of document.querySelectorAll("*")) { |
| 118 | + let evAttrs = [...node.attributes].filter(a => a.name.toLowerCase().startsWith("on")); |
| 119 | + for (let a of evAttrs) { |
| 120 | + debug("Reparsing event attribute after CSP", a, node); |
| 121 | + node.removeAttributeNode(a); |
| 122 | + node.setAttributeNodeNS(a); |
| 123 | + } |
| 124 | + } |
| 125 | + } catch (e) { |
| 126 | + error(e); |
| 127 | + } |
| 128 | + } |
| 129 | + debug("Stopping event suppression"); |
| 130 | + for (let et of eventTypes) document.removeEventListener(et, eventSuppressor, true); |
| 131 | + }); |
116 | 132 | } |
117 | 133 |
|
118 | 134 | addEventListener("beforescriptexecute", e => { |
|
125 | 141 | return; |
126 | 142 | } |
127 | 143 | let replacement = document.createRange().createContextualFragment(s.outerHTML); |
128 | | - replacement._original = e.target; |
| 144 | + replacement._original = s; |
| 145 | + s._replaced = true; |
129 | 146 | earlyScripts.push(replacement); |
130 | 147 | e.preventDefault(); |
131 | 148 | dequeueEarlyScripts(true); |
|
0 commit comments