-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
170 lines (164 loc) · 4.8 KB
/
background.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Handle web_accessible_resources iframe request
self.addEventListener("fetch", async (event) => {
try {
const requestUrl = new URL(event.request.url);
const entries = requestUrl.searchParams;
if (entries.has("sdp")) {
const webAppDetails = await getWebAppInternalsDetails();
console.log(webAppDetails);
const window = await openIsolatedWebApp(
webAppDetails,
entries.get("name"),
`?${entries.toString()}`,
);
}
if (!entries.has("sdp")) {
const webAppDetails = await getWebAppInternalsDetails();
console.log(webAppDetails);
const window = await openIsolatedWebApp(
webAppDetails,
entries.get("name"),
);
console.log(window);
}
} catch (e) {
console.error(chrome.runtime.lastError, e);
}
});
// Handle fetch() request - from/to the current Web page, excluding chrome:
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener(
async (event) => {
console.log(event);
try {
if (
(await chrome.tabs.query({
windowType: "app",
title: "TCPServerSocket",
}))
.length === 0
) {
const webAppDetails = await getWebAppInternalsDetails();
console.log(webAppDetails);
const window = await openIsolatedWebApp(
webAppDetails,
"TCPServerSocket",
);
console.log(window);
}
} catch (e) {
console.error(chrome.runtime.lastError, e);
}
},
);
// Handle document.title update
chrome.tabs.onUpdated.addListener(async (tabId, { title, url }, tab) => {
try {
if (title?.includes("?name=TCPServerSocket")) {
console.log(tab.url, title);
const webAppDetails = await getWebAppInternalsDetails();
console.log(webAppDetails);
const window = await openIsolatedWebApp(
webAppDetails,
"TCPServerSocket",
);
console.log(window);
}
if (title?.includes("?sdp=") && !tab.url.startsWith("isolated-app:")) {
console.log(tab.url);
const re = /\?sdp=.+/;
const webAppDetails = await getWebAppInternalsDetails();
console.log(webAppDetails);
const [sdp] = title.match(re);
const window = await openIsolatedWebApp(
webAppDetails,
"Signed Web Bundle in Isolated Web App",
sdp,
);
console.log(window);
}
if (url?.includes("isolated-app")) {
console.log(tab.url);
}
} catch (e) {
console.log(chrome.runtime.lastError, e);
}
});
chrome.runtime.onInstalled.addListener(async (reason) => {
console.log(reason);
});
// Get wet-app-internals JSON
// Inject extension ID into all Web pages for web_accessible_resources request
// Get injected extension extension ID in Web page, delete private origin file
chrome.scripting.unregisterContentScripts().then(() =>
chrome.scripting
.registerContentScripts([{
id: "get-iwa-details",
js: ["get-web-app-internals-json.js"],
persistAcrossSessions: false,
matches: ["chrome://web-app-internals/*"],
runAt: "document_idle",
}, {
id: "set-extension-id",
js: ["get-extension-id.js"],
persistAcrossSessions: false,
matches: ["<all_urls>", "chrome://*/*"],
world: "ISOLATED",
runAt: "document_start",
}, {
id: "get-extension-id",
js: ["get-extension-id.js"],
persistAcrossSessions: false,
matches: ["<all_urls>", "chrome://*/*"],
world: "MAIN",
runAt: "document_idle",
}])
).catch((e) => console.error(chrome.runtime.lastError, e));
// Open IWA window
async function openIsolatedWebApp(
webAppDetails,
isolatedWebAppName,
detail = "",
) {
try {
console.log(isolatedWebAppName, detail);
const url = webAppDetails.find((webapp) =>
webapp["!name"] === isolatedWebAppName
)
.start_url;
return await chrome.windows.create({
url: `${url}${detail}`,
height: 0,
width: 0,
left: 0,
top: 0,
focused: false,
type: "normal",
});
} catch (e) {
console.error(chrome.runtime.lastError, e);
}
}
// Get chrome://web-app-internals JSON
async function getWebAppInternalsDetails() {
try {
const { id, tabs: [{ id: tabId }] } = await chrome.windows.create({
url: "chrome://web-app-internals",
state: "minimized",
focused: false,
});
const { resolve, promise } = Promise.withResolvers();
const handleMessage = async (message) => {
resolve(message);
chrome.runtime.onMessage.removeListener(handleMessage);
await chrome.windows.remove(id);
};
chrome.runtime.onMessage.addListener(handleMessage);
const result = await promise;
const { InstalledWebApps: { Details } } = result.find((
{ InstalledWebApps },
) => InstalledWebApps);
return Details;
} catch (e) {
console.error(chrome.runtime.lastError, e);
}
}