Skip to content

A simple polyfill, based on Javascript Proxy, for Promise based WebExtension.

License

Notifications You must be signed in to change notification settings

raffaeleflorio/webextension-browser-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 

Repository files navigation

webextension-browser-proxy

webextension-browser-proxy allows extensions written with standardized (https://browserext.github.io/browserext/) Promise based WebExtension API to work without modification in Chrome/Chromium. The latter use a callback based API.

How to use

You can download directly polyfill.js or you can clone this repo and check signauture's commit. Public Key from https://raffaeleflorio.github.io or https://pgp.mit.edu).

Then you have to include polyfill.js before any browser APIs are used.

For background scripts or content_scripts add polyfill.js entry in manifest.json:

// ...
	"background": {
		      "scripts": [
		      		 "path/to/polyfill.js",
				 "background_script1.js",
				 "background_script2.js",
				 // ...
		      ]
	},

	"content_scripts": [{
			   // ...
			   "js": [
			   	 "path/to/polyfill.js",
				 "content_script1.js",
				 "content_script2.js",
				 // ...
			   ]
	}]
// ...

For HTML files include it with script tag before any script that use browser API is used:

<!DOCTYPE html>
	  <!-- ... -->
		<script src="path/to/polyfill.js"></script>
		<script src="other_script1.js"></script>
		<script src="other_script2.js"></script>
	  <!-- ... -->
</html>

Then you can use Promise based API.

Where it has been tested

It has been tested on Chrome/Chromium browser, with these Promise based extensions:

How it works

polyfill.js is an IIFE. It defines browser as Proxy of chrome ([0]). Furthermore it adds a isPolyfilled property to the browser object. The Proxy handles three cases when a browser's property (or nested object's property) is accessed ([1]):

  1. If the property is an object, it returns a Proxy with handler ([4]) as handler.
  2. If the property is a function, it returns a function ([2]) (read below).
  3. Otherwise it returns the target property.

How returned function works

The returned function try to calls the target function in this way:

  1. If the target function accepts a callback, it returns a Promise resolved by [3].
  2. If the target function accepts (or not) some parameters, it returns the target function returned value.

[0] = polyfill.js#L46
[1] = polyfill.js#L2
[2] = polyfill.js#L7
[3] = polyfill.js#L14
[4] = polyfill.js#L5