您不需要将脚本注入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内的网址发送更新消息避免混淆。您可能需要更多筛选,以防止它在您不想要的其他页面上运行。
这是未经测试的,但我相当确信这种解决方案是可行的。