Learning by following this https://www.youtube.com/watch?v=QukSh7ss7A8 and the code is here: https://github.com/pitops/design-review-chrome-extension
It's smooth sailing until I get to the part about onMessage. In particular, I'm stuck on this:
contentScript.js
const actions = {
"toggle-designmode": toggleDesignMode,
"get-state": getState,
capture: captureVisibleTab,
drawImage: drawImage,
};
chrome.runtime.onMessage.addListener((request, sender, response) => {
const reply = actions[request.type] && actions[request.type](request.payload);
reply && response(reply);
});
...
const getState = () => {
const state = sessionStorage.getItem("state");
return {
type: "get-state",
payload: state ? JSON.parse(state) : { designMode: document.designMode },
};
};
This is triggered by popup.js
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, message, function (response) {
This line in particularly confuses me the most:
const reply = actions[request.type] && actions[request.type](request.payload);
Haven't seen this kind of JS notation before, particularly the part about the payload with the braces?
Thanks!!!