我已经在权限中添加了“标签”,但此代码不起作用。当url直接传递而不是从脚本传递时,iframe会起作用。
<html>
<head>
</head>
<body >
<iframe src="www" id="link" width="400" height="300">
<p>Your browser does not support iframes.</p>
</iframe>
<script>
var url1="xxx";
chrome.tabs.getSelected(null,function(tab) {
var url1= tab.url;
});
document.getElementById("link").src=url1;
</script>
</body>
</html>发布于 2011-08-21 02:46:51
让我看看我是否理解了这个问题。
您需要向内容脚本清单中添加访问包含iframe的页面的权限。这样做之后,您就可以使用Content Scripts及其Message Passing将数据传递给该iframe。
在上面的例子中,你需要注入一个内容脚本,所以在你的清单中:
"content_scripts": [{
"matches": ["https://www.my.injected.url/*"],
"js": ["injected_script_that_has_the_iframe.js"],
"run_at": "document_end",
"all_frames": true
}]然后在该内容脚本中,您可以执行普通的JavaScript来设置URL:
document.getElementById("link").src= someURL;现在,这个URL是从哪里来的?是从你的分机发来的吗?如果是,使用消息传递来请求。
chrome.extension.sendRequest({method: "GetURL"}, function(response) {
document.getElementById("link").src= response.url;
});在你的后台页面中,你可以监听来自后台页面的请求:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "GetURL")
sendResponse({url: "http://rim.com"});
else
sendResponse({}); // snub them.
});简单地说,您的内容脚本询问您的扩展(后台页面),URL是什么,一旦得到响应,它就会更新iframe。
https://stackoverflow.com/questions/7127297
复制相似问题