Apple  Wang
Apple Wang

Reputation: 245

Chrome extension WebNavigation API getFrame

I found a chrome extension WebNavigation API, but I don't know how to use it. Could someone give me a simple example?

API:

chrome.webNavigation.getFrame(object details, function callback)

If I want to get iframe id and iframe's scr in a page, can I use this API ??

Upvotes: 0

Views: 3266

Answers (2)

Martin Zeitler
Martin Zeitler

Reputation: 76679

As the docs state, one needs to pass tabId, processId, frameId...

in order to get these values, one needs to listen for .onCompleted():

chrome.webNavigation.onCompleted.addListener(function(e){

    chrome.webNavigation.getFrame(
        {tabId: e.tabId, processId: e.processId, frameId: e.frameId},
        function(details){
            console.dir(details);
        }
    );

});

The event's properties are already known before the .getFrame()

Upvotes: 3

hamczu
hamczu

Reputation: 1774

If you want to access page content you should use content scripts

So, for example in the manifest.json:

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "matches": ["http://www.example.com/*"],
      "js": ["jquery.js", "myscript.js"]
    }
  ],
}

And in myscript.js:

var iframe = document.querySelector('iframe');
alert(iframe.getAttribute('id'), iframe.getAttribute('src'));

Another way is to use programmatic injection which is in fact simplified content scripting.

Update: To get src from all iframes on the page:

var iframes = document.querySelectorAll('iframe');
for(var i = 0; i < iframes.length; i++){
    console.log(iframes[i].getAttribute('id'), iframes[i].getAttribute('src'));
}

Upvotes: 2

Related Questions