Javascript 如何解决“权限被拒绝访问属性'警报'”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5840928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve "Permission denied to access property 'alert'"?
提问by user1651105
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.
我正在为一个网页制作 greasmonkey 脚本。这个页面有几个 iframe。我发现它们是问题所在,但是我不知道如何解决问题。
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.
起初,我的脚本创建了一个带有按钮的小 div 框。通过按下按钮脚本分析网页内容并alert
在某些条件下调用。
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.
Firefox 中的 Javascript 控制台已经向我大喊访问被拒绝,document
因为我的脚本正在使用document.getElementByID
查找顶部文档的正文,其中 div 框被附加到。
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
.
这是很容易避免的问题,因为脚本会失败并卡在 iframe 中,但它仍然在主页上继续运行,因为它确实提供了对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'
.
当我尝试在我的函数中使用警报时,问题就出现了。似乎 iframe 接管了我的脚本并使用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.
我如何告诉浏览器/脚本或其他什么,我只希望我的脚本在主文档上运行,而我不想被 iframe 打扰?我安装了 NO-script 插件,允许主域并阻止了辅助域(即在 iframe 中加载)并且所有警报消息都正常。但是,我不能要求我的用户安装 noscript 启用/禁用特定域,我的脚本应该可以正常工作。我需要找到适用于所有启用的 iframe 的解决方案。
Hope it didn't sound confusing.
希望这听起来不会令人困惑。
Thank you for any help.
感谢您的任何帮助。
回答by Brock Adams
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.
我如何告诉浏览器/脚本或其他什么,我只希望我的脚本在主文档上运行,而我不想被 iframe 打扰?我安装了 NO-script 插件,允许主域并阻止了辅助域(即在 iframe 中加载)并且所有警报消息都正常。
First, adjust your include and exclude directives to ignore the iframes as much as possible. EG:
首先,调整您的包含和排除指令以尽可能地忽略 iframe。例如:
// @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.
这将阻止 GM 脚本在 iframe 上运行。