electron/spec-main/fixtures/extensions/chrome-api/background.js

30 lines
720 B
JavaScript
Raw Normal View History

/* global chrome */
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
2020-03-20 20:28:31 +00:00
const { method, args = [] } = message;
const tabId = sender.tab.id;
switch (method) {
case 'sendMessage': {
2020-03-20 20:28:31 +00:00
const [message] = args;
chrome.tabs.sendMessage(tabId, { message, tabId }, undefined, sendResponse);
break;
}
case 'executeScript': {
2020-03-20 20:28:31 +00:00
const [code] = args;
chrome.tabs.executeScript(tabId, { code }, ([result]) => sendResponse(result));
break;
}
case 'connectTab': {
2020-03-20 20:28:31 +00:00
const [name] = args;
const port = chrome.tabs.connect(tabId, { name });
port.postMessage('howdy');
break;
}
}
// Respond asynchronously
2020-03-20 20:28:31 +00:00
return true;
});