-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathdevtools.js
More file actions
57 lines (52 loc) · 1.99 KB
/
devtools.js
File metadata and controls
57 lines (52 loc) · 1.99 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
let availableMemoryCapacity;
let totalMemoryCapacity;
let youClickedOn;
chrome.devtools.panels.create("Custom", "icon.png", "panel.html", panel => {
// code invoked on panel creation
panel.onShown.addListener( (extPanelWindow) => {
// memory
availableMemoryCapacity = extPanelWindow.document.querySelector('#availableMemoryCapacity');
totalMemoryCapacity = extPanelWindow.document.querySelector('#totalMemoryCapacity');
// 2-way message sending
let sayHello = extPanelWindow.document.querySelector('#sayHello');
youClickedOn = extPanelWindow.document.querySelector('#youClickedOn');
sayHello.addEventListener("click", () => {
// show a greeting alert in the inspected page
chrome.devtools.inspectedWindow.eval('alert("Hello from the DevTools extension!");');
});
});
});
// Update Memory display
setInterval(() => {
chrome.system.memory.getInfo((data) => {
if (availableMemoryCapacity) {
availableMemoryCapacity.innerHTML = data.availableCapacity;
}
if (totalMemoryCapacity) {
totalMemoryCapacity.innerHTML = data.capacity;
}
});
}, 1000);
// Send message from inspected page to DevTools
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// Messages from content scripts should have sender.tab set
if (sender.tab && request.click == true) {
console.log('I am here!');
if (youClickedOn) {
youClickedOn.innerHTML = `(${request.xPosition}, ${request.yPosition})`;
}
sendResponse({
xPosition: request.xPosition,
yPosition: request.yPosition
});
}
});
// Create a connection to the background service worker
const backgroundPageConnection = chrome.runtime.connect({
name: "devtools-page"
});
// Relay the tab ID to the background service worker
backgroundPageConnection.postMessage({
name: 'init',
tabId: chrome.devtools.inspectedWindow.tabId
});