我可以在 Firefox 扩展中检测并阻止 JavaScript 驱动的重定向吗?

发布于 2025-01-07 16:52:21 字数 232 浏览 3 评论 0 原文

基本上,我想让 Firefox 遵循“当网站尝试重定向或重新加载页面时警告我”用户首选项。目前,这对于任何类型的入门作家等来说都是真正的芝麻开门。

请在 相关超级用户帖子

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.

Please find the detailed description of this misbehaviour in related superuser post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2025-01-14 16:52:21

您可以使用 Object.watch() 拦截某些属性的更改,例如 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);

您必须类似地拦截对其他 window.location 属性,例如 hostpathname。这种方法的一大缺点是:只能有一个观察者。如果页面安装了自己的观察程序或只是调用 wnd.unwatch("location") 所有检测都将消失。

如何在任何页面 JavaScript 有机会运行之前到达窗口取决于您是否使用附加 SDK 或者是否有经典附加组件。如果您使用的是附加 SDK,则可以使用 page-mod 模块,其中 contentScriptWhen 参数设置为 start。在上面的代码示例中,将 wnd 替换为 unsafeWindow (该窗口必须直接访问,否则无法工作)。

在经典插件中,您可以为 content-document-global-created< 注册观察者/code> 通知。在这里,您还必须直接访问该窗口:

var wnd = XPCNativeWrapper.unwrap(subject);

请参阅有关XPCNativeWrapper.unwrap()的文档

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).

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:

var wnd = XPCNativeWrapper.unwrap(subject);

See documentation on XPCNativeWrapper.unwrap().

一梦等七年七年为一梦 2025-01-14 16:52:21

“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's setter 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文