Skip to content

Inject into other extensions

Dahan Gong edited this page Apr 5, 2019 · 9 revisions

(This page is only for advanced users)

Vimium C is carefully designed and provides a convenient injector to run onto pages of another extension, as soon as the extension declares that itself has Vimium C supports.

So here's a tutorial about how to create such an extension (e.g. the official PDF Viewer).

Prepare sources

  1. Install Vimium C and PDF Viewer on your browser
  2. Go to Chrome's User Data folder, which may be "C:\Users\<your username>\AppData\Local\Google\Chrome\User Data" on Windows
  3. search the folder "Default" for a folder named "oemmndcbldboiebfnladdacbdfmadadm", copy it's subfolder (e.g. named "2.0.673_0") to somewhere others for further work

Modify HTML pages of the extension

  1. search the folder for .html and .html files (e.g. content/web/viewer.html)
  2. and open them using a good text editor / IDE (like Notepad++ and Visual Studio Code)
  3. find "</head>", and if the text doesn't exist, find "</body>" or even "</html>"
  4. insert a line of <script type="text/javascript" src="chrome-extension://hfjbmagddngcpeloejdejnfgbamkjaeg/lib/injector.js"></script> before the found position
  • if Vimium C has a different ID (the options page is not chrome-extension://hfjbmagddngcpeloejdejnfgbamkjaeg/pages/options.html)
  • you will need to modify the URL above and replace the URL's origin part.
  1. repeat the step above on all HTML files, and save all changes

Modify extension properties (only required on Chrome)

  1. open manifest.json using your text editor
  2. find the line containing "content_security_policy", and insert " chrome-extension://hfjbmagddngcpeloejdejnfgbamkjaeg/" (note the beginning space character) between "script-src ..." and the next ";"
  3. if there's no such a line containing "content_security_policy", then just insert a line of "content_security_policy": "script-src 'self' chrome-extension://hfjbmagddngcpeloejdejnfgbamkjaeg/; object-src 'self'", after the first "{" character
  4. the CSP rule above is required since Chrome 56, or external scripts wouldn't run on injected extensions
  5. delete a line looking like "key": "...", (to avoid ID conflicts with the original extension)
  6. save manifest.json
  7. delete the folder _metadata because now the folder cann't pass Chrome's file content verification

Load the modified extension and enable injection

  1. open chrome://extension, and click the switcher of "Developer mode" (in Chinese, it's "开发者模式")
  2. click the newly visible button "Load unpacked extension..." and choose the work folder to load it as an extension
  3. now Chrome will assign a new unique ID for the new extension, which is shown on chrome://extension, and remember it (e.g. "nacjakoppgmdcpemlfnfegmlhipddanj")
  4. go to the Vimium C Options page, in advanced options add the ID into the "White list of other extension IDs", and then save changes
  5. open pages of the new extension and now Vimium C should work perfectly.
  6. if the modified extension is PDF Viewer, then just open a PDF file and Vimium C will be able to mark most buttons

Notes for Firefox

Firefox generates a different UUID for the same extension everytime you install it, making the URL of "injector.js" is uncertain. Therefore, Vimium C puts its UUID into the response for message "99" on Firefox since version 1.74.

browser.runtime.sendMessage('vimium-c@gdh1995.cn', { handler: 99 }).then(res => {
  const { host } = res;
  console.log(host); // for example, log "d1ba02e9-ec90-46dc-8623-69e15961cc86"
  // localStorage.vimiumHost = host; // may cache it for further usages
  const injector = `moz-extension://${host}/lib/injector.js`;
  const script = document.createElement('script');
  script.src = injector;
  document.head.appendChild(script);
});

Some comments

The key changes are: <script> in HTML files, changing CSP and removing "key": line in manifest.json, and add wanted extension IDs into Vimium C's white list for injection.

Usually Vimium C works perfectly on extension environments, but if any bug or crash is found, please file an issue on https://github.com/gdh1995/vimium-c/issues . Thanks for your help.

For example, a modified viewer.html from PDF Viewer looks like: modified result of viewer.html

And a modified manifest.json from X New Tab looks like: modified result of manifest.json

Clone this wiki locally