编程语言
首页 > 编程语言> > javascript – 如何解决“权限被拒绝访问属性’警报’”?

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

作者:互联网

我正在制作一个greasmonkey脚本的网页.此页面包含多个iframe.我发现它们是问题,但我不知道如何解决这个问题.

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

Firefox中的Javascript控制台已经向我大声说,文档拒绝访问,因为我的脚本正在使用document.getElementByID来查找顶部文档的正文,其中div框被追加到其中.

这是一个容易避免的问题,因为脚本失败并陷入iframe,但它仍然在主页上继续,因为它确实提供了对文档的访问.

当我尝试在我的函数中使用警报时问题出现了问题.看起来像iframe接管我的脚本,并通过拒绝访问属性’alert’来杀死它.

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

希望它听起来并不令人困惑.

感谢您的任何帮助.

解决方法:

回覆:

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.

首先,调整include和exclude指令以尽可能忽略iframe.例如:

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

接下来,将这两行添加为Metadata Block之后的第一行代码:

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

这将阻止GM脚本在iframe上运行.

标签:permission-denied,javascript,firefox,alert,greasemonkey
来源: https://codeday.me/bug/20190903/1797575.html