2012-01-02 43 views
0

我发现了Chrome扩展WebNavigation API,但我不知道如何使用它。有人能给我一个简单的例子吗?Chrome扩展Web导航API getFrame

API:

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

如果我想要得到的iframe的id和iframe的SCR在一个页面中,我可以使用这个API?

回答

1

如果您要访问的网页的内容,你应该在manifest.json中使用content scripts

因此,举例来说:

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

而且在myscript.js:

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

另一个方法是使用programmatic injection这实际上是简化的内容脚本。

更新: 从页面上的所有I帧得到SRC:

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

对不起,我正在尝试这段代码,我可以得到一些iframe src,但是我无法获得页面中的所有iframe src,请帮助我。感谢帮助。 – 2012-01-05 05:44:30

+0

你应该迭代通过DOM对象 - 我已经添加了一个特殊的更新;) – hamczu 2012-01-05 13:22:43

2

随着docs状态,需要通过tabId,的ProcessID,frameId ...

为了得到这些值,需要监听.onCompleted():

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

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

}); 

事件的属性在之前.getFrame已经知道()