-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlatealways_patch.js
456 lines (414 loc) · 17.7 KB
/
latealways_patch.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
if ("serviceWorker" in navigator && !document.location.href.startsWith("file:")) {
navigator.serviceWorker.register("sw.js");
}
let manifest = {
update_url: "https://clients2.google.com/service/update2/crx",
manifest_version: 2,
name: "TI Connect CE App for Chrome OS",
short_name: "TI Connect CE App",
version: "0.1.0.9 Patched by LateAlways",
version_name: "0.1.0.9",
description: "Manage USB connected Texas Instruments TI‑84 Plus CE graphing calculators",
icons: {
16: "images/icon16.png",
48: "images/icon48.png",
128: "images/icon128.png"
},
permissions: [
{
fileSystem: [
"write",
"retainEntries",
"directory"
]
},
"usb",
{
usbDevices: [
{
vendorId: 1105,
productId: 57352
}
]
},
"https://www.google-analytics.com/",
"storage"
],
app: {
background: {
scripts: [
"js/background.js"
]
}
},
file_handlers: {
tic_handler: {
extensions: [
"8xv"
]
}
},
minimum_chrome_version: "41"
};
// runtime api
window.chrome.runtime = {
lastError: 0,
getManifest: () => {
return manifest
},
getBackgroundPage: (s) => {
s(window)
return window
}
};
// storage api
window.chrome.storage = {
local: {
QUOTA_BYTES: 5242880,
set: (w, then) => {
for (let key in w) {
localStorage.setItem("emul_storage_local_"+key, JSON.stringify(w[key]));
}
then();
},
get: (w, then) => {
if (!localStorage.getItem("emul_storage_local_"+w)) {
window.chrome.runtime.lastError = {message: "fail to get key."};
} else {
window.chrome.runtime.lastError = undefined;
}
then({ [w]: JSON.parse(localStorage.getItem("emul_storage_local_"+w)) });
},
getBytesInUse: (w, then) => {
if (then) {
then(0);
} else {
return new Promise((res, rej) => {
res(0);
});
}
},
},
sync: {
QUOTA_BYTES: 5242880,
set: (w, then) => {
for (let key in w) {
localStorage.setItem("emul_storage_sync_"+key, JSON.stringify(w[key]));
}
then();
},
get: async (w, then) => {
if (!localStorage.getItem("emul_storage_sync_"+w)) {
window.chrome.runtime.lastError = {message: "fail to get key."};
return then();
}
await new Promise(res => setTimeout(res, 100));
window.chrome.runtime.lastError = undefined;
then({ [w]: JSON.parse(localStorage.getItem("emul_storage_sync_"+w)) });
},
getBytesInUse: (w, then) => {
if (then) {
then(0);
} else {
return new Promise((res, rej) => {
res(0);
});
}
},
},
onChanged: {
addListener: () => { }
}
}
// usb api
let currentDevice = null;
let events = {
onDeviceAdded: [],
onDeviceRemoved: []
};
window.chrome.usb = {
closeDevice: (w,s) => {chrome.runtime.lastError = undefined; s(); },
onDeviceAdded: {
addListener: (s) => {
events.onDeviceAdded.push(s);
}
},
onDeviceRemoved: {
addListener: (s) => {
events.onDeviceRemoved.push(s);
}
},
getDevices: (w, then) => {
chrome.runtime.lastError = undefined;
then(currentDevice ? [currentDevice] : []);
},
openDevice: (w, then) => {
chrome.runtime.lastError = undefined;
then(w);
},
getConfiguration: (usbdevice, then) => {
chrome.runtime.lastError = undefined;
let patchedConfiguration = {
configurationValue: usbdevice._device.configuration.configurationValue,
interfaces: []
}
for (let i = 0; i < usbdevice._device.configuration.interfaces.length; i++) {
let patchedInterface = {endpoints: []};
for (let j = 0; j < usbdevice._device.configuration.interfaces[i].alternate.endpoints.length; j++) {
let endpoint = usbdevice._device.configuration.interfaces[i].alternate.endpoints[j];
let patchedEndpoint = {
address: endpoint.address,
type: endpoint.type,
direction: endpoint.direction,
maximumPacketSize: endpoint.maximumPacketSize
};
patchedInterface.endpoints.push(patchedEndpoint);
}
patchedConfiguration.interfaces.push(patchedInterface);
}
then(patchedConfiguration);
},
claimInterface: async (usbdevice, interfaceNumber, then) => {
chrome.runtime.lastError = {message: "fail to open interface"};
await usbdevice._device.open();
if (usbdevice._device.configuration === null) await usbdevice._device.selectConfiguration(1);
await usbdevice._device.claimInterface(interfaceNumber);
chrome.runtime.lastError = undefined;
then();
},
bulkTransfer: async (usbdevice, transferInfo, then) => {
//let endpoint = usbdevice._device.configuration.interfaces[0].alternate.endpoints.find(e => e.direction === transferInfo.direction).endpointNumber;
//console.warn(endpoint, transferInfo.direction)
if(transferInfo.direction === "out") {
usbdevice._device.transferOut(2, transferInfo.data).then(result => then({resultCode: result.status == "ok" ? 0 : 1})).catch(e => {chrome.runtime.lastError = "fail to write to usb "+e; then({resultCode: 1}); });
} else {
usbdevice._device.transferIn(1, transferInfo.length).then(result => then({resultCode: result.status == "ok" ? 0 : 1, data: result.data.buffer})).catch(e => {chrome.runtime.lastError = "fail to write to usb "+e; then({resultCode: 1}); });
}
chrome.runtime.lastError = undefined;
},
releaseInterface: (device, interfacenum, callback) => {
callback();
}
}
navigator.usb.addEventListener("disconnect", () => {
if(currentDevice) {
events.onDeviceRemoved.forEach(e => e(currentDevice));
}
currentDevice = null;
})
// file system api
window.chrome.fileSystem = {
chooseEntry: async (options, callback) => {
if(options.type === "saveFile") {
chrome.runtime.lastError = undefined;
callback({
createWriter: (callback2) => {
let writer = {
truncate: () => {},
position: 0,
onwriteend: () => {},
write: async (blob) => {
let arrayBuffer = new TI_File(new Uint8Array(await blob.arrayBuffer())).fileData;
// analyze blob to see if its a python appvar
let appvarid = arrayBuffer[2] << 24 | arrayBuffer[3] << 16 | arrayBuffer[4] << 8 | arrayBuffer[5];
if(appvarid === 1348027204) {
// convert to appvar to py
lib.FS.writeFile("./"+options.suggestedName, new Uint8Array(await blob.arrayBuffer()));
let pythonAppvar = lib.TIVarFile.loadFromFile("./"+options.suggestedName);
let pyData = pythonAppvar.getReadableContent();
blob = new Blob([pyData], {type: "application/x-python"});
options.suggestedName = options.suggestedName.substr(0,options.suggestedName.length-4)+".py";
options.accepts[0].extensions = ["py"];
}
showSaveFilePicker({suggestedName: options.suggestedName, types: [{accept: {"application/octet-stream": options.accepts[0].extensions.map(s => "."+s)}}]}).then(async fileEntry => {
let writable = await fileEntry.createWritable();
await writable.write(blob);
await writable.close();
writer.onwriteend();
});
}
}
callback2(writer);
}
});
} else if(options.type === "openFile") {
chrome.runtime.lastError = undefined;
let extensions = options.accepts[0].extensions.map(s => "."+s);
extensions.push(".py");
showOpenFilePicker({multiple: options.acceptsMultiple || false, types: [{description: options.accepts[0].description, accept: {"application/octet-stream": extensions}}]}).then(async fileEntries => {
let returnValue = [];
let files = new Promise((res, rej) => {
fileEntries.forEach(async fileEntry => {
if(fileEntry.name.substr(fileEntry.name.length-3, fileEntry.name.length).toLowerCase() === ".py") {
let newfilename = fileEntry.name.substr(0,fileEntry.name.length-3).substr(0,8).toUpperCase();
let pythonappvar = lib.TIVarFile.createNew(lib.TIVarType.createFromName("PythonAppVar"), newfilename, lib.TIModel.createFromName("84+CEPy"));
let filecontent = await fileEntry.getFile();
await filecontent.arrayBuffer().then(buffer => {
pythonappvar.setContentFromString(buffer);
});
let output = pythonappvar.saveVarToFile("", newfilename)
let rawdata = lib.FS.readFile(output, {encoding: "binary"});
returnValue.push({
file: async (s) => {
let out = new File([rawdata], newfilename+".8xv", {type: "application/octet-stream"});
s(out);
return out;
},
name: newfilename+".8xv"
});
} else {
returnValue.push({
file: async (s) => {
let out = await fileEntry.getFile();
console.log(out);
s(out);
return out;
},
name: fileEntry.name
});
}
if(returnValue.length === fileEntries.length) res();
})
})
files.then(() => {
if(options.acceptsMultiple) {
callback(returnValue);
} else {
callback(returnValue[0]);
}
});
});
}else if(options.type === "openDirectory") {
callback([1]);
} else {
console.log("invalid option: ", options)
}
},
getWritableEntry: (filehandle, callback) => {
let retval = {
getFile: async (filename, options, callback2) => {
callback2({
createWriter: async (callback3) => {
let writer = {
truncate: () => {},
position: 0,
write: async (blob) => {
let arrayBuffer = new TI_File(new Uint8Array(await blob.arrayBuffer())).fileData;
// analyze blob to see if its a python appvar
let appvarid = arrayBuffer[2] << 24 | arrayBuffer[3] << 16 | arrayBuffer[4] << 8 | arrayBuffer[5];
if(appvarid === 1348027204) {
// convert to appvar to py
lib.FS.writeFile("./"+filename, new Uint8Array(await blob.arrayBuffer()));
let pythonAppvar = lib.TIVarFile.loadFromFile("./"+filename);
let pyData = pythonAppvar.getReadableContent();
blob = new Blob([pyData], {type: "application/x-python"});
filename = filename.substr(0,filename.length-4)+".py";
}
showSaveFilePicker({suggestedName: filename}).then(async fileEntry => {
let writable = await fileEntry.createWritable();
await writable.write(blob);
await writable.close();
writer.onwriteend();
}).catch(err => setTimeout(() => writer.write(blob), 100));
},
onwriteend: () => {}
}
callback3(writer);
}
})
}
}
callback(retval)
}
}
function waitForElementToExist(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
subtree: true,
childList: true,
});
});
}
window.latealways_patch = {
setUsbDevice: (w) => {
return navigator.usb.requestDevice({filters: w}).then(device => {
window.chrome.runtime.lastError = undefined;
if(currentDevice) {
events.onDeviceRemoved.forEach(e => e(currentDevice));
}
let device_ret = {
_device: device,
configuration: device.configuration,
};
currentDevice = device_ret
events.onDeviceAdded.forEach(e => e(device_ret));
setTimeout(() => angular.element("#refresh").controller().refresh(), 500);
return device
})
},
pairDevice: () => latealways_patch.setUsbDevice([{vendorId: 1105, productId: 57347}, {vendorId: 1105, productId: 57352}])
}
window.hookfunction = (parent, func, newfunc) => {
let oldfunc = parent[func];
parent[func] = newfunc.bind(parent);
return oldfunc;
}
document.addEventListener("DOMContentLoaded", (event) => {
document.title = "TI Connect CE";
let icon = document.createElement("link")
icon.rel = "icon"
icon.type = "image/x-icon"
icon.href = manifest.icons[Object.keys(manifest.icons).map(s => Number(s)).toSorted((a,b) => a>b).reverse()[0]]
document.head.appendChild(icon);
waitForElementToExist("#toolbar_add_from_comp").then(e => {
angular.element('#toolbar_add_from_comp').controller().fileTypes.PROTECTED_PROGRAM = {name:"Protected Program",icon:"images/filetype_program.svg"}
})
let oldHandleDrop;
oldHandleDrop = hookfunction(angular.element("body").scope(), "handleDrop", async (e) => {
let newe = {
preventDefault: () => {},
stopPropagation: () => {}
};
e.preventDefault();
e.stopPropagation();
let items = e.dataTransfer.items;
newe.dataTransfer = {
items: []
}
for(let i = 0; i < items.length; i++) {
let name = items[i].getAsFile().name;
if(name.substr(name.length-3, name.length).toLowerCase() === ".py") {
let newfilename = name.substr(0,name.length-3).substr(0,8).toUpperCase();
let pythonappvar = lib.TIVarFile.createNew(lib.TIVarType.createFromName("PythonAppVar"), newfilename, lib.TIModel.createFromName("84+CEPy"));
let filecontent = await items[i].getAsFile().arrayBuffer();
pythonappvar.setContentFromString(filecontent);
let output = pythonappvar.saveVarToFile("", newfilename)
let rawdata = lib.FS.readFile(output, {encoding: "binary"});
newe.dataTransfer.items.push({
webkitGetAsEntry: () => {
return {
name: newfilename+".8xv",
file: async (s) => {
let out = new File([rawdata], newfilename+".8xv", {type: "application/octet-stream"});
s(out);
return out;
}
}
}
});
} else {
newe.dataTransfer.items.push(items[i]);
}
}
return oldHandleDrop(newe);
})
});