Description

An abstraction for communications between JavaScript and host platforms.

  1. First you construct your platform-specific configuration (eg: WebkitMessagingConfig)
  2. Then use that to get an instance of the Messaging utility which allows you to send and receive data in a unified way
  3. Each platform implements MessagingTransport along with its own Configuration

Example

Webkit Messaging

import { Messaging, WebkitMessagingConfig } from "@duckduckgo/content-scope-scripts/lib/messaging.js"

// This config would be injected into the UserScript
const injectedConfig = {
hasModernWebkitAPI: true,
webkitMessageHandlerNames: ["foo", "bar", "baz"],
secret: "dax",
};

// Then use that config to construct platform-specific configuration
const config = new WebkitMessagingConfig(injectedConfig);

// finally, get an instance of Messaging and start sending messages in a unified way 🚀
const messaging = new Messaging(config);
messaging.notify("hello world!", {foo: "bar"})

Example

Windows Messaging

import { Messaging, WindowsMessagingConfig } from "@duckduckgo/content-scope-scripts/lib/messaging.js"

// Messaging on Windows is namespaced, so you can create multiple messaging instances
const autofillConfig = new WindowsMessagingConfig({ featureName: "Autofill" });
const debugConfig = new WindowsMessagingConfig({ featureName: "Debugging" });

const autofillMessaging = new Messaging(autofillConfig);
const debugMessaging = new Messaging(debugConfig);

// Now send messages to both features as needed 🚀
autofillMessaging.notify("storeFormData", { "username": "dax" })
debugMessaging.notify("pageLoad", { time: window.performance.now() })

References

Re-exports WebkitMessagingConfig
Re-exports WindowsMessagingConfig

Generated using TypeDoc