Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 http://code.google.com/chrome/extensions/content_scripts.html ...

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

like image 210
fmsf Avatar asked Jul 04 '12 08:07

fmsf


People also ask

Can we use iframe in Chrome extension?

No, the content scripts will NOT execute in the iframes loaded dynamically via JavaScript in the page.

How do I find iFrames in Google Chrome?

We can detect if an element is inside an iframe by inspecting the element with the Chrome Developer Tools. An easier way is to perform a Right Click near the element in your browser and see if View Frame Source option is present in the context dropdown.


1 Answers

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} ] 

main.js:

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

sub.js:

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) {         chrome.tabs.sendMessage(sender.tab.id, message.data);     } }); 

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

Relevant documentation

  • Message passing c.runtime.sendMessage / c.tabs.sendMessage / c.runtime.onMessage
  • MessageSender and Tab types.
  • Content scripts
  • chrome.tabs.executeScript
like image 113
Rob W Avatar answered Oct 11 '22 12:10

Rob W