Remote Procedure Call implementation for WebExtensions, to easily call functions across content scripts and background script.

Readme.md 2.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # webextension-rpc
  2. This module provides a *Remote Procedure Call* abstraction around the message passing that is
  3. available to WebExtension (browser extension) scripts. It makes it easier to call a function in the
  4. background script from a tab’s content script, or vice versa.
  5. ## Example use
  6. In `background.js`:
  7. import { RpcServer } from 'webextension-rpc'
  8. async function myFunc(arg) {
  9. return arg * 2
  10. }
  11. new RpcServer({ myFunc })
  12. In `content_script.js`:
  13. import { RpcClient } from 'webextension-rpc'
  14. const myRemoteFunc = new RpcClient().func('myFunc')
  15. await myRemoteFunc(21) // 42!
  16. Note that the remote function always returns a `Promise`, which resolves with the remote function’s
  17. actual return value (if the return value is itself a Promise, its result is awaited too).
  18. ## Use in TypeScript
  19. When used TypeScript, the type of a remote/proxy function is nearly equal to its original, and can
  20. be derived from it automatically. For an example how to do this, see the `example` folder.
  21. ## Install
  22. ### Using NPM
  23. This module is published [on npm](https://www.npmjs.com/package/webextension-rpc), as an ES module.
  24. Run `npm install webextension-rpc` or equivalent, and in your code import what you need, e.g.:
  25. import { RpcClient, RpcServer } from 'webextension-rpc'
  26. ## API
  27. ### RpcClient
  28. #### `new RpcClient(options?)` (constructor)
  29. Instantiate the RpcClient.
  30. Arguments:
  31. - `options` (object, optional):
  32. - `options.tabId` (number): The id of the tab whose content script is the remote side. Leave undefined or
  33. null to invoke functions in the background script (from a content script).
  34. #### `func(functionName, options?)`
  35. Create a proxy function that invokes the specified remote function.
  36. Arguments:
  37. - `functionName` (string, required): name of the function as registered on the remote side.
  38. - `options` (object, optional): override any options passed to the constructor.
  39. ### RpcServer
  40. #### `new RpcServer(functions)` (constructor)
  41. Register one or more functions to enable remote scripts to call them.
  42. Arguments:
  43. - `functions` (object, required): An object with a `{ functionName: function }` mapping. Each
  44. function will be remotely callable using the given name.
  45. ### `injectRpcInfo`
  46. If the special symbol `injectRpcInfo` is passed as the first argument to a proxy function, this
  47. argument will be replaced on the executing side by an `RpcInfo` object, which contains the following
  48. attributes:
  49. - `tab`: the [`Tab`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/Tab),
  50. from which the call was made, if it was made by a content script.
  51. [Tab]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/Tab