Skip to content

Commit 762b012

Browse files
committed
Work-around for conflict with extensions inserting elements into content pages' DOM early.
1 parent 7af5194 commit 762b012

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/lib/SyncMessage.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,17 @@
195195
let uuid = () => (Math.random() * Date.now()).toString(16);
196196
let docUrl = document.URL;
197197
browser.runtime.sendSyncMessage = (msg, callback) => {
198+
// we interrogate the canScript() callback to know whether the caller
199+
// wants scripts deferred by sendSyncMessage to be eventually executed:
200+
// - undefined -> too soon to tell, suspend
201+
// - true -> go on and execute
202+
// - false -> block
198203
let canScript;
199204
if (callback && typeof callback === "object") {
200205
({canScript, callback} = callback);
201-
} else {
206+
}
207+
if (typeof canScript !== "function") {
208+
// if no canScript() callback was passed, default to execute scripts
202209
canScript = () => true;
203210
}
204211

@@ -213,9 +220,10 @@
213220

214221
if (MOZILLA) {
215222
// In order to cope with inconsistencies in XHR synchronicity,
216-
// allowing DOM element to be inserted and script to be executed
217-
// (seen with file:// and ftp:// loads) we additionally suspend on
218-
// Mutation notifications and beforescriptexecute events
223+
// allowing scripts to be executed (especially with synchronous loads
224+
// or when other extensions manipulate the DOM early) we additionally
225+
// suspend on beforescriptexecute events
226+
219227
let suspendURL = url + "&suspend=true";
220228
let suspended = false;
221229
let suspend = () => {
@@ -230,20 +238,21 @@
230238
}
231239
suspended = false;
232240
};
233-
let domSuspender = new MutationObserver(records => {
234-
suspend();
235-
});
236-
domSuspender.observe(document.documentElement, {childList: true});
237241

238242
let onBeforeScript = e => {
239-
suspend();
240-
if (!canScript()) e.preventDefault();
243+
while(typeof canScript() === "undefined") {
244+
suspend();
245+
}
246+
if (!canScript()) {
247+
console.debug("sendSyncMessage blocked a script element", e.target);
248+
e.preventDefault();
249+
}
241250
};
251+
242252
addEventListener("beforescriptexecute", onBeforeScript, true);
243253

244254
let finalize = () => {
245255
removeEventListener("beforescriptexecute", onBeforeScript, true);
246-
domSuspender.disconnect();
247256
};
248257

249258
// on Firefox we first need to send an async message telling the

0 commit comments

Comments
 (0)