Fix injecting JS plugins, remove webcomponents-lite.js

Loading `webcomponents-lite.js` as a content script is problematic,
because it does not have proper access to the `window` object of the
page. It also seemed a bit ugly, because Gerrit would load it again
itself, only too late.

We are now taking a different approach. We are moving all the custom
elements that we want to use into a separate `elements.js` bundle,
and load that script "normally" from a <script> tag in the header,
exactly as we do for injected js plugins.

Change-Id: I5662989b115a57e751842f8b19afacb33433e5f1
7 files changed
tree: 2a6053d62e773ffe57a389a575d6a3e87bd0b798
  1. data/
  2. src/
  3. webpack/
  4. .gitignore
  5. .prettierrc
  6. demo.png
  7. package-lock.json
  8. package.json
  9. README.md
  10. release-notes.md
  11. tsconfig.json
README.md

Gerrit FE Dev Helper

Gerrit FE Dev helper is a Chrome extension that will focus on helping frontend developers on Gerrit development.

As mentioned in readme from polygerrit-ui, we already support to start your local host for developing / debugging / testing, but it has quite a few restrictions:

  1. No auth support
  2. Restart needed to switch hosts
  3. Not easy to test plugins

To solve these pain points, Tao Zhou created this Chrome extension. It proxies all assets requests or any requests to a local HTTP server, and has the ability to inject any plugins exposed by the local HTTP server.

Features

See in release notes and Features below.

Install

The easiest is to install from the Chrome web store here: https://chrome.google.com/webstore/detail/gerrit-fe-dev-helper/jimgomcnodkialnpmienbomamgomglkd.

After you have installed and enabled the extension, you should see something similar to demo.png.

BUILD

To build from source:

npm install
npm run build

Then you should have gerrit_fe_dev_helper.zip that you can test with.

How to use

  1. For Gerrit core development, start the local Gerrit dev server to host app code locally
yarn start

Or if you are developing a plugin, serve your plugin via a local HTTP server via any means.

Example:

npx http-server -c-1 --cors
  1. Go to any Gerrit sites, enable the extension by clicking the icon
  2. You should see a red notice show up in the bottom right of the page: Gerrit dev helper is enabled, and now your Gerrit assets should be loaded from your local HTTP server
  3. Change files locally and refresh the page, you should have the changes immediately

The extension comes with a set of default rules, but you can change the rules by clicking the extension icon again.

The extension supports six different type of rules:

  1. block: block a certain request
  2. redirect: redirect a url to another url
  3. injectHtmlCode: inject a piece of html code to the page
  4. injectJsPlugin: inject a js plugin(url) to the site
  5. injectJsModule: inject a js module file to the site (type=“module” will be added when load script)
  6. addReqHeader: to add arbitrary header when you send a request
  7. addRespHeader: to add arbitrary header when you receive a request
  8. rRespHeader: to remove arbitrary header on any response

The option to inject any plugins (injectJsPlugin) is meant to help you develop your plugins for your Gerrit sites. As they are served from your local HTTP server, you do not need to deploy them on the target Gerrit server.

How to use dev helper with js plugins

For single-file js plugins, use injectJsPlugin rule or use redirect if it is an exising js plugin.

For multi-file modularized js plugins (you have import / export in source code), you have two options:

  1. compile them and then treat it as single-file js plugin
  2. or if you want to load source code as it is
  • use injectJsModule, this will load the js with type="module", and due to restriction of type="module", Gerrit won't be able to recognize the plugin without a proper url set when calling Gerrit.install, so you also need to tweak your code to call Gerrit.install(callback, undefined, 'http://localhost:8081/plugins_/checks/gr-checks/gr-checks.js') to let Gerrit treat it as a legit plugin.

Either way, you need to block the existing plugin if its already on the page.

Testing a new version

  • Execute npm run build.
  • Go to chrome://extensions/.
  • Turn on Developer Mode.
  • Click Load Unpacked.
  • Choose the dist directory.

As a Google developer you will have to add a key to the manifest.json in the dist/ directory as documented here: http://go/extension-identification#i%E2%80%99m-developing-a-chrome-extension-on-my-computer

Publish a new version to the Chrome Webstore

This section is for members of Google's developer team only.

Contact

Please don't hesitate to contact dhruvsri@google.com for support on this extension.