Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing iframes from a Chrome content-script extension

I'm writing a Chrome extension that needs to alter a popular web app when loaded. Unfortunately, most of the UI of that web app is rendered inside an iframe, and although the address of that iframe matches my content_scripts match declaration, the user script only gets invoked for the top frame.

Q: Is there a method of accessing HTML rendered inside an iframe from a Chrome content script extension? If yes, what permissions and other manifest options should I specify? Thanks.

like image 674
dpq Avatar asked Oct 14 '10 20:10

dpq


People also ask

Can we use iframe in Chrome extension?

You can just inject an iframe of that web app in any page instead of re-implementing the whole UI from ground up. Just remember to first wrap that iframe in another iframe with src attribute pointing to a chrome-extension://* URL.

Can Chrome Extension read page content?

To use Read Aloud, navigate to the web page you want to read, then click the Read Aloud icon on the Chrome menu. In addition, the shortcut keys ALT-P, ALT-O, ALT-Comma, and ALT-Period can be used to Play/Pause, Stop, Rewind, and Forward. You may also select the text you want to read before activating the extension.

How do you view the code of an extension in Chrome?

After the extension is added to your browser, open an extension's page on the Chrome Web Store, such as the Markdown Preview extension page. When the page loads, click on the CRX icon in the extensions bar in Chrome and select “View source.”


2 Answers

I've resolved the problem. The following option has to be specified in the content_scripts section of the manifest.json: "all_frames": true. Without it, the script is only applied to the top frame.

// Sometimes one just has to RTFM carefully.

like image 143
dpq Avatar answered Sep 28 '22 01:09

dpq


Even "all_frames": true doesn't seem to help in the case of iframes without @src. This is discussed in the bug http://code.google.com/p/chromium/issues/detail?id=20773, which also covers some workarounds, including getting the contentDocument of the iframe element in the source page, e.g. $('a', $($("#canvas_frame")[0].contentDocument)).

This bug is not exactly the problem you were having (you wanted to load your content script inside the iframe, not interact between the iframe and the outer frame) but I think that most people who have one of the problems will have the other one as well.

like image 32
Kragen Javier Sitaker Avatar answered Sep 28 '22 01:09

Kragen Javier Sitaker