如何解决“访问属性的权限被拒绝‘警报’”?

发布于 2024-11-04 05:47:51 字数 685 浏览 4 评论 0原文

我正在为一个网页制作 greasmonkey 脚本。该页面有多个 iframe。我发现它们是问题所在,但我不知道如何解决该问题。

首先,我的脚本创建了一个带有按钮的小 div 框。通过按下按钮,脚本分析网页内容并在某些条件下调用警报

Firefox 中的 Javascript 控制台已经告诉我 document 的访问被拒绝,因为我的脚本正在使用 document.getElementByID 来查找顶部文档的正文,其中附加了 div 框到。

这是很容易避免的问题,因为脚本会失败并卡在 iframe 中,但它仍然在主页上继续,因为它确实提供了对 document 的访问权限。

当我尝试在我的函数中使用警报时,问题就出现了。似乎 iframe 接管了我的脚本,并以访问属性“alert”的权限被拒绝的方式终止了它。

我如何告诉浏览器/脚本或其他什么,我只希望我的脚本在主文档上运行,并且我不想被 iframe 打扰?我安装了无脚本插件,允许主域并阻止辅助域(即加载到 iframe 中)并且所有警报消息都正常。但是,我不能要求我的用户安装 noscript 启用/禁用特定域,并且我的脚本应该可以正常工作。我需要找到适用于所有启用的 iframe 的解决方案。

希望这听起来没有令人困惑。

感谢您的帮助。

There is a webpage i am making a greasmonkey script for. This page has several iframes. I have found out that they are the problem, however i do not know how to solve the issue.

At first my script creates a small div box with the button. By pressing a button script analyzes webpage contents and calls alert on certain conditions.

Javascript console in Firefox already shouts to me that access was denied for document because my script is using document.getElementByID to find the top document's body, where the div box is being appended to.

This is easily avoidable problem as then script fails and gets stuck in iframes, yet it still continues on the main page as it does give the access to document.

The problem araises when i try to use alert in my function. Seems like iframes take over my script and kills it with the Permission denied to access property 'alert'.

How do i tell browser/script or whatever, that i only want my script to run on main document and i do not want to be bothered by iframes?I installed NO-script addon, allowed the primary domain and blocked the secondary domain (that loads inside iframes) and all the alert messages go normal. But, i can't ask my users to install noscript enable/disable specific domains and my script should be working fine. I need to find solution which would work with all the iframes enabled.

Hope it didn't sound confusing.

Thank you for any help.

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

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

发布评论

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

评论(1

南街九尾狐 2024-11-11 05:47:51

关于:

我如何告诉浏览器/脚本或其他什么,我只希望我的脚本在主文档上运行,并且我不想被 iframe 打扰?我安装了无脚本插件,允许主域并阻止辅助域域(在 iframe 内加载)和所有警报消息都正常。

首先,调整包含和排除指令以尽可能忽略 iframe。例如:

// @include  http://example.com/normal/*
// @include  http://example.com/extra/*
// @exclude  http://example.com/extra/in_iframe/*

接下来,将这两行添加为 元数据块 之后的第一行代码:

if (window.top != window.self)  //-- Don't run on frames or iframes
    return;

这将停止 GM 脚本从 iframe 上运行。

RE:

How do i tell browser/script or whatever, that i only want my script to run on main document and i do not want to be bothered by iframes?I installed NO-script addon, allowed the primary domain and blocked the secondary domain (that loads inside iframes) and all the alert messages go normal.

First, adjust your include and exclude directives to ignore the iframes as much as possible. EG:

// @include  http://example.com/normal/*
// @include  http://example.com/extra/*
// @exclude  http://example.com/extra/in_iframe/*

Next, add these 2 lines as the first lines of code after the Metadata Block:

if (window.top != window.self)  //-- Don't run on frames or iframes
    return;

That will stop the GM script from running on iframes.

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