-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground-utils.ts
More file actions
291 lines (275 loc) · 10.5 KB
/
background-utils.ts
File metadata and controls
291 lines (275 loc) · 10.5 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2021 IMAGE Project, Shared Reality Lab, McGill University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* and our Additional Terms along with this program.
* If not, see <https://github.com/Shared-Reality-Lab/IMAGE-browser/LICENSE>.
*/
import browser from "webextension-polyfill";
import { v4 as uuidv4 } from "uuid";
import { IMAGERequest } from "./types/request.schema";
import { getAllStorageSyncData, getCapabilities, getRenderers, getLanguage, windowsPanel } from './utils';
import { encryptData, monarchPopUp, saveToLocalStorage } from "./monarch/utils";
import { TatStorageData } from "./monarch/types";
import { openRenderingsinWindow } from "./config";
/**
* Generates a query for remote resources
*
* @param message - Object containing context, URL, dimensions, and graphic blob
* @returns Promise resolving to an IMAGERequest object
*/
export async function generateQuery(message: { context: string, url: string, dims: [number, number], graphicBlob: string }): Promise<IMAGERequest> {
let renderers = await getRenderers();
let capabilities = await getCapabilities();
console.debug("inside generate query");
return {
"request_uuid": uuidv4(),
"timestamp": Math.round(Date.now() / 1000),
"URL": message.url,
"graphic": message.graphicBlob,
"dimensions": message.dims,
"context": message.context,
"language": await getLanguage(),
"capabilities": capabilities,
"renderers": renderers
} as IMAGERequest;
}
/**
* Generates a query for local resources
*
* @param message - Object containing context, dimensions, image, and graphic blob
* @returns Promise resolving to an IMAGERequest object
*/
export async function generateLocalQuery(message: { context: string, dims: [number, number], image: string, graphicBlob: string }): Promise<IMAGERequest> {
let renderers = await getRenderers();
let capabilities = await getCapabilities();
return {
"request_uuid": uuidv4(),
"timestamp": Math.round(Date.now() / 1000),
"graphic": message.graphicBlob,
"dimensions": message.dims,
"context": message.context,
"language": await getLanguage(),
"capabilities": capabilities,
"renderers": renderers
} as IMAGERequest;
}
/**
* Generates a query for chart resources
*
* @param message - Object containing highChartsData
* @returns Promise resolving to an IMAGERequest object
*/
export async function generateChartQuery(message: { highChartsData: { [k: string]: unknown } }): Promise<IMAGERequest> {
let renderers = await getRenderers();
let capabilities = await getCapabilities();
return {
"request_uuid": uuidv4(),
"timestamp": Math.round(Date.now() / 1000),
"highChartsData": message.highChartsData,
"language": await getLanguage(),
"capabilities": capabilities,
"renderers": renderers
} as IMAGERequest;
}
/**
* Processes tactile rendering data for Monarch or Tactile Authoring Tool
*
* @param tactileSvgGraphic - SVG graphic data in base64 format
* @param query - The IMAGERequest object
* @param message - The message object containing options
* @param serverUrl - The server URL to send requests to
*/
export async function processTactileRendering(tactileSvgGraphic: string, query: IMAGERequest, message: any, serverUrl: RequestInfo) {
let items = await getAllStorageSyncData();
let encodedSvg = tactileSvgGraphic.split("data:image/svg+xml;base64,")[1];
let svgDom = atob(encodedSvg);
console.log("SVG DOM", svgDom);
let reqTitle = items["monarchTitle"];
let reqSecretKey = items["monarchSecretKey"];
let reqChannelId = items["monarchChannelId"];
let encryptionKey = items["monarchEncryptionKey"];
const flowType = reqChannelId ? "update" : "create";
let monarchTargetUrl = `${serverUrl}/monarch`;
monarchTargetUrl = monarchTargetUrl.replace(/([^:]\/)\/+/g, "$1");
let monarchFetchUrl = flowType == "update" ?
`${monarchTargetUrl}/update/${reqChannelId}` :
`${monarchTargetUrl}/create`;
let encryptedGraphicBlob = query["graphic"] && await encryptData(query["graphic"], encryptionKey);
let encryptedCoordinates = query["coordinates"] && await encryptData(JSON.stringify(query["coordinates"]), encryptionKey);
let encryptedPlaceId = query["placeID"] && await encryptData(query["placeID"], encryptionKey);
const reqData = await encryptData(svgDom, encryptionKey);
const reqBody = {
"data": reqData,
"layer": "None",
"title": reqTitle,
"secret": reqSecretKey,
"graphicBlob": encryptedGraphicBlob,
"coordinates": encryptedCoordinates,
"placeID": encryptedPlaceId
};
if (message["sendToMonarch"]) {
/** Send Graphic to Monarch flow - Make curl request to monarch */
const response = await fetch(monarchFetchUrl,
{
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": JSON.stringify(reqBody)
});
let responseJSON = {
"id": reqChannelId || "",
"secret": ""
};
if (flowType == "create") {
responseJSON = await response.json();
browser.storage.sync.set({
"monarchChannelId": responseJSON["id"],
"monarchSecretKey": responseJSON["secret"]
});
}
let currentTab = await browser.tabs.query({ active: true, currentWindow: true });
// Check if the current tab is an extension page
if (currentTab[0].url && !currentTab[0].url.startsWith('chrome-extension://')) {
browser.scripting.executeScript({
target: { tabId: currentTab[0].id || 0 },
func: monarchPopUp,
args: [responseJSON["id"], flowType]
});
} else {
// If we're on an extension page, use notifications instead of alert
const message = flowType === "create"
? `New channel created with code ${responseJSON["id"]}`
: `Graphic in channel ${responseJSON["id"]} has been updated!`;
browser.notifications.create({
type: 'basic',
iconUrl: 'image-icon-128.png',
title: 'Monarch Response',
message: message
});
}
} else {
/** Handle "Load in Tactile Authoring Tool" flow */
const tatStorageData: TatStorageData = {
channelId: items["monarchChannelId"],
graphicTitle: items["monarchTitle"],
secretKey: items["monarchSecretKey"],
graphicBlob: query["graphic"],
coordinates: query["coordinates"] && JSON.stringify(query["coordinates"]),
placeID: query["placeID"],
}
let tatTargetUrl = `${serverUrl}/tat/`;
tatTargetUrl = tatTargetUrl.replace(/([^:]\/)\/+/g, "$1");
let tabs = await browser.tabs.query({ url: tatTargetUrl });
/** encrypt data before storing in local storage */
let encryptedSvgData = await encryptData(svgDom, encryptionKey);
let encryptedTatData: TatStorageData = {channelId:"", graphicTitle:"", secretKey:""};
for (let key of Object.keys(tatStorageData)) {
let stringToEncrypt = tatStorageData[key as keyof TatStorageData];
if (stringToEncrypt){
encryptedTatData[key as keyof TatStorageData] = await encryptData(tatStorageData[key as keyof TatStorageData], encryptionKey);
}
}
if (tabs && tabs.length > 0) {
let existingTab = tabs[0];
browser.scripting.executeScript({
target: { tabId: existingTab.id || 0 },
func: saveToLocalStorage,
args: [encryptedSvgData, encryptedTatData, existingTab]
});
browser.tabs.update(existingTab.id, { active: true });
}
else {
let authoringTool = browser.tabs.create({
url: tatTargetUrl
});
authoringTool.then((tab) => {
browser.scripting.executeScript({
target: { tabId: tab.id || 0 },
func: saveToLocalStorage,
args: [encryptedSvgData, encryptedTatData]
});
}, (error) => { console.log(error) });
}
}
}
/**
* Converts a Blob to a base64 string
*
* @param blob - The Blob to convert
* @returns Promise resolving to the base64 string
*/
export function blobToBase64(blob: Blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(blob);
});
}
/**
* Creates a panel or tab to display renderings
*
* @param query - The IMAGERequest object
* @param graphicUrl - URL of the graphic
* @returns Promise resolving to the created window or tab
*/
export function createPanel(query: IMAGERequest, graphicUrl: string) {
let window = windowsPanel ? browser.windows.create({
type: "normal",
url: `info/info.html?uuid=${query["request_uuid"]}&graphicUrl=${graphicUrl}&dimensions=${query["dimensions"]}`,
height: 1080,
width: 1920
}) : browser.tabs.create({
url: `info/info.html?uuid=${query["request_uuid"]}&graphicUrl=${graphicUrl}&dimensions=${query["dimensions"]}`,
});
return window;
}
/**
* Toggles map options based on debug and monarch settings
*
* @param showDebugOptions - Boolean indicating whether to show debug options
* @param monarchEnabled - Boolean indicating whether monarch is enabled
*/
export function toggleMapOptions(showDebugOptions: Boolean, monarchEnabled: Boolean) {
let mapButtonContainer = document.getElementById("map-button-container");
let mapSelectContainer = document.getElementById("map-select-container");
if (mapButtonContainer && mapSelectContainer) {
if (monarchEnabled) {
mapSelectContainer.style.display = "flex";
mapButtonContainer.style.display = "none";
} else {
mapSelectContainer.style.display = "none";
mapButtonContainer.style.display = "flex";
}
}
}
/**
* Creates an offscreen document to keep the service worker running
*/
export async function createOffscreen() {
// @ts-ignore
await browser.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['BLOBS'],
justification: 'keep service worker running',
}).catch(() => { });
}
/**
* Callback function for browser.contextMenus.create
*/
export function onCreated(): void {
if (browser.runtime.lastError) {
//console.error(browser.runtime.lastError);
}
}