Skip to content

External Message API

Gold Holk edited this page Dec 9, 2022 · 7 revisions

External Message API

Webscrapbook allow other extensions to trigger capturing with send message. Two type of messages are allowed: {cmd: "invokeCapture"} and {cmd: "invokeCaptureEx"} (defined in onMessageExternal event). Both of them require arguments in args property. To send message to webscrapbook, an extension should call browser.runtime.sendMessage with webscrapbook's extension id as the first argument.

Extension Id

Extension id can be found in about:debugging#/runtime/this-firefox (firefox) or chrome://extensions in developer mode (chromium). Firefox allow author to specify extension id in its manifest, so webscrapbook's id should always be [email protected] (defined in webscrapbook's manifest). Chromium will always generate a random id while installation.

Extensions can look up other extensions' id from WebExtensions management.getAll api. This requires a management permission in manifest. Note webscrapbook's ExtensionInfo is localized, so you may need to test on the non-localized properties like homepageUrl.

const addons = await browser.management.getAll();
const webscrapbook = addons.find(info =>
    /danny0838.webscrapbook/.test(info.homepageUrl)
);
const id = webscrapbook.id;

Invoke Capture

A message with cmd: 'invokeCapture' property should specify the tabs to captured with its tab id, and webscrapbook will capture the tabs in default options. The args should be a array of objects which hold a integer tabId property.

While capturing, webscrapbook will open a capture window, and capture every tab specified in args in sequence in that capture window.

Example

This example will capture the current focused tab. Note the browser.tabs api will not work in [content script].

const tabs = await browser.tabs.query({active: true, currentWindow: true});
const tabId = tabs[0].id;
await browser.runtime.sendMessage("[email protected]", {
  cmd: "invokeCapture",
  args: [{tabId: tabId}]
});

[content script]:

Invoke Capture EX

To be continue...

Clone this wiki locally