fmsf fmsf - 2 years ago 454
HTML Question

access iframe content from a chrome's extension content script

I'm doing a plugin to do some transformations to the interface. I keep getting

unsafe javascript attempt to access frame with url.... Domains, protocols and ports must match
(typical cross site issue)

But being an extension it should have access to the iframe's content ...

Doesn anyone know how to access it's contents so they can be capturable?

Answer Source

There's generally no direct way of accessing a different-origin window object. If you want to securely communicate between content scripts in different frames, you have to send a message to the background page which in turn sends the message back to the tab.

Here is an example:

Part of manifest.json:

"background": {"scripts":["bg.js"]},
"content_scripts": [
    {"js": ["main.js"], "matches": ["<all_urls>"]},
    {"js": ["sub.js"], "matches": ["<all_urls>"], "all_frames":true}


var isTop = true;
chrome.runtime.onMessage.addListener(function(details) {
    alert('Message from frame: ' +;


if (!window.isTop) { // true  or  undefined
    // do something...
    var data = 'test';
    // Send message to top frame, for example:
    chrome.runtime.sendMessage({sendBack:true, data:data});

Background script 'bg.js':

chrome.runtime.onMessage.addListener(function(message, sender) {
    if (message.sendBack) {

An alternative method is to use chrome.tabs.executeScript in bg.js to trigger a function in the main content script.

Relevant documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download