简体   繁体   中英

Using a bookmarklet to modify the current page, and accessing iFrames

I have been working on making a bookmarklet to make a tiny change to a webpage, for my own personal use as I am browsing the site. I have the bookmarklet dynamically loading jquery (if needed) and can access and modify the page without any issues.

This page has several nested iFrames, the html, I want to modify is within an iFrame which has a different domain then the original page. So I get the:

Unsafe JavaScript attempt to access frame with URL

The browser is using the topmost page as the point of origin for my script, which is interesting since it's origin is a bookmark. Is it possible to get around this limitation?

There is no way to get around this limitation using only a bookmarklet. When you execute a bookmarklet it executes within the context of the current (top) page. Even though the restriction is called "same origin policy", origin means the context within which the code was executed, not the origin of where the code came from. When you execute a bookmarklet, the code comes from the bookmark, but it executes inside the current page.

To achieve your goal you'll have to create a user script or create an addon/extension. User scripts are supported in Firefox if you install Greasemonkey and natively in Chrome.

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