谷歌Chrome扩展:传递javascript变量到iframe src
Google Chrome Extension: Passing javascript variable to iframe src
我在权限中添加了"tabs",但这段代码不起作用。当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>
让我看看我是否理解了这个问题。
您需要在您的内容脚本清单中添加访问具有iframe的页面的权限。一旦你这样做,你使用内容脚本和它的消息传递传递数据到该iframe。
在上面的例子中,你需要注入一个内容脚本,所以在你的manifest中:"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从哪里来?是你的分机吗?如果是,使用Message Passing请求。
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
相关文章:
- 如何使用变量填充iframe源
- 在跨子域上将JS变量从子Iframe传递给父JSP
- 在 iframe 中指定全局变量名称时,表单目标会中断
- JavaScript 变量等于 iframe
- javascript变量中的简单Iframe
- 您可以从S3上托管的Iframe中访问全局JS变量吗
- 如何将较少的变量更改推送到html iframe
- JavaScript 访问 Firefox 中的 iframe 内部变量
- 在 iframe 中访问 javascript 变量
- 将 iframe 的 id 存储在 javascript 变量中
- 未在 iFrame 中定义的 Javascript 变量
- 使用 javascript 访问父级中的 iframe 变量
- 在 iframe 内分配一个变量
- 从 iFrame 中提取 url 变量
- 如何在 iframe 中访问包含另一个 html 的变量
- PHP 中的跨域变量 iframe
- 动态更改Youtube Iframe播放器变量
- mootools将变量存储在IE中iframe的父级中
- 如何使用js在父窗口的所有iframe中设置变量
- 如何使用显示变量url的iframe打开引导模式