|
195 | 195 | let uuid = () => (Math.random() * Date.now()).toString(16); |
196 | 196 | let docUrl = document.URL; |
197 | 197 | browser.runtime.sendSyncMessage = (msg, callback) => { |
| 198 | + let canScript; |
| 199 | + if (callback && typeof callback === "object") { |
| 200 | + ({canScript, callback} = callback); |
| 201 | + } else { |
| 202 | + canScript = () => true; |
| 203 | + } |
| 204 | + |
198 | 205 | let msgId = `${uuid()},${docUrl}`; |
199 | 206 | let url = `${ENDPOINT_PREFIX}id=${encodeURIComponent(msgId)}` + |
200 | 207 | `&url=${encodeURIComponent(docUrl)}`; |
|
203 | 210 | // about frameAncestors |
204 | 211 | url += "&top=true"; |
205 | 212 | } |
206 | | - /* |
207 | | - if (document.documentElement instanceof HTMLElement && !document.head) { |
208 | | - // let's insert a head element to let userscripts work |
209 | | - document.documentElement.appendChild(document.createElement("head")); |
210 | | - }*/ |
211 | 213 |
|
212 | 214 | if (MOZILLA) { |
213 | | - // on Firefox we first need to send an async message telling the |
214 | | - // background script about the tab ID, which does not get sent |
215 | | - // with "privileged" XHR |
216 | | - let result; |
217 | | - browser.runtime.sendMessage( |
218 | | - {__syncMessage__: {id: msgId, payload: msg}} |
219 | | - ).then(r => { |
220 | | - result = r; |
221 | | - if (callback) callback(r); |
222 | | - }).catch(e => { |
223 | | - throw e; |
224 | | - }); |
225 | | - |
226 | 215 | // In order to cope with inconsistencies in XHR synchronicity, |
227 | 216 | // allowing DOM element to be inserted and script to be executed |
228 | 217 | // (seen with file:// and ftp:// loads) we additionally suspend on |
|
245 | 234 | suspend(); |
246 | 235 | }); |
247 | 236 | domSuspender.observe(document.documentElement, {childList: true}); |
248 | | - addEventListener("beforescriptexecute", suspend, true); |
| 237 | + |
| 238 | + let onBeforeScript = e => { |
| 239 | + suspend(); |
| 240 | + if (!canScript()) e.preventDefault(); |
| 241 | + }; |
| 242 | + addEventListener("beforescriptexecute", onBeforeScript, true); |
249 | 243 |
|
250 | 244 | let finalize = () => { |
251 | | - removeEventListener("beforescriptexecute", suspend, true); |
| 245 | + removeEventListener("beforescriptexecute", onBeforeScript, true); |
252 | 246 | domSuspender.disconnect(); |
253 | 247 | }; |
254 | 248 |
|
| 249 | + // on Firefox we first need to send an async message telling the |
| 250 | + // background script about the tab ID, which does not get sent |
| 251 | + // with "privileged" XHR |
| 252 | + let result; |
| 253 | + browser.runtime.sendMessage( |
| 254 | + {__syncMessage__: {id: msgId, payload: msg}} |
| 255 | + ).then(r => { |
| 256 | + result = r; |
| 257 | + if (callback) callback(r); |
| 258 | + }).catch(e => { |
| 259 | + throw e; |
| 260 | + }); |
| 261 | + |
| 262 | + |
| 263 | + |
255 | 264 | if (callback) { |
256 | 265 | let realCB = callback; |
257 | 266 | callback = r => { |
|
263 | 272 | }; |
264 | 273 | return; |
265 | 274 | } |
| 275 | + |
266 | 276 | try { |
267 | 277 | suspend(); |
268 | 278 | } finally { |
|
0 commit comments