0

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.

Community
  • 1
  • 1
bmurr
  • 121
  • 1
  • 8
  • Those permissions give you the ability to make cross domain ajax requests. I do not believe that it gives you cross frame scripting permissions. The iframe must be serving content on the same domain as your page. https://developer.mozilla.org/en-US/docs/HTML/Element/iframe#Scripting – Adam Oct 05 '12 at 17:28
  • @Adam Surely I am able to do this somehow. Perhaps I should be trying to do this through the Chrome API instead? – bmurr Oct 05 '12 at 18:00

0 Answers0