所有类型的Chrome扩展脚本如何工作? [英] How do all types of Chrome extension scripts work?

查看:111
本文介绍了所有类型的Chrome扩展脚本如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Chrome扩展程序,并希望了解所有类型的脚本/页面是如何工作的。



这是我的理解:



首先 - 有内容脚本,应该用于实际修改页面。

第二 - 有一个后台脚本,设计用来像你发送和接收消息的服务器一样工作,但不会修改页面的DOM;所以它可以执行诸如处理存储和脚本之间的通信但不修改页面的任务。
$ b 最后 - 有弹出脚本,它们与内容脚本和后台脚本分开,但您仍然可以在它们之间发送/接收消息。

弹出式脚本不能直接修改页面(与后台脚本相同),它们只能将消息发送给其他两个。

您不在manifest文件中声明它们,您可以只需直接在弹出的html文件中使用它们即可。



最终,只有内容脚本才能最终实际修改页面。



我是否正确?

解决方案

文档链接来统一他们所有的,一个链接找到他们,

一个链接,使他们都在黑暗中 bind()他们 1



:每个扩展仅存在一个页面。它是不可见的,永远不会显示在选项卡中。通常情况下,只要Chrome打开,它就会打开,但也有例外。由于它始终存在并且具有访问Chrome API的最高级别,因此它经常用于扩展各部分之间的主逻辑/事件路由。总之,后台工作



活动页面/脚本如果没有代码在运行,则卸载后台页面的变体。这样可以节省内存,但会增加维护状态的复杂性。 Chrome会记住应该侦听哪些事件(通过 addListener ),并在页面发生时再次加载页面。因此,活动页面



除此之外,扩展程序还可以有其他可见页面。您可以在选项卡中打开它们(它们将具有 chrome-extension://extensionidgoeshere/page.html 地址),并且它们将具有相同级别的Chrome API访问权限。两种UI类型对于扩展来说是特殊的:

浏览器 / 页面操作弹出窗口:一个小窗口只需点击相应的UI元素即可打开。不幸的是,它也非常脆弱 - 只要失去了重点,它就会关闭。除此之外,它只是一个扩展页面。



选项页面:有两种口味。 版本1选项页面只是调用扩展选项时打开的选项卡; 版本2选项页面可以选择显示在 chrome内的特殊框中: //扩展/ 。另外,除此之外,它只是一个具有扩展权限的页面。



最后,拥有一堆页面很有趣,但如果您想与现有页面/选项卡进行交互,你需要在它们中注入脚本。



内容脚本 是与页面一起运行的脚本;出于兼容性原因,他们在孤立的世界中运行。出于安全原因,他们在访问Chrome API方面受到严重限制。但是它们与页面共享DOM,因此可以修改它。



页面级脚本是您在文档中几乎找不到的东西(作为DOM注入脚本),但它们对于打破屏障非常有用在扩展JavaScript和页面自己的JavaScript之间。对这个答案中的宏伟这个答案(再次由Rob W)和消息文档






1 尽管如此,每个开始的扩展开发者都需要阅读它,而这个页面并不是突出的文件。好,Google。


I'm new to Chrome extensions and would like to understand how all types of scripts/pages work.

Here is my understanding:

First - there are "content scripts", ones that should be used to actually modify the pages.

Second - there is a "background script", designed to work like as a server that you send and receive messages from, but it does not modify the pages' DOM; so it can perform tasks like dealing with storage and communicating between scripts but not modifying the page.

Lastly - there are "popup scripts", they are separated from both content scripts and background scripts, but you can still send/receive messages between them.
The popup scripts can NOT directly modify the page (same as background script), they can only send messages to the other two.
You do not declare them at all in the manifest file, you can just go straight and use them in your popup html file.

In the end only the content scripts can eventually actually modify a page.

Am I correct?

解决方案

One Chrome extension documentation Link to rule them all, One Link to find them,
One Link to bring them all and in the darkness bind() them1:

>> Architecture Overview <<

(artist's impression)

It should answer many of your questions. However, that would be a bad SO answer, so a summary from me:

Background page/scripts: Only one page exists per extension. It is invisible and can never be displayed in a tab. Normally, it is open as long as Chrome is open, though there are exceptions. Since it's always there and has the highest level of access to Chrome APIs, it's frequently used for main logic / event routing between parts of the extension. In short, background work.

Event page/scripts: A variant of background pages that are unloaded if there is no code running. This saves memory, but introduces complexity as to maintaining state. Chrome remembers which events should be listened to (via addListener) and loads the page again when they happen. Hence, event page.

Besides that, extension can have other, visible pages. You can just open them in a tab (they would have chrome-extension://extensionidgoeshere/page.html address), and they will have same level of access to Chrome API. Two UI types are special to extensions though:

Browser/Page Action popup: A small window that's opened with a click on the corresponding UI element. Unfortunately, it's also very fragile - it will close as soon as it loses focus. Other than that, it's just an extension page.

Options page: Comes in two flavours. Version 1 Options page is just a tab that's opened when invoking options for an extension; Version 2 Options page can optionally show in a special box inside chrome://extensions/. Again, other than that it's just a page with extension privileges.

Finally, having a bunch of pages is fun, but if you want to interact with existing pages/tabs, you'll need to inject scripts in them.

Content Scripts are scripts that run alongside pages; for compatibility reasons, they run in an isolated world. For security reasons, they are severely limited in access to Chrome API. But they share the DOM with the page, and as such can modify it.

Page-level Scripts is something you barely find mentioned in documentation (as "DOM injected scripts"), but they are very useful to break the barrier between extension JavaScript and page's own JavaScript. A good overview of them is presented in this answer by the magnificent Rob W.

Having defined all relevant extension parts, the documentation page also briefly mentions how to communicate between them. For a more in-depth look into this aspect, see this answer (again by Rob W) and the Messaging documentation.


1 Seriously though, every beginning extension developer needs to read that, and this page is not prominent the documentation. Good job, Google.

这篇关于所有类型的Chrome扩展脚本如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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