OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 }); | 295 }); |
296 }); | 296 }); |
297 } | 297 } |
298 }; | 298 }; |
299 | 299 |
300 let observer = { | 300 let observer = { |
301 QueryInterface: XPCOMUtils.generateQI([ | 301 QueryInterface: XPCOMUtils.generateQI([ |
302 Ci.nsIObserver, Ci.nsISupportsWeakReference | 302 Ci.nsIObserver, Ci.nsISupportsWeakReference |
303 ]), | 303 ]), |
304 | 304 |
305 topic: "content-document-global-created", | 305 topic: "document-element-inserted", |
306 styleURL: Utils.makeURI("about:abp-elemhide?css"), | 306 styleURL: Utils.makeURI("about:abp-elemhide?css"), |
307 sheet: null, | 307 sheet: null, |
308 | 308 |
309 init: function() | 309 init: function() |
310 { | 310 { |
311 Services.obs.addObserver(this, this.topic, true); | 311 Services.obs.addObserver(this, this.topic, true); |
312 onShutdown.add(() => | 312 onShutdown.add(() => |
313 { | 313 { |
314 Services.obs.removeObserver(this, this.topic); | 314 Services.obs.removeObserver(this, this.topic); |
315 }); | 315 }); |
316 | 316 |
317 port.on("elemhideupdate", () => | 317 port.on("elemhideupdate", () => |
318 { | 318 { |
319 this.sheet = null; | 319 this.sheet = null; |
320 }); | 320 }); |
321 }, | 321 }, |
322 | 322 |
323 observe: function(subject, topic, data) | 323 observe: function(subject, topic, data) |
324 { | 324 { |
325 if (topic != this.topic) | 325 if (topic != this.topic) |
326 return; | 326 return; |
327 | 327 |
328 if (subject.document.readyState == "uninitialized") | 328 let window = subject.defaultView; |
| 329 if (!window) |
329 { | 330 { |
330 // It would be nice to listen to the readystatechange event here. However, | 331 // This is typically XBL bindings and SVG images, but also real |
331 // adding event listeners on uninitialized documents isn't possible. So | 332 // documents occasionally - probably due to speculative loading? |
332 // we listen for DOMContentLoaded and beforescriptexecute - whichever | |
333 // fires first. | |
334 let listener = () => | |
335 { | |
336 subject.removeEventListener("DOMContentLoaded", listener); | |
337 subject.removeEventListener("beforescriptexecute", listener); | |
338 this.observe(subject, topic, data); | |
339 }; | |
340 subject.addEventListener("DOMContentLoaded", listener); | |
341 subject.addEventListener("beforescriptexecute", listener); | |
342 return; | 333 return; |
343 } | 334 } |
| 335 let type = window.QueryInterface(Ci.nsIInterfaceRequestor) |
| 336 .getInterface(Ci.nsIWebNavigation) |
| 337 .QueryInterface(Ci.nsIDocShellTreeItem) |
| 338 .itemType; |
| 339 if (type != Ci.nsIDocShellTreeItem.typeContent) |
| 340 return; |
344 | 341 |
345 port.emitWithResponse("elemhideEnabled", { | 342 port.emitWithResponse("elemhideEnabled", { |
346 frames: getFrames(subject), | 343 frames: getFrames(window), |
347 isPrivate: isPrivate(subject) | 344 isPrivate: isPrivate(window) |
348 }).then(({ | 345 }).then(({ |
349 enabled, contentType, docDomain, thirdParty, location, filter, | 346 enabled, contentType, docDomain, thirdParty, location, filter, |
350 filterType | 347 filterType |
351 }) => | 348 }) => |
352 { | 349 { |
353 if (Cu.isDeadWrapper(subject)) | 350 if (Cu.isDeadWrapper(window)) |
354 { | 351 { |
355 // We are too late, the window is gone already. | 352 // We are too late, the window is gone already. |
356 return; | 353 return; |
357 } | 354 } |
358 | 355 |
359 if (enabled) | 356 if (enabled) |
360 { | 357 { |
361 if (!this.sheet) | 358 if (!this.sheet) |
362 { | 359 { |
363 this.sheet = Utils.styleService.preloadSheet(this.styleURL, | 360 this.sheet = Utils.styleService.preloadSheet(this.styleURL, |
364 Ci.nsIStyleSheetService.USER_SHEET); | 361 Ci.nsIStyleSheetService.USER_SHEET); |
365 } | 362 } |
366 | 363 |
367 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor) | 364 let utils = window.QueryInterface(Ci.nsIInterfaceRequestor) |
368 .getInterface(Ci.nsIDOMWindowUtils); | 365 .getInterface(Ci.nsIDOMWindowUtils); |
369 try | 366 try |
370 { | 367 { |
371 utils.addSheet(this.sheet, Ci.nsIStyleSheetService.USER_SHEET); | 368 utils.addSheet(this.sheet, Ci.nsIStyleSheetService.USER_SHEET); |
372 } | 369 } |
373 catch (e) | 370 catch (e) |
374 { | 371 { |
375 // Ignore NS_ERROR_ILLEGAL_VALUE - it will be thrown if we try to add | 372 // Ignore NS_ERROR_ILLEGAL_VALUE - it will be thrown if we try to add |
376 // the stylesheet multiple times to the same document (the observer | 373 // the stylesheet multiple times to the same document (the observer |
377 // will be notified twice for some documents). | 374 // will be notified twice for some documents). |
378 if (e.result != Cr.NS_ERROR_ILLEGAL_VALUE) | 375 if (e.result != Cr.NS_ERROR_ILLEGAL_VALUE) |
379 throw e; | 376 throw e; |
380 } | 377 } |
381 } | 378 } |
382 else if (filter) | 379 else if (filter) |
383 { | 380 { |
384 RequestNotifier.addNodeData(subject.document, subject.top, { | 381 RequestNotifier.addNodeData(window.document, window.top, { |
385 contentType, docDomain, thirdParty, location, filter, filterType | 382 contentType, docDomain, thirdParty, location, filter, filterType |
386 }); | 383 }); |
387 } | 384 } |
388 }); | 385 }); |
389 } | 386 } |
390 }; | 387 }; |
391 observer.init(); | 388 observer.init(); |
OLD | NEW |