-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdevtools.js
More file actions
81 lines (73 loc) · 2.17 KB
/
devtools.js
File metadata and controls
81 lines (73 loc) · 2.17 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
80
81
function getLiveDebuggerSessionURL() {
return new Promise((resolve, reject) => {
const script = `
(function() {
function getSessionId() {
let el;
if ((el = document.querySelector('[data-phx-main]'))) {
return el.id;
}
if ((el = document.querySelector('[id^="phx-"]'))) {
return el.id;
}
if ((el = document.querySelector('[data-phx-root-id]'))) {
return el.getAttribute('data-phx-root-id');
}
return null;
}
function handleMetaTagError() {
throw new Error("LiveDebugger meta tag not found!");
}
function getLiveDebuggerBaseURL() {
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
if (metaTag) {
return metaTag.getAttribute('url');
} else {
handleMetaTagError();
}
}
function getSessionURL(baseURL) {
const session_id = getSessionId();
const session_path = session_id ? \`transport_pid/\${session_id}\` : '';
return \`\${baseURL}/\${session_path}\`;
}
const baseURL = getLiveDebuggerBaseURL();
return getSessionURL(baseURL);
})();
`;
chrome.devtools.inspectedWindow.eval(script, (result, isException) => {
if (isException || !result) {
reject(new Error("Error fetching LiveDebugger session URL"));
} else {
resolve(result);
}
});
});
}
chrome.devtools.panels.create(
"LiveDebugger",
"images/icon-16.png",
"panel.html",
function (panel) {
let panelWindow;
let isShown = false;
panel.onShown.addListener(async (window) => {
if (!isShown) {
panelWindow = window;
isShown = true;
try {
window.set_iframe_url(await getLiveDebuggerSessionURL());
} catch (error) {
window.set_iframe_url(null);
}
}
});
chrome.webNavigation.onCompleted.addListener(async () => {
try {
panelWindow.set_iframe_url(await getLiveDebuggerSessionURL());
} catch (error) {
panelWindow.set_iframe_url(null);
}
});
}
);