简体   繁体   English

Firefox WebExtensions 注入 iframe 中的脚本

[英]Firefox WebExtensions inject script in iframe

I am new to javascript and writing web extensions.我是 javascript 和编写 web 扩展的新手。 I want to write a web extension which changes the color of schedule entrys in my university planner.我想写一个 web 扩展来改变我的大学规划器中日程表条目的颜色。 After some starting struggle I've been finally able to write a Firefox WebExtension that does exactly that on my uni's platform.经过一番努力,我终于能够在我的 uni 平台上编写一个 Firefox WebExtension。 The problem is that my uni uses multiple platforms for different tasks which can either be accessed directly or via a platform that connects them all.问题是我的大学使用多个平台来完成不同的任务,这些任务可以直接访问,也可以通过连接它们的平台访问。 I've only been able to succesfully change the colors on the platform that directly shows me my schedule, not on the "general" platform.我只能在直接向我显示我的日程安排的平台上成功更改 colors,而不是在“通用”平台上。 After some more research I found the problem.经过更多研究,我发现了问题所在。 The planner is loaded in an iframe and since both platforms have a different domain this seems to be a bigger problem then I thought.规划器加载在 iframe 中,由于两个平台具有不同的域,这似乎是一个比我想的更大的问题。 I read a lot of confusing information on how to work with these cross-domain iframes which I couldn't really understand or am just unable to execute.我阅读了很多关于如何使用这些我无法真正理解或无法执行的跨域 iframe 的令人困惑的信息。

As far as I know I should be able to simply inject my script directly in the iframe and get the wanted result.据我所知,我应该能够简单地将我的脚本直接注入 iframe 并获得想要的结果。 But I haven't been able to do so.但我一直无法这样做。 I didn't find any helpful information on how to do that with an WebExtension.我没有找到任何有用的信息来说明如何使用 WebExtension 做到这一点。 I would really appreciate if someone could help me figure this out.如果有人能帮我解决这个问题,我将不胜感激。

Maybe my idea is completly wrong and I need to try something else like using post message (which I didn't understand how to implement either) or something completly different.也许我的想法是完全错误的,我需要尝试其他的东西,比如使用 post message(我也不明白如何实现)或完全不同的东西。 So anyways, I am looking forward to your replies.所以无论如何,我期待着你的回复。

As far as I know I should be able to simply inject my script directly in the iframe and get the wanted result.据我所知,我应该能够简单地将我的脚本直接注入 iframe 并获得想要的结果。 But I haven't been able to do so.但我一直无法这样做。

iframe is just like a web page and you can inject scripts into it like any web page. iframe 就像 web 页面一样,您可以像任何 web 页面一样将脚本注入其中。

There are the following considerations:有以下考虑:

  • Extension must have to correct Permission for iframe (which maybe from a different domain)扩展必须更正 iframe 的Permission (可能来自不同的域)
  • Setting all_frames: true (default is false) in content_scriptscontent_scripts中设置all_frames: true (默认为 false)

If you are injecting script by other method than manifest.json , let me know and I explain that too.如果您通过manifest.json以外的其他方法注入脚本,请告诉我,我也会解释。

I'm so sorry, but it is not posible to modify one website on an iframe if this iframe contains a website from other domain.很抱歉,如果 iframe 包含来自其他域的网站,则无法在 iframe 上修改一个网站。

Maybe, you founded some examples with java script, but if you see this examples in deep you'll find that they works because the domain is always the same...也许,您使用 java 脚本创建了一些示例,但如果您深入了解这些示例,您会发现它们有效,因为域始终相同......

The different origin forbiden this actions.异源禁止这种行为。

Solution解决方案

Thanks to erosman 's comment I looked at my manifest.json again and played around a bit to finally get my script to work the way I wanted.感谢erosman的评论,我再次查看了manifest.json并进行了一些尝试,最终让我的脚本按照我想要的方式工作。 My problem was actually in wrong URL-patterns I provided.我的问题实际上出在我提供的错误 URL 模式中。 For anyone having a similar problem like me I will provide what would've saved my past me a lot of time.对于像我这样有类似问题的人,我将提供可以为我过去节省很多时间的方法。

Like erosman says:就像爱罗斯曼说的:

iframe is just like a web page and you can inject scripts into it like any web page. iframe 就像 web 页面一样,您可以像任何 web 页面一样将脚本注入其中。

Since I don't care about accessing the iframe's content from the parent document this means I can indeed just inject my script into the iframe directly and let it work there.由于我不关心从父文档访问 iframe 的内容,这意味着我确实可以直接将我的脚本注入 iframe 并让它在那里工作。 The result will be visible on the webpage containing the iframe.结果将显示在包含 iframe 的网页上。 In terms of Firefox WebExtensions this simply means setting the right parameters in your manifest.json .就 Firefox WebExtensions 而言,这仅意味着在manifest.json中设置正确的参数。 In content_scripts set "all_frames": true to allow your script to get injected into frames.content_scripts中设置"all_frames": true以允许您的脚本被注入到帧中。 Now you have to provide the URL of the webpage displaying the iframe aswell as the iframe's URL.现在您必须提供显示 iframe 以及 iframe 的 URL 的网页的 URL。 Your content_scripts should look like:您的content_scripts应如下所示:

    "content_scripts": [
        {
         "matches": ["*://*.webpage.org/*", "*://*.iframe.com/*"],
         "all_frames": true,
         "js": ["script.js"]
        }
     ]

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

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