您不需要將腳本注入html文件裏面的的擴展名。從這些,你將能夠從頁面腳本訪問鉻。* API。這就是說,如果iframe和帶有文本框的框架位於不同的域中,則由於跨站點腳本安全性阻止了它,所以不能從另一個域訪問它。
我需要看到更多的你已經有了HTML文件,但你也許能夠做這樣的事......
在擴展選項卡(包含文本框):
chrome.extension.onRequest.addListener(function (response) {
if (response.type === "urlUpdate") {
document.getElementById("addressBar").value = response.url;
}
});
在後臺頁面:
chrome.extension.onRequest.addListener(function(request, sender) {
if (request.type === "urlUpdate") {
chrome.tabs.sendRequest(sender.tab.id, {type: "urlUpdate", url: request.url});
}
});
最後,內容腳本在http://*
和https://*
運行。請確保你在你的清單(將其添加到內容腳本的現有列表)有這樣的:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["address.js"],
"all_frames": true
}
];
這是你有all_frames
設置爲true
拿到腳本的iframe內運行重要 。
然後,你必須在這個address.js
:
if (window.top !== window) {
chrome.extension.sendRequest({type: "urlUpdate", url: location.href});
}
基本上,window.top
檢查只能由從iframe內的網址發送更新消息避免混淆。您可能需要更多篩選,以防止它在您不想要的其他頁面上運行。
這是未經測試的,但我相當確信這種解決方案是可行的。