This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcontent.js
85 lines (73 loc) · 2.34 KB
/
content.js
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
80
81
82
83
84
85
const injectScript = document.createElement('script');
const injectFunction = () => {
let gProfile = null;
const symbolReplyPromiseMap = new Map();
window.addEventListener('message', event => {
if (event.source != window) {
return;
}
if (event.data.type === 'ProfilerConnectToPage') {
gProfile = event.data.payload;
connectToPage();
document.addEventListener('DOMContentLoaded', connectToPage);
} else if (event.data.type === 'ProfilerGetSymbolTableReply') {
const { debugName, breakpadId, status, result, error } = event.data;
const promiseKey = [debugName, breakpadId].join(':');
const { resolve, reject } = symbolReplyPromiseMap.get(promiseKey);
symbolReplyPromiseMap.delete(promiseKey);
if (status === 'success') {
const [addresses, index, buffer] = result;
resolve([addresses, index, buffer]);
} else {
reject(error);
}
}
});
function connectToPage() {
if (window.connectToGeckoProfiler) {
window.connectToGeckoProfiler({
getProfile: () => Promise.resolve(gProfile),
getSymbolTable,
});
}
}
function getSymbolTable(debugName, breakpadId) {
return new Promise((resolve, reject) => {
window.postMessage(
{ type: 'ProfilerGetSymbolTable', debugName, breakpadId },
'*'
);
symbolReplyPromiseMap.set([debugName, breakpadId].join(':'), {
resolve,
reject,
});
});
}
};
injectScript.innerHTML = `(${injectFunction.toString()})();`;
document.documentElement.appendChild(injectScript);
const port = browser.runtime.connect({ name: 'ProfilerPage' });
window.addEventListener('message', event => {
if (event.source != window) {
return;
}
const validMessages = ['ProfilerGetSymbolTable'];
if (validMessages.includes(event.data.type)) {
port.postMessage(event.data);
}
});
port.onMessage.addListener(message => {
// const validMessages = [
// 'ProfilerConnectToPage',
// 'ProfilerGetSymbolTableReply',
// ];
if (message.type === 'ProfilerGetSymbolTableReply') {
if (message.status === 'success') {
window.postMessage(message, '*', message.result.map(r => r.buffer));
} else {
window.postMessage(message, '*');
}
} else if (message.type === 'ProfilerConnectToPage') {
window.postMessage(message, '*');
}
});