forked from vuejs/devtools-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevtools.js
More file actions
79 lines (70 loc) · 1.92 KB
/
devtools.js
File metadata and controls
79 lines (70 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// this script is called when the VueDevtools panel is activated.
import { initDevTools, setAppConnected } from '@front'
import { Bridge } from '@vue-devtools/shared-utils'
initDevTools({
/**
* Inject backend, connect to background, and send back the bridge.
*
* @param {Function} cb
*/
connect (cb) {
// 1. inject backend code into page
injectScript(chrome.runtime.getURL('build/backend.js'), () => {
// 2. connect to background to setup proxy
const port = chrome.runtime.connect({
name: '' + chrome.devtools.inspectedWindow.tabId,
})
let disconnected = false
port.onDisconnect.addListener(() => {
disconnected = true
setAppConnected(false)
})
const bridge = new Bridge({
listen (fn) {
port.onMessage.addListener(fn)
},
send (data) {
if (!disconnected) {
// if (process.env.NODE_ENV !== 'production') {
// console.log('[chrome] devtools -> backend', data)
// }
port.postMessage(data)
}
},
})
// 3. send a proxy API to the panel
cb(bridge)
})
},
/**
* Register a function to reload the devtools app.
*
* @param {Function} reloadFn
*/
onReload (reloadFn) {
chrome.devtools.network.onNavigated.addListener(reloadFn)
},
})
/**
* Inject a globally evaluated script, in the same context with the actual
* user app.
*
* @param {String} scriptName
* @param {Function} cb
*/
function injectScript (scriptName, cb) {
const src = `
(function() {
var script = document.constructor.prototype.createElement.call(document, 'script');
script.src = "${scriptName}";
document.documentElement.appendChild(script);
script.parentNode.removeChild(script);
})()
`
chrome.devtools.inspectedWindow.eval(src, function (res, err) {
if (err) {
console.error(err)
}
cb()
})
}