Basically, i want to make Firefox obey "Warn me when web sites try to redirect or reload the page" user preference. Currently, this is really open sesame for any kind of doorway writers etc.
You can use Object.watch() to intercept changes of some properties like window.location:
function onLocationChange(id, oldval, newval)
{
if (confirm("Do you want to navigate to " + newval + "?"))
return newval;
else
return oldval;
}
wnd.watch("location", onLocationChange);
wnd.document.watch("location", onLocationChange);
wnd.location.watch("href", onLocationChange);
You would have to similarly intercept assignments to other window.location properties like host or pathname. The big disadvantage of this approach: there can be only one watcher. If the page installs its own watcher or simply calls wnd.unwatch("location") all your detection will be gone.
How to get to the window before any page JavaScript has a chance to run depends on whether you are using the Add-on SDK or whether you have a classic add-on. If you are using the Add-on SDK then you use the page-mod module with contentScriptWhen parameter set to start. In the code example above you replace wnd by unsafeWindow (the window has to be accessed directly, otherwise it won't work).
A "JavaScript-driven redirect" would be done by using the window.location property. All you would need to do is replace that property's setter function as soon as the page loads.
I wasn't able to override the default window.location setter using client side js, but it may be possible using a more privileged extension environment though.
发布评论
评论(2)
您可以使用
Object.watch()
拦截某些属性的更改,例如window.location
:您必须类似地拦截对其他
window.location
属性,例如host
或pathname
。这种方法的一大缺点是:只能有一个观察者。如果页面安装了自己的观察程序或只是调用wnd.unwatch("location")
所有检测都将消失。如何在任何页面 JavaScript 有机会运行之前到达窗口取决于您是否使用附加 SDK 或者是否有经典附加组件。如果您使用的是附加 SDK,则可以使用
page-mod
模块,其中contentScriptWhen
参数设置为start
。在上面的代码示例中,将wnd
替换为unsafeWindow
(该窗口必须直接访问,否则无法工作)。在经典插件中,您可以为
content-document-global-created< 注册观察者/code> 通知
。在这里,您还必须直接访问该窗口:
请参阅有关
XPCNativeWrapper.unwrap()的文档
。
You can use
Object.watch()
to intercept changes of some properties likewindow.location
:You would have to similarly intercept assignments to other
window.location
properties likehost
orpathname
. The big disadvantage of this approach: there can be only one watcher. If the page installs its own watcher or simply callswnd.unwatch("location")
all your detection will be gone.How to get to the window before any page JavaScript has a chance to run depends on whether you are using the Add-on SDK or whether you have a classic add-on. If you are using the Add-on SDK then you use the
page-mod
module withcontentScriptWhen
parameter set tostart
. In the code example above you replacewnd
byunsafeWindow
(the window has to be accessed directly, otherwise it won't work).In a classic add-on you register an observer for the
content-document-global-created
notification. Here you also have to access the window directly:See documentation on
XPCNativeWrapper.unwrap()
.“JavaScript 驱动的重定向”可以通过使用
window.location
属性来完成。您需要做的就是在页面加载后立即替换该属性的setter
函数。这是 John Resig 的一篇关于定义属性的 setter 和 getter 的博客文章。 http://ejohn.org/blog/javascript-getters-and-setters/
我无法使用客户端 js 覆盖默认的
window.location
setter,但可以使用更特权的扩展环境。A "JavaScript-driven redirect" would be done by using the
window.location
property. All you would need to do is replace that property'ssetter
function as soon as the page loads.Here is a John Resig blog post on defining setters and getters for properties. http://ejohn.org/blog/javascript-getters-and-setters/
I wasn't able to override the default
window.location
setter using client side js, but it may be possible using a more privileged extension environment though.