问题描述
我已经构建了一个 Chrome 扩展程序,我已经使用 Selenium 将其安装到 Chrome 中.
现在我想从源代码构建我自己的 Chromium,这样我的扩展就被预先捆绑到构建的分布式包中,这样我就不必担心需要 Selenium 为我的用例安装 CRX 文件.
我找到了几个论坛,人们建议他们尝试这个,但没有一个最终看起来像他们成功.
我发现了一些关于系统管理员如何为网络中的用户强制将扩展安装到 Chrome 中的提示:
3: 现在,我们将开始修改 C++ 源文件.让我们声明我们的扩展名和 ID.我们将在这些文件中这样做:srcextensionscommonextension.h
我刚刚在 extensions
命名空间下声明了这些变量.请记住,我们在下面分配的扩展 ID 必须与 Chromium 分配的扩展 ID 匹配.
那些变量的定义在:srcextensionscommonextension.cc
Chromium 将在首次启动时创建配置文件.所以我们假设还没有配置文件存在,因为我们将在它第一次启动时在运行时安装我们的扩展.Windows 计算机上的配置文件通常应存在于此处:C:UsersUsernameAppDataLocalCompanyNameChromiumForkName
,因此请确保在启动 Chromium 之前删除 CompanyName
文件夹.当然,我们也可以在创建配置文件后执行安装过程.为此,您必须检查我们的扩展程序是否已安装,以防止多次尝试安装.
Chromium 在这个文件中处理启动浏览器的创建内容:srcchromerowseruistartupstartup_browser_creator.cc
所以我们在配置文件初始化和浏览器启动后安装这个扩展.您还必须添加一些头文件.我们将在 LaunchBrowser
方法中这样做:
这应该会安装我们的扩展程序,但由于我们希望在没有任何用户交互的情况下强制安装我们的扩展程序,让我们在这里进行:chrome/browser/extensions/extension_install_prompt.cc
4:即使我们自动执行安装过程,Chromium 也会禁用我们的扩展程序,因为它不是从 Chrome 网上应用店安装的.在这里处理:srcchromerowserextensionsinstall_verifier.cc
在这个方法中:
这将确保在我们绕过 Chrome 网上应用店检查时启用我们的扩展程序.
如果您不希望您的扩展程序被卸载并保持启用状态,那么您可以通过修改此文件来实现:chrome/browser/extensions/standard_management_policy_provider.cc
并修改这些方法:MustRemainInstalled
和 MustRemainEnabled
5:现在您可以通过执行此命令来构建迷你安装程序
以上命令将构建mini_installer.exe
.注意如果您将 --system-level
参数传递给 mini_installer.exe
那么它应该在 Program files
中安装您的 Chromium fork代码>文件夹.安装完成后,您的 crx 文件应位于:C:Program Files (x86)YourChromiumApplication66.0.3359.139Extensions ab_capture.crx
.
Chromium 将解压此 crx 文件并将其安装到您的配置文件中:C:UsersUsernameAppDataLocalYourChromiumUser DataDefaultExtensions
(假定的默认配置文件)
注意:为了提高代码可读性和易用性,您可以使用容器类来保存这些扩展文件名及其对应的 ID,并在基于范围的 for 循环中轻松使用.
让我知道它是否有效.花费的时间比预期的要长,因为我注意到他们的代码库中有很多变化,而且我们的旧代码在最新的 Chromium 版本中不起作用.我敢肯定,我没有错过任何其他东西:)
I have built a Chrome extension that I have been installing into Chrome using Selenium.
Now I would like to build my own Chromium from source so that my extension is pre-bundled into the built distributed package so that I don't have to worry about needing Selenium to install the CRX file for my use case.
I have found several forums where people suggested they were going to try this, but none of them ended up seeming like they were successful.
I found some tips on how a system administrator can force install extensions into chromium for users in their network: https://support.google.com/chrome/a/answer/6306504?hl=en But that is for chrome enterprise, probably not going to be useful for me.
Here is another post which talks about how to offline install chrome extensions. I might be able to use some of this to make what I want happen.
Has anyone had success actually building into chromium a CRX so that the CRX is just installed automatically?
Quick update:
I just want to note: I'm installing my custom version of chrome with an InnoSetup installer. So I do have the chance to, after my chromium fork is installed, do some custom execution steps post install. And my extensions are hosted on the chrome web store and approved.
So if there is some way to programmatically install chrome extensions into a Chromium installation from the web store, I would could easily use that.
This has been tested in our Chromium fork versions 66.0.3359.139 to 7x.x.x
on Windows 10. The extension bundling process might be different for Linux and macOS. I have also tried to make it as easy as possible to accomplish this task. There are a couple of things you will have to do to accomplish this:
- Add your Chromium extension (.crx) file to a list of default extensions to bundle with mini installer
- Find out the ID of that extension
- Automate extension installation process
- By pass Chrome Web Store check
- Build mini installer to install your Chromium fork
1: To bundle your extension with the installer, you will have to modify: srcchromerowserextensionsdefault_extensionsBUILD.gn
file. Suppose tab_capture.crx
is your extension then it's contents should look something like this:
I have just appended tab_capture.crx
and have not modified anything else.
Your extension file should be in this location: srcchromerowserextensionsdefault_extensions ab_capture.crx
2: Each extension will have a unique ID assigned to it by Chromium to identify that extension. To find out the ID of your extension you should go to chrome://extensions/
page and drag and drop your crx
file. A confirmation dialog box should popup. Click Add extension
button and make sure Developer mode
is enabled then your ID should be visible but the extension will be disabled as shown below:
3: Now, we will start modifying C++ source files. Let's declare our extension's name and ID. We will do so in these files:
srcextensionscommonextension.h
I have just declared those variables below extensions
namespace. Remember, extension ID that we are assigning below must match with the extension ID assigned by Chromium.
Those variables' definition in: srcextensionscommonextension.cc
Chromium will create a profile when it's first launched. So we assume no profile exists yet because we will install our extension at run time when it's first launched. A profile on a Windows machine should typically exists here: C:UsersUsernameAppDataLocalCompanyNameChromiumForkName
so make sure to delete CompanyName
folder before launching Chromium. Of course, we can do the installation process after a profile has been created too. For that you will have to check if our extension has been installed or not to prevent multiple attempts of installation.
Chromium handles startup browser creation stuff in this file: srcchromerowseruistartupstartup_browser_creator.cc
so we install this extension after a profile has been initialized and browser has been launched. You will have to add some header files too. We will do so in LaunchBrowser
method:
That should install our extension but as we want our extension to be forcefully installed without any user interaction, let's do it here: chrome/browser/extensions/extension_install_prompt.cc
4: Even if we automate the installation process, Chromium will disable our extension cause it was not installed from Chrome Web Store. It's handled here: srcchromerowserextensionsinstall_verifier.cc
in this method:
This will ensure that our extension will be enabled as we are bypassing Chrome Web Store check.
If you don't want your extension to be uninstalled and remain enabled then you can do so by modifying this file: chrome/browser/extensions/standard_management_policy_provider.cc
and modify these methods: MustRemainInstalled
and MustRemainEnabled
5: Now you can build mini installer by executing this command
The above command will build mini_installer.exe
. Note If you pass --system-level
argument to mini_installer.exe
then it should install your Chromium fork in Program files
folder. After the installation is complete your crx file should be located here: C:Program Files (x86)YourChromiumApplication66.0.3359.139Extensions ab_capture.crx
.
Chromium will unpack and install this crx file to your profile: C:UsersUsernameAppDataLocalYourChromiumUser DataDefaultExtensions
(Assumed default profile)
Note: To improve code readability and ease of use, you could use container classes to hold those extension file names and their corresponding IDs and use it easily in a range based for loop.
Let me know if it works. Took longer than expected cause I noticed lots of changes in their code base and our old code was not working in this latest Chromium build. I am sure, I have not missed anything else :)
这篇关于在 Chromium 中构建 Chrome 扩展的标准方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!