Possible Duplicate:
access iframe content from a chrome's extension content script
I'm trying to manipulate part of an iframe on a webpage via a Chrome extension.
Everytime I do, I get a "Unsafe JavaScript attempt to access frame with URL" error. Reading the Chrome extension documentation, it appears I should be able to ask for permissions to do this in my manifest.json. But nothing I've done seems to allow me to do this.
My manifest.json:
{
"name": "Manipulate an iframe",
"description": "Test to manipulate an iframe",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": [
"tabs",
"http://*/",
"https://*/",
"<all_urls>"
],
"browser_action": {
"name": "Modify iframe",
"icons": ["icon.png"]
},
"manifest_version": 2
}
My background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, { allFrames:true, runAt:"document_end", file: "jquery-latest.min.js" }, function() {
chrome.tabs.executeScript(null, { allFrames:true, runAt:"document_end", file: "content-script.js" });
});
});
My content-script.js:
$('.container').css('width','98%');
$('#myPage iframe').contents().find('#code').unwrap()
The container is resized correctly when I click the extension button, but I can't touch the iframe. It seems to me that I've given all the permissions I can, so is there somewhere else I need to specify permissions?
Edit :
I found this answer to be helpful in understanding the different types of scripts in a Chrome extension: https://stackoverflow.com/a/9916089/1698152
The flagged duplicate answer: access iframe content from a chrome's extension content script was sufficient to answer my question.
Instead of trying to inject into all frames via tabs.executeScript, I injected content scripts into all frames by defining them in the manifest. I was then able to call functions in these scripts from the background page script via message passing.
I still don't understand why this is any different from what I was already doing, however.