electron/spec-main/fixtures/extensions/chrome-api/background.js
John Kleinschmidt 0201b3e571
feat(extensions): add chrome.tabs.connect API (#22457)
* feat(extensions): add chrome.tabs.connect API

* test(extensions): verify that chrome.tabs.connect port communication works
2020-03-05 09:59:32 -05:00

29 lines
706 B
JavaScript

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