简体   繁体   English

如何在Chrome扩展程序中的popup.html中访问iframe

[英]How to access iframe in popup.html in a chrome extension

I have a chrome extension that opens a webpage via an iframe inside it's popup.html 我有一个chrome扩展程序,可以通过popup.html中的iframe打开网页

What I need to do is detect when the user browses to certain URLs in the iframe, and then open those URLs as a new tab. 我需要做的是检测用户何时浏览到iframe中的某些URL,然后将这些URL作为新标签打开。

I was trying to access the .location of the iframe, but unfortunately was getting the policy issues between "chrome-extensions" and "http" 我试图访问iframe的.location,但不幸的是遇到了“ chrome-extensions”和“ http”之间的政策问题

I've been researching, and I'm seeing a lot of notes on using content scripts, but I'm really not understanding how they work fully. 我一直在研究,并且看到了很多有关使用内容脚本的说明,但是我真的不了解它们如何充分发挥作用。 Most examples are trying to inject code into current pages in the browser itself so I don't think they apply. 大多数示例都试图将代码注入浏览器本身的当前页面中,因此我认为它们不适用。 Is a content script what I need? 我需要内容脚本吗?

Any help would be most helpful. 任何帮助将是最有帮助的。

不可能,这是不可能的,因为chrome扩展名弹出窗口是另一个域站点,您无法在站点内访问它。

I've been trying to do the same thing and i found this answer on SO. 我一直在尝试做同样的事情,所以我找到了这个答案。 This worked for me and may be can work in our case as well. 这对我有用,在我们的情况下也可能适用。 Just in case if the link may stop working in future, i'm copying his answer here: 以防万一该链接将来可能停止工作,我在这里复制他的答案:

"content_scripts": [{
    "matches": ["http://*/*", "https://*/*"],
    "js": ["content_frame.js"],
    "all_frames": true
}],


By setting all_frames to true you will inject/include content_frame.js javascript file into top document and all iframes. 通过将all_frames设置为true您可以将content_frame.js javascript文件注入/包括到顶级文档和所有iframe中。 In case you just need javascript injected in iframe but not in top document you can check inside your content_frame.js file like this: 如果您只需要在iframe中注入javascript而不在顶层文档中注入JavaScript,则可以像下面这样检查content_frame.js文件:

if (parent === top) {
    // here you can put your code that will run only inside iframe
}

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

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