简体   繁体   中英

How to solve “Permission denied to access property 'alert'”?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM