我發現了Chrome擴展WebNavigation API,但我不知道如何使用它。有人能給我一個簡單的例子嗎?Chrome擴展Web導航API getFrame
API:
chrome.webNavigation.getFrame(object details, function callback)
如果我想要得到的iframe的id和iframe的SCR在一個頁面中,我可以使用這個API?
我發現了Chrome擴展WebNavigation API,但我不知道如何使用它。有人能給我一個簡單的例子嗎?Chrome擴展Web導航API getFrame
API:
chrome.webNavigation.getFrame(object details, function callback)
如果我想要得到的iframe的id和iframe的SCR在一個頁面中,我可以使用這個API?
如果您要訪問的網頁的內容,你應該在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'));
}
隨着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已經知道()
對不起,我正在嘗試這段代碼,我可以得到一些iframe src,但是我無法獲得頁面中的所有iframe src,請幫助我。感謝幫助。 – 2012-01-05 05:44:30
你應該迭代通過DOM對象 - 我已經添加了一個特殊的更新;) – hamczu 2012-01-05 13:22:43