fix: regressions introduced by adding world isolation to Chrome extension content scripts (#17422)

This commit is contained in:
Milan Burda 2019-03-19 14:45:48 +01:00 committed by Alexey Kuzmin
parent 2fb9085e5b
commit 53f4af7722
6 changed files with 83 additions and 11 deletions

View file

@ -0,0 +1,20 @@
/* 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
}
}
})

View file

@ -1,15 +1,28 @@
/* global chrome */
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
sendResponse(message)
})
const testMap = {
getManifest () {
const manifest = chrome.runtime.getManifest()
console.log(JSON.stringify(manifest))
},
sendMessage (message) {
chrome.runtime.sendMessage({ method: 'sendMessage', args: [message] }, response => {
console.log(JSON.stringify(response))
})
},
executeScript (code) {
chrome.runtime.sendMessage({ method: 'executeScript', args: [code] }, response => {
console.log(JSON.stringify(response))
})
}
}
const dispatchTest = (event) => {
const testName = event.data
const test = testMap[testName]
test()
const { method, args = [] } = JSON.parse(event.data)
testMap[method](...args)
}
window.addEventListener('message', dispatchTest, false)

View file

@ -8,5 +8,9 @@
"run_at": "document_start"
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}