I am looking into ways to extend Firefox pop-up blocking from an extension. One option is replacing window.open()
(or rather Window.prototype.open()
) in the webpage by a wrapper function. An important requirement is that this manipulation cannot be detected or reverted by the webpage. For example, if I simply do this:
Window.prototype.open = wrapper;
The webpage can easily revert the change by doing:
delete Window.prototype.open;
Instead I can use Object.defineProperty() to set advanced property flags:
Object.defineProperty(Window.prototype, "open", {value: wrapper, configurable: false});
The webpage can no longer revert this change but it can still detect it: delete Window.prototype.open
normally changes the value of Window.prototype.open
(different instance of the same function it seems), here delete
won't have any effect at all. Also, Window.prototype.open = "test";delete Window.prototype.open;
will produce inconsistent results (different ones depending on whether writable: false
flag is specified for the property).
Is there anything else that I can do to emulate the behavior of the original property (short of using binary XPCOM components which has way too many issues of its own)?
You might try using the nsIWindowWatcher
interface to register your own window creator (nsIWindowCreator
). That way you can control whether a new window is opened without affecting the window object itself (and thus remaining invisible to web sites).
I'm not sure whether the inability to change the implementation of window.open()
without this being detectable is a bug. Perhaps it's just not considered an important requirement for methods like Object.defineProperty
. But it might be worth filing a bug to see what others think about making this an option in the future. After all, ad blocking is a major use case.