|
| 1 | +// the background script runs all the time and serves as a central message |
| 2 | +// hub for each vue devtools (panel + proxy + backend) instance. |
| 3 | + |
| 4 | +const ports = {} |
| 5 | + |
| 6 | +chrome.runtime.onConnect.addListener((port) => { |
| 7 | + let tab |
| 8 | + let name |
| 9 | + if (isNumeric(port.name)) { |
| 10 | + tab = port.name |
| 11 | + name = 'devtools' |
| 12 | + installProxy(+port.name) |
| 13 | + } |
| 14 | + else { |
| 15 | + tab = port.sender.tab.id |
| 16 | + name = 'backend' |
| 17 | + } |
| 18 | + |
| 19 | + if (!ports[tab]) { |
| 20 | + ports[tab] = { |
| 21 | + devtools: null, |
| 22 | + backend: null, |
| 23 | + } |
| 24 | + } |
| 25 | + ports[tab][name] = port |
| 26 | + |
| 27 | + if (ports[tab].devtools && ports[tab].backend) { |
| 28 | + doublePipe(tab, ports[tab].devtools, ports[tab].backend) |
| 29 | + } |
| 30 | +}) |
| 31 | + |
| 32 | +function isNumeric(str) { |
| 33 | + return `${+str}` === str |
| 34 | +} |
| 35 | + |
| 36 | +function installProxy(tabId) { |
| 37 | + chrome.tabs.executeScript(tabId, { |
| 38 | + file: '/build/proxy.js', |
| 39 | + }, (res) => { |
| 40 | + if (!res) { |
| 41 | + ports[tabId].devtools.postMessage('proxy-fail') |
| 42 | + } |
| 43 | + else { |
| 44 | + if (process.env.NODE_ENV !== 'production') { |
| 45 | + // eslint-disable-next-line no-console |
| 46 | + console.log(`injected proxy to tab ${tabId}`) |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +function doublePipe(id, one, two) { |
| 53 | + one.onMessage.addListener(lOne) |
| 54 | + function lOne(message) { |
| 55 | + if (message.event === 'log') { |
| 56 | + // eslint-disable-next-line no-console |
| 57 | + return console.log(`tab ${id}`, message.payload) |
| 58 | + } |
| 59 | + if (process.env.NODE_ENV !== 'production') { |
| 60 | + // eslint-disable-next-line no-console |
| 61 | + console.log('%cdevtools -> backend', 'color:#888;', message) |
| 62 | + } |
| 63 | + two.postMessage(message) |
| 64 | + } |
| 65 | + two.onMessage.addListener(lTwo) |
| 66 | + function lTwo(message) { |
| 67 | + if (message.event === 'log') { |
| 68 | + // eslint-disable-next-line no-console |
| 69 | + return console.log(`tab ${id}`, message.payload) |
| 70 | + } |
| 71 | + if (process.env.NODE_ENV !== 'production') { |
| 72 | + // eslint-disable-next-line no-console |
| 73 | + console.log('%cbackend -> devtools', 'color:#888;', message) |
| 74 | + } |
| 75 | + one.postMessage(message) |
| 76 | + } |
| 77 | + function shutdown() { |
| 78 | + if (process.env.NODE_ENV !== 'production') { |
| 79 | + // eslint-disable-next-line no-console |
| 80 | + console.log(`tab ${id} disconnected.`) |
| 81 | + } |
| 82 | + one.onMessage.removeListener(lOne) |
| 83 | + two.onMessage.removeListener(lTwo) |
| 84 | + one.disconnect() |
| 85 | + two.disconnect() |
| 86 | + ports[id] = null |
| 87 | + } |
| 88 | + one.onDisconnect.addListener(shutdown) |
| 89 | + two.onDisconnect.addListener(shutdown) |
| 90 | + if (process.env.NODE_ENV !== 'production') { |
| 91 | + // eslint-disable-next-line no-console |
| 92 | + console.log(`tab ${id} connected.`) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +chrome.runtime.onMessage.addListener((req, sender) => { |
| 97 | + if (sender.tab && req.vueDetected) { |
| 98 | + const suffix = req.nuxtDetected ? '.nuxt' : '' |
| 99 | + |
| 100 | + chrome.browserAction.setIcon({ |
| 101 | + tabId: sender.tab.id, |
| 102 | + path: { |
| 103 | + 16: `icons/16${suffix}.png`, |
| 104 | + 48: `icons/48${suffix}.png`, |
| 105 | + 128: `icons/128${suffix}.png`, |
| 106 | + }, |
| 107 | + }) |
| 108 | + chrome.browserAction.setPopup({ |
| 109 | + tabId: sender.tab.id, |
| 110 | + popup: req.devtoolsEnabled ? `popups/enabled${suffix}.html` : `popups/disabled${suffix}.html`, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + if (req.action === 'vue-take-screenshot' && sender.envType === 'devtools_child') { |
| 115 | + browser.tabs.captureVisibleTab({ |
| 116 | + format: 'png', |
| 117 | + }).then((dataUrl) => { |
| 118 | + browser.runtime.sendMessage({ |
| 119 | + action: 'vue-screenshot-result', |
| 120 | + id: req.id, |
| 121 | + dataUrl, |
| 122 | + }) |
| 123 | + }) |
| 124 | + } |
| 125 | +}) |
0 commit comments