2019-03-19 13:45:48 +00:00
|
|
|
/* 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;
|
2019-03-19 13:45:48 +00:00
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case 'sendMessage': {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [message] = args;
|
|
|
|
chrome.tabs.sendMessage(tabId, { message, tabId }, undefined, sendResponse);
|
|
|
|
break;
|
2019-03-19 13:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'executeScript': {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [code] = args;
|
|
|
|
chrome.tabs.executeScript(tabId, { code }, ([result]) => sendResponse(result));
|
|
|
|
break;
|
2019-03-19 13:45:48 +00:00
|
|
|
}
|
2020-03-05 14:59:32 +00:00
|
|
|
|
|
|
|
case 'connectTab': {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [name] = args;
|
|
|
|
const port = chrome.tabs.connect(tabId, { name });
|
|
|
|
port.postMessage('howdy');
|
|
|
|
break;
|
2020-03-05 14:59:32 +00:00
|
|
|
}
|
2019-03-19 13:45:48 +00:00
|
|
|
}
|
2020-01-15 23:11:51 +00:00
|
|
|
// Respond asynchronously
|
2020-03-20 20:28:31 +00:00
|
|
|
return true;
|
|
|
|
});
|