Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension content scripts and iframe

I'm tryng to make an extension for google chrome that does this things:

Run when loading www.example.com example.com has an iframe to another site, i need to get access to a link from this iframe (this is rapidshare, I need to grab the downloadlink)

So far so good, but..

Then I need the extension to inform the link url to example.com for further processing.

Any ideas o directions??

I've read http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication but can't make it work...

like image 916
ricardocasares Avatar asked Jul 04 '11 10:07

ricardocasares


2 Answers

You need to inject 2 content scripts:

"content_scripts": [
    {
      "matches": ["http://www.example.com/*"],
      "js": ["example.js"]
    },{
      "matches": ["http://www.rapidshare.com/*"],
      "all_frames": true,
      "js": ["rapidshare.js"]
    }
]

In order to transfer the link from one script to another you need to communicate through background page:

rapidshare.js:

chrome.extension.sendRequest({url: "link"});

background.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    chrome.tabs.sendRequest(sender.tab.id, request);
    sendResponse({});
});

example.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    console.log("received url:", request.url);
    sendResponse({});
});
like image 191
serg Avatar answered Sep 27 '22 23:09

serg


iframe is a separate document for the browser, that's why you can use chrome extension and integrate it directly into iframe. You need just mention URL of the iframe in chrome extension manifest file. We use integration of some menus and toolbars of our chrome extension apps into iframes. It works.

There is a nice article as additional info for Chrome Extensions and iFrames http://www.natewillard.com/blog/chrome/javascript/iframes/2015/10/20/chrome-communication/

like image 40
Aleksandr Golovatyi Avatar answered Sep 27 '22 21:09

Aleksandr Golovatyi