Skip to content

External Message API

Gold Holk edited this page Apr 17, 2024 · 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 and scrapbook.invokeCapture). 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 webscrapbook@danny0838.addons.mozilla.org (defined in webscrapbook's manifest). Chromium will always generate a random id while installation.

Extensions can also 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 extensions = await browser.management.getAll();
const webscrapbook = extensions.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 scripts.

// in firefox
let extensionId = "webscrapbook@danny0838.addons.mozilla.org";

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

Invoke Capture EX

To be continue...

Clone this wiki locally