-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathurl.js
More file actions
100 lines (88 loc) · 2.7 KB
/
url.js
File metadata and controls
100 lines (88 loc) · 2.7 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function getLiveDebuggerSessionURL(browserElement) {
return new Promise((resolve, reject) => {
const script = `
(function() {
function getMetaTag() {
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
if (metaTag) {
return metaTag;
} else {
handleMetaTagError();
}
}
function handleMetaTagError() {
throw new Error("LiveDebugger meta tag not found!");
}
function getLiveDebuggerBaseURL(metaTag) {
return metaTag.getAttribute('url');
}
function getVersion(metaTag) {
const version = metaTag.getAttribute('version');
return version ? version : "0.2"
}
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 getSessionURL(baseURL, version, sessionId) {
let prefix = '';
if (version.startsWith("0.2")) {
prefix = "transport_pid";
} else {
prefix = "redirect";
}
const session_path = sessionId ? \`\${prefix}/\${sessionId}\` : '';
return \`\${baseURL}/\${session_path}\`;
}
const metaTag = getMetaTag();
const version = getVersion(metaTag);
const baseURL = getLiveDebuggerBaseURL(metaTag);
const sessionId = getSessionId();
const url = getSessionURL(baseURL, version, sessionId);
return url;
})();
`;
browserElement.devtools.inspectedWindow.eval(
script,
(result, isException) => {
if (isException || !result) {
reject(new Error("Error fetching LiveDebugger session URL"));
} else {
resolve(result);
}
}
);
});
}
function allowRedirects() {
return new Promise((resolve, reject) => {
const script = `
(function() {
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
if (metaTag) {
return metaTag.getAttribute('devtools-allow-redirects') === 'true';
}
return false;
})();
`;
browserElement.devtools.inspectedWindow.eval(
script,
(result, isException) => {
if (isException) {
reject(new Error("Error checking allow redirects"));
} else {
resolve(result);
}
}
);
});
}