使用 chrome 扩展在 iframe 中注入 javascript [英] Inject javascript in an iframe using chrome extension

查看:144
本文介绍了使用 chrome 扩展在 iframe 中注入 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于将 javascript 注入文档页面的问题/答案.我真正想做的是在文档的 iframe 中注入 javascript.

There are lots of questions/answers on injecting javascript into the document page. What I really want to do is inject javascript in an iframe within the document.

需要明确的是,iframe 与文档不属于同一个域.我已经通过控制台(不是通过扩展)尝试过它,但无法这样做.

To be clear, that iframe doesn't belong to the same domain as the document. I've already tried it through console (not through extension) but was unable to do so.

我很好奇这是否可以使用 chrome 扩展来完成?

I'm very curious if this is something that can be done using a chrome-extension ?

推荐答案

是的,您可以使用 content_scripts 属性如下:

Yes, you can use content_scripts property in manifest.json like this:

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

通过将 all_frames 设置为 true,您会将 content_frame.js javascript 文件注入/包含到顶级文档和所有 iframe 中.如果您只需要在 iframe 中注入 javascript 而不是在顶级文档中,您可以像这样检查 content_frame.js 文件:

By setting all_frames to true you will inject/include content_frame.js javascript file into top document and all iframes. 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:

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

这篇关于使用 chrome 扩展在 iframe 中注入 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆