javascript Chrome 扩展内容脚本和 iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6570363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome extension content scripts and iframe
提问by ricardocasares
I'm tryng to make an extension for google chrome that does this things:
我正在尝试为 google chrome 做一个扩展来做这些事情:
Run when loading www.example.com example.com has an iframe to another site, i need to get access to a link from this iframe (this is rapidshare, I need to grab the downloadlink)
在加载 www.example.com 时运行 example.com 有一个 iframe 到另一个站点,我需要从这个 iframe 访问链接(这是 Rapidshare,我需要获取下载链接)
So far so good, but..
到目前为止一切顺利,但是..
Then I need the extension to inform the link url to example.com for further processing.
然后我需要扩展来通知 example.com 的链接 url 以进行进一步处理。
Any ideas o directions??
任何想法或方向?
I've read http://code.google.com/chrome/extensions/content_scripts.html#host-page-communicationbut can't make it work...
我已阅读http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication但无法使其工作...
回答by serg
You need to inject 2 content scripts:
您需要注入 2 个内容脚本:
"content_scripts": [
{
"matches": ["http://www.example.com/*"],
"js": ["example.js"]
},{
"matches": ["http://www.rapidshare.com/*"],
"all_frames": true,
"js": ["rapidshare.js"]
}
]
In order to transfer the link from one script to another you need to communicate through background page:
为了将链接从一个脚本转移到另一个脚本,您需要通过后台页面进行通信:
rapidshare.js:
Rapidshare.js:
chrome.extension.sendRequest({url: "link"});
background.js:
背景.js:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
chrome.tabs.sendRequest(sender.tab.id, request);
sendResponse({});
});
example.js:
例子.js:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
console.log("received url:", request.url);
sendResponse({});
});
回答by Aleksandr Golovatyi
iframe is a separate document for the browser, that's why you can use chrome extension and integrate it directly into iframe. You need just mention URL of the iframe in chrome extension manifest file. We use integration of some menus and toolbars of our chrome extension apps into iframes. It works.
iframe 是浏览器的单独文档,这就是为什么您可以使用 chrome 扩展并将其直接集成到 iframe 中。您只需要在 chrome 扩展清单文件中提及 iframe 的 URL。我们将 chrome 扩展应用程序的一些菜单和工具栏集成到 iframe 中。有用。
There is a nice article as additional info for Chrome Extensions and iFrames http://www.natewillard.com/blog/chrome/javascript/iframes/2015/10/20/chrome-communication/
有一篇不错的文章作为 Chrome 扩展和 iFrame 的附加信息http://www.natewillard.com/blog/chrome/javascript/iframes/2015/10/20/chrome-communication/