简体   繁体   English

Chrome 扩展内容脚本和 iframe

[英]Chrome extension content scripts and iframe

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) 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)

So far so good, but..到目前为止一切顺利,但是..

Then I need the extension to inform the link url to example.com for further processing.然后我需要扩展通知链接 url 到 example.com 进行进一步处理。

Any ideas o directions??任何想法或方向?

I've read http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication but can't make it work...我已阅读http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication但无法使其工作...

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:快速共享.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({});
});

iframe is a separate document for the browser, that's why you can use chrome extension and integrate it directly into iframe. iframe 是浏览器的单独文档,这就是为什么您可以使用 chrome 扩展并将其直接集成到 iframe 中的原因。 You need just mention URL of the iframe in chrome extension manifest file.您只需在 chrome 扩展清单文件中提及 iframe 的 URL 即可。 We use integration of some menus and toolbars of our chrome extension apps into iframes.我们将 chrome 扩展应用程序的一些菜单和工具栏集成到 iframe 中。 It works.有用。

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 扩展和 iFrames http://www.natewillard.com/blog/chrome/javascript/iframes/2015/10/20/chrome-communication/的附加信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM