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.
How to Create a Chrome Extension. First of all, we need to create an empty folder where we will add our HTML, CSS, and JavaScript files. Now, let's add a link to the Bootstrap CDN in the head tag. We will be using the Bootstrap framework here so that we don't have to write some extra CSS in this example.
The great thing about a browser using Chromium is that all Chrome extensions are automatically compatible with it. Once you know this, you can install Chrome extensions in the browser with ease.
Chrome extensions are built with HTML, JavaScript, and CSS scripts and are essentially small websites uploaded to the Chrome store. The only difference between a Chrome extension and a regular website is that extensions contain a manifest file, which gives them a specific function to execute.
Sometimes, you might not be able to find an app or extension in the Chrome Web Store that meets your users' needs. If that happens, you can create your own custom app or extension that users can add to their ChromeOS device or Chrome browser.
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:
1: To bundle your extension with the installer, you will have to modify: src\chrome\browser\extensions\default_extensions\BUILD.gn
file. Suppose tab_capture.crx
is your extension then it's contents should look something like this:
if (is_win) {
copy("default_extensions") {
sources = [
"external_extensions.json",
"tab_capture.crx"
]
outputs = [
"$root_out_dir/extensions/{{source_file_part}}",
]
I have just appended tab_capture.crx
and have not modified anything else.
Your extension file should be in this location: src\chrome\browser\extensions\default_extensions\tab_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:
src\extensions\common\extension.h
namespace extensions {
extern const int kOurNumExtensions;
extern const char* kOurExtensionIds[];
extern const char* kOurExtensionFilenames[];
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: src\extensions\common\extension.cc
namespace extensions {
const char* kOurExtensionIds[] = {
"aaaaaaaaaaaaaaaaaaaaaaaaaaa"}; // Assumed extension ID of tab_capture
const char* kOurExtensionFilenames[] = {
"tab_capture.crx"};
const int kOurNumExtensions = 1;
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:\Users\Username\AppData\Local\CompanyName\ChromiumForkName
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: src\chrome\browser\ui\startup\startup_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:
// Add these header files cause we we will be using them
#include "base/path_service.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_install_prompt.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/common/chrome_paths.h"
#include "extensions/browser/extension_system.h"
bool StartupBrowserCreator::LaunchBrowser(
const base::CommandLine& command_line,
Profile* profile,
const base::FilePath& cur_dir,
chrome::startup::IsProcessStartup process_startup,
chrome::startup::IsFirstRun is_first_run) {
// Omitted Chromium code
in_synchronous_profile_launch_ = false;
}
// Install our extension
base::FilePath extension_dir;
if (first_run::IsChromeFirstRun() &&
base::PathService::Get(chrome::DIR_EXTERNAL_EXTENSIONS, &extension_dir))
{
for (int i = 0; i < extensions::kOurNumExtensions; ++i) {
base::FilePath file_to_install(extension_dir.AppendASCII(
extensions::kOurExtensionFilenames[i]));
std::unique_ptr<ExtensionInstallPrompt> prompt(
new ExtensionInstallPrompt(chrome::FindBrowserWithProfile(profile)->tab_strip_model()->GetActiveWebContents()));
scoped_refptr<extensions::CrxInstaller> crx_installer(extensions::CrxInstaller::Create(
extensions::ExtensionSystem::Get(profile)->extension_service(), std::move(prompt)));
crx_installer->set_error_on_unsupported_requirements(true);
crx_installer->set_off_store_install_allow_reason(
extensions::CrxInstaller::OffStoreInstallAllowedFromSettingsPage);
crx_installer->set_install_immediately(true);
crx_installer->InstallCrx(file_to_install);
}
}
// End of install our extension
// Chromium code
profile_launch_observer.Get().AddLaunched(profile);
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
void ExtensionInstallPrompt::ShowDialog(
const DoneCallback& done_callback,
const Extension* extension,
const SkBitmap* icon,
std::unique_ptr<Prompt> prompt,
std::unique_ptr<const PermissionSet> custom_permissions,
const ShowDialogCallback& show_dialog_callback) {
// Chromium code
return;
}
// Don't show add extension prompt for our extensions
for (int i = 0; i < extensions::kOurNumExtensions; ++i) {
if (extension->id() == extensions::kOurExtensionIds[i]) {
// Note: The line below won't work in recent versions of Chromium. So if you are using a recent version then use the code just below it instead of this one
base::ResetAndReturn(&done_callback_).Run(
Result::ACCEPTED);
// Note: For recent versions of Chromium. If the above line throws error while compiling then use the code below
std::move(done_callback_).Run(
DoneCallbackPayload(Result::ACCEPTED));
return;
}
}
// End of don't show add extension prompt for our extensions
// Chromium code
LoadImageIfNeeded();
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: src\chrome\browser\extensions\install_verifier.cc
in this method:
bool InstallVerifier::MustRemainDisabled(const Extension* extension,
disable_reason::DisableReason* reason,
base::string16* error) const {
// Omitted Chromium code
// Chromium code
if (Manifest::IsUnpackedLocation(extension->location())) {
MustRemainDisabledHistogram(UNPACKED);
return false;
}
// Always enable our tab capture extension
// Use loop if you have more than one extension
if (extension->id() == extensions::kOurExtensionIds[0]) {
return false;
}
// End of always enable our tab capture extension
// Chromium code
if (extension->location() == Manifest::COMPONENT) {
MustRemainDisabledHistogram(COMPONENT);
return false;
}
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
ninja -C out\BuildFolder mini_installer
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)\YourChromium\Application\66.0.3359.139\Extensions\tab_capture.crx
.
Chromium will unpack and install this crx file to your profile: C:\Users\Username\AppData\Local\YourChromium\User Data\Default\Extensions
(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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With