Skip to content

Instantly share code, notes, and snippets.

@lmorchard
Created March 7, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmorchard/bc768f49bd1b102d15450269ad864181 to your computer and use it in GitHub Desktop.
Save lmorchard/bc768f49bd1b102d15450269ad864181 to your computer and use it in GitHub Desktop.
Dispatching messages from web content in an add-on
const messageListener = port => message => {
let theme;
switch (message.type) {
case "fetchTheme":
log("fetchTheme");
fetchTheme().then(({ theme: currentTheme }) =>
port.postMessage({ type: "fetchedTheme", theme: currentTheme })
);
break;
case "setTheme":
theme = normalizeTheme(message.theme);
log("setTheme", theme);
storeTheme({ theme });
applyTheme({ theme });
break;
case "ping":
port.postMessage({ type: "pong" });
break;
default:
log("unexpected message", message);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment