To run the generator, you must clone it.
From the terminal cd into the chrome-extension-generator directory and run:
npm install && npm linkThis will give you global access to the generator.
Now go to the root directory where the project will be created and run:
create-chrome-extensionThis will run the generator (provide inputs as required).
Once the project has been built, you will be prompted to run commands to install dependencies and run linters.
When in the extension directory, open in VS Code with:
code .(Here is how to set up the VS Code command line interface)
To add the new repo to GitHub using Git
- Create the new repo
- Follow instructions (copy and paste code suggested in GitHub)
You can import scripts with static ES modules using the usual import syntax.
Content scripts do not support ES modules. You need to declare your functions in the window global namespace. Here is how to do it:
- Declare all the files in the manifest in order of dependency, e.g.
"content_scripts": [ { "matches": ["<all_urls>"], "js": [ "content_scripts/utils.js", "content_scripts/content-script.js" ] } ]
- In
utils.js, declare all the functions you want to use incontent-script.jsin thewindowglobal namespace, e.g.window.myUsefulFunction = function () { // Your code here }
- Call the function in
content-script.jsfrom thewindownamespace, e.g.const result = window.myUsefulFunction()
- Make sure your manifest has the right permissions for what you're trying to achieve. Error messages don't explicitely mention that you're missing a permission.
- Once you've (re)loaded your extension locally, try to reload the web page you're working on, that may solve some communication issues between the service worker and the content script.
- If you have weird errors, don't hesitate to
RemoveandLoad Unpackedthe extension again, particularly if you've modified the manifest.
You can use your extension locally in Chrome (or Chromium-based browsers such as Brave) as follows:
- Go to the menu (burger or three dots in the top right corner) then
Extensions > Manage Extensions. - Make sure the
Developer Modeis activated (toggle in the top right corner) - Click
Load Unpackedand point to the directory hosting your extension code (by defaultpublish, it's the directory containing yourmanifest.json)
We've written a short article about the main steps and pitfalls to publishing your extension on the Chrome Web Store.