-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
840 lines (740 loc) · 28 KB
/
main.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/* -*- coding: iso-safe -*- */
// TODO: A lot of unused code.
// TODO: Starting the Daemon from `gui.init` is counter-intuitive.
// TODO: Refactor:
// TODO - tabs.suspend should really only suspend tabs.
// TODO - a funciton inside the sott.* namespace should handle
// TODO diciding whether suspending the tab is allowed.
// TODO: Whitelist option.
// See https://stackoverflow.com/a/2593661/2075630 for
// a `regexp-quote' implementation.
'use strict';
//// ====== UTILITY ==================================================
const util = (function(){
const util = {};
util.die = function die(...args) {
const msgParts = [];
for(const a of args){
msgParts.push(
typeof a === 'string' ? a : JSON.stringify(a));
}
debug.log(...msgParts);
throw new Error(msgParts.join(' '));
}
util.getProp = function getProp(obj,field){
/** Obtain obj[field], raise error if undefined. */
const val = obj[field];
if(val === undefined){
util.die('No field', JSON.stringify(field),'in', obj);
}
return val;
};
util.logArgs = function logArgs(func){
/** On error, log the arguments. */
const wrapped = function(...args){
try{
return func(...args);
} catch(err){
console.log('Caught error on arguments\n ',
args,
'\nfor function\n ', func);
throw err;
}
}
return wrapped;
};
util.range = function range(){
/** Usage:
* util.range(STOP)
* util.range(START,STOP)
* util.range(START,STOP,STEP)
*/
const n = arguments.length;
const [start,stop,step] =
n == 1 ? [0, ...arguments, 1]
: n == 2 ? [...arguments, 1]
: n == 3 ? arguments
: util.die(`Expected 1..3 arguments, got ${n}`);
const r = [];
for(let m = start; m <= stop; m+=step){
r.push(m);
}
return r;
}
util.mapObj = function mapObj(object, callback){
const r = [];
for(const field in object){
r.push(callback(field,object[field]));
}
return r;
}
util.html2dom = function html2dom(htmlString){
/** Convert HTML string to DOM object. */
const div = document.createElement('div');
div.innerHTML = htmlString;
return div.childNodes.length != 1 ? div : div.childNodes[0];
};
util.pad = function pad(value, len, fill=' ',align='right'){
/** Pad value.toString to len characters, right-aligned.
@param{value} Some value with toString method.
@param{len} Requested string length.
@param{optional fill} Determine what character to pad
with, defaults to space.
@param{optional align} Determines orientation of
filling. Defaults to 'right'.
*/
let s = value.toString();
fill = fill.toString()[0];
while(s.length < len){
if(align==='right'){
s = fill + s;
}
else if(align==='left'){
s = s + fill;
}
else{
throw new Error(`Invalid parameter, align=${align}`);
}
}
return s;
};
util.identity = function identity(arg){
/** identity(X) == X */
return arg;
};
util.loadScript = function loadScript(path){
/** Loads javascript from `path`. */
const s = document.createElement('script');
s.src = path;
document.head.appendChild(s);
}
util.pprint = async function(value){
/** Pretty-print a value as JSON. */
const expanded = await util.pprintExpand(value);
console.log("--------------------------------------------------\n");
console.log(JSON.stringify(expanded, null, 2));
console.log({pprint:value});
}
util.pprintExpand = async function(obj, json=true, promise=true, circles=true){
/** Converts an object tree recursively into a pprintable form.
Unless disabled by ``promise=False``, promises are replaced by
{'[[Promise]]': value}
Unless disabled by ``json=False``,
1. Maps are replaced by {'[[Map]]': [[Key, Value], ...]}.
2. Non-serializable types are replaced by their `toString`.
Unless disabled by ``circles=false``, circular references are
replaced by '[[CircularReference]]'. This is subject to change.
*/
const knownObjects = new Map();
return _recur(obj);
async function _recur(obj){
/** Steps recursively through the tree. */
// console.log({'DEBUG':1, s:obj.toString(), obj:JSON.stringify(obj),
// known:Array.from(knownObjects),
// promise: await obj !== obj
// });
// Circular references
if(knownObjects.has(obj) && typeof obj === 'object'){
if(circles) {
return {'[[CircularReference]]': obj.toString()};
}
else {
return obj;
}
}
let ret;
knownObjects.set(obj, true);
// Promises
if(promise && await obj !== obj){
ret = {'[[Promise]]': await _recur(await obj)};
}
// undefined, null as special cases
else if(obj === null){
ret = obj;
}
else if(obj === undefined){
ret = json ? '[[undefined]]' : obj;
}
// JSON-compatible non-objects
else if(['number', 'string', 'boolean'].includes(typeof obj)){
ret = obj;
}
// JSON-incompatible non-objects
else if(typeof obj !== 'object'){
ret = {};
ret[`[[${typeof obj}]]`] = obj.toString();
}
// Arrays
else if(Array.isArray(obj)){
ret = Promise.all(obj.map(_recur));
}
// Maps
else if(obj instanceof Map){
if(json){
let keyval = [];
ret = {'[[Map]]': keyval};
for(let [k,v] of obj){
keyval.push([await _recur(k), await _recur(v)]);
}
}
else{
ret = new Map();
for(let [k,v] of obj){
ret.set(await _recur(k), await _recur(v));
}
}
}
// Dates
else if(obj instanceof Date){
ret = obj.toLocaleString();
}
// General objects
else {
ret = {};
for(let field in obj){
ret[field] = await _recur(obj[field]);
}
}
return ret;
}
}
return Object.freeze(util);
})();
//// ====== DEBUG ====================================================
const debug = (function(){
const debug = {};
const _devel = new Promise(function(resolve){
chrome.management.getSelf(resolve);
}).then(function(self){
return self.installType == 'development';
});
debug.isDevel = async function isDevel(){
/** true, when installed in developer mode */
// return _devel;
return (await options).debugMode;
}
debug.ifDevel = async function ifDevel(callback){
/** Run ``callback`` if developer mode. */
if(await debug.isDevel){
return callback();
}
}
debug.log = async function log(...args){
/** If we are in developer mode, do some logging. */
if(await debug.isDevel()){
console.log('debug.log:', ...args);
}
}
return debug;
})();
//// ====== OPTIONS ==================================================
const options = (function(){
/** Options are accessed as promise,
*
* const opt = wait options;
*
* or
*
* options.then(function(opt){
* do_something(opt);
* });
* */
const options = {};
const INFO = options.INFO = Object.freeze({
waitMinutes: Object.freeze({
cast: Number,
valueField: 'valueAsNumber',
default: 5,
html1: 'Waiting time until suspension in minutes',
type: 'number'
}),
suspendPinnedTabs: Object.freeze({
cast: Boolean,
valueField: 'checked',
default: false,
html1: 'Suspend pinned tabs?',
type: 'checkbox',
}),
pollInterval: Object.freeze({
cast: Number,
valueField: 'valueAsNumber',
default: 60,
html1: 'Polling interval in seconds',
type: 'number'
}),
debugMode : Object.freeze({
cast: Boolean,
valueField: 'checked',
default: false,
html1: 'Enable developer mode?',
type: 'checkbox'
})
});
options.then = function then(callback){
const names = Object.getOwnPropertyNames(INFO);
return new Promise(function(resolve,reject){
chrome.storage.sync.get('options', function(obj){
if(chrome.runtime.lastError){
reject(chrome.runtime.lastError);
} else {
resolve(obj.options !== undefined ? obj.options : {});
}
});
}).then(function(syncedOpt){
const opt = {};
names.forEach(function(name){
const cast = INFO[name].cast;
const val = syncedOpt[name];
opt[name] = val !== undefined ? cast(val) : cast(INFO[name].default);
});
return callback(opt);
});
}
options.set = function(obj){
/** Update option keys, e.g.
*
* options.set({pollInterval:30, waitMinutes:5});
*
* Returns a promise for the updated options.
*/
// Verify keys of obj.
const names = Object.getOwnPropertyNames(INFO);
const nameSet = new Set(names);
for(const name in obj){
if(! nameSet.has(name)){
throw Error(`Not a valid option key: ${name}`);
}
}
return options.then(function(opt){
for(const name in obj){
opt[name] = INFO[name].cast(obj[name]);
}
chrome.storage.sync.set({options:opt}, function(){
if(chrome.runtime.lastError){
throw chrome.runtime.lastError;
}
});
return opt;
});
}
options.reset = function reset(){
/** Reset options, by clearing the sync storage.
* Returns promise that is fulfilled, when the value has been
* synced. */
return new Promise(function(resolve,reject){
chrome.storage.sync.set({options:{}}, function(){
if(chrome.runtime.lastError){
reject(chrome.runtime.lastError);
} else {
debug.log('SYNCED_OPTION',obj.options);
resolve(undefined);
}
});
});
}
return Object.freeze(options);
})();
//// ====== GUI ELEMENTS AND INIT ====================================
const gui = (function(){
const gui = {};
gui.init = function init(){
let hashItems = location.hash.split('&');
let doctype = hashItems[0].replace('#','');
debug.log('gui.init', {doctype});
let initializer = gui.initializers[doctype];
if(typeof initializer !== 'function'){
console.error(Error(`Not a function: gui.initializers.${doctype}`));
location.href = '#debugpage';
location.reload();
} else {
initializer(document.body);
}
}
gui.initializers = {};
gui.initializers.backgroundpage = function backgroundpage(){
debug.log('Nothing to do for #background');
sott.initDaemon();
chrome.tabs.onActivated.addListener(function(){
tabs.updateLastActive();
});
tabs.updateLastActive();
// TODO: Set up communication, where background page is told
// about changed options.
};
gui.initializers.debugpage = function debugpage(parent){
debug.log('Debug page!');
for(const field in gui.initializers){
const initializer = gui.initializers[field];
if(initializer !== gui.initializers.debugpage){
debug.log("Initializer:", field);
initializer(parent);
}
}
}
gui.initializers.optionspage = function optionspage(parent) {
const content = [];
const c = (...args) => content.push(...args);
options.then(function(opts){
// Build the page contents
c('<h1>Options</h1>',
'<table>',
' <tr><th>Description</th><th>Value</th></tr>');
debug.log('options.INFO', options.INFO);
debug.log('opts', opts);
for(const name in options.INFO){
const opt = options.INFO[name];
c(' <tr>',
` <td class='options.description'>${opt.html1}</td>`,
` <td class='options.value'>`,
` <input opt='${name}' type='${opt.type}' class='options.input'>`,
` </td>`,
` </tr>`);
}
c(`<tr><td></td><td><button class='options.reset'>Reset Options</td></tr>`,
`</table>`);
c(`<div>For bug-reports, see `
+`<a href='https://github.com/kbauer/Silence-of-the-Tabs#knownissues'>`
+`[HERE]</a></div>`);
const div = document.createElement('div');
div.class = 'options';
div.innerHTML = content.join('\n');
function updateGuiValues(){
options.then(function(opt){
for(const elem of div.getElementsByClassName('options.input')){
const name = elem.getAttribute('opt');
const inf = options.INFO[name];
elem[inf.valueField] = inf.cast(opt[name]);
debug.log('Updated',util.pad(opt[name],5),elem);
}
});
}
updateGuiValues();
// Add event handlers
for(const elem of div.getElementsByClassName('options.input')){
elem.addEventListener('change', function optChangeHandler(event){
const name = elem.getAttribute('opt');
const inf = options.INFO[name];
const val = inf.cast(elem[inf.valueField]);
options.set({[[name]]:val}).then(function(){
updateGuiValues();
});
});
}
for(const elem of div.getElementsByClassName('options.reset')){
elem.addEventListener('click', function optResetHandler(event){
const r = options.reset()
debug.ifDevel(async function(){
await r;
updateGuiValues();
debug.log('Options have been reset:', await options);
});
});
}
parent.append(div);
return div;
});
};
gui.initializers.popuppage = async function popuppage(parent){
const div = document.createElement('div');
div.style.width = '20em';
div.style.textAlign = 'center';
const buttons = [
{
text: 'Suspend current tab',
action: () => chrome.tabs.getSelected(function(tab){
tabs.suspendSafe(tab,{
debugText:'Suspended manually',
prefix:'Suspended without autoreload',
noAutoReload:true,
force:true
});
})
},
{
text: 'Suspend other tabs',
action: async function(){
const current = await new Promise(r => chrome.tabs.getSelected(r));
const all = await new Promise(r => chrome.tabs.getAllInWindow(null,r));
for(const tab of all){
if( tab.id != current.id ){
tabs.suspendSafe(tab,{
debugText:'Suspended manually',
force:true
});
}
}
}
},
... ! await debug.isDevel() ? [] : [{
text: 'Devel: Suspend current tab (autoreload)',
action: function(){
chrome.tabs.getSelected(null, function(tab){
tabs.suspendSafe(tab,{
debugText:'Suspended manually (Devel: With autoreload)',
force:true
});
});
}
}],
];
for(const {text, action} of buttons){
const a = document.createElement('a');
const d = document.createElement('div');
a.href = '#';
a.innerText = text;
a.addEventListener('click', action);
d.append(a);
div.append(d);
}
parent.append(div);
return Promise.resolve(div);
};
window.addEventListener('DOMContentLoaded', gui.init);
return Object.freeze(gui);
})();
//// ====== TABS =====================================================
const tabs = (function(){
const tabs = {};
tabs.map = async function map(callback=util.identity){
/** Returns a ``Promise`` for the list of results of
* ``callback`` applies to all tabs.
*/
return new Promise(function(resolve){
return chrome.windows.getAll(null, resolve);
}).then(function(windows){
return Promise.all(windows.map(function(window){
return new Promise(function(resolve){
chrome.tabs.getAllInWindow(window.id, function(tabs){
resolve(Promise.all(tabs.map(callback)));
});
})
// Once ``callback`` has been executed for all tabs in
// all windows, we need to flatten the per-window
// result lists.
})).then(function(nestedResults){
const results = [];
for(const r of nestedResults){
results.push(...r);
}
return results;
});
});
};
let _lastActive = {};
tabs.lastActive = function(tab){
/** Calculates the times tab was last seen to be active.
*/
return _lastActive[tab.id] =
tab.active ? new Date() :
_lastActive[tab.id] ? _lastActive[tab.id] :
new Date();
}
tabs.updateLastActive = function(){
/** Update and clean up cached information.
* Implies removal of no-longer-existing tab IDs.
* Returns [lastActiveMap, tabList] as promise.
*/
const newMap = {};
return tabs.map(function(tab){
newMap[tab.id] = tabs.lastActive(tab);
return tab;
}).then(function(tabList){
_lastActive = newMap;
return [newMap,tabList];
});
}
tabs.format = function format(tab){
/** Produce a formatted string representing a tab. */
const titleMaxLen = 80;
const num = util.pad(tab.id, 4, ' ');
const wnum = util.pad(tab.windowId, 3, ' ');
const title = tab.title.length <= titleMaxLen ? tab.title
: tab.title.substring(0, titleMaxLen);
const marks =
(tab.active ? 'A' : ' ')
+(tab.pinned ? 'P' : ' ');
const lastActive = tabs.lastActive(tab).toLocaleTimeString();
return `${wnum} ${num} ${marks} ${lastActive} ${title}`;
};
tabs.dump = function dump(){
/** Dumps all tabs. **/
tabs.map(tabs.format).then(function(strings){
const header = "WIN TAB LASTACTIVE TITLE";
strings = [header,...strings,header];
console.log(strings.join('\n'));
});
};
// Important: Must not allow suspension of data-uris, to prevent
// suspension-inception.
tabs.suspendProtocols = new Set(['https:','file:','http:','ftp:']);
tabs.suspendSafe = async function suspend(tab,{noAutoReload,prefix,force,debugText}={}){
/** Suspend a tab, by redirecting to a data-uri.
*
* @params{tab}
* The tab to suspend. An object of the form as returned by the
* ``chrome.tabs`` API.
* @params{noAutoReload}
* Toggle suppression of auto-reloading suspended tabs on focus.
* @params{prefix}
* A text to display before the back-link in suspended tabs.
* @params{force}
* Force suspension of tab, even if it isn't old enough yet for
* regular suspension. One cannot force suspending pinned tabs
* when the `suspendPinnedTabs` option is false, nor can one
* suspend protocols not in `tabs.suspendProtocols`.
* @params{debugText}
* A text to display in the suspended tab in developer mode.
*
* TODO: tabs.suspend should handle only redirecting,
* TODO not deciding whether to redirect.
*/
prefix = prefix || 'Suspended';
const h = (string) => {
/** Quote string for html. */
const tmp = document.createElement('div');
tmp.innerText = string;
return tmp.innerHTML;
};
const a = (string) => {
/** Escape string as attribute. */
return `"${encodeURI(decodeURI(string))}"`;
}
const iconmeta = tab.favIconUrl
? `<link rel='shortcut icon' type='image/x-icon' href=${a(tab.favIconUrl)}>`
: '';
const suspendPageSource = `
<html>
<head>
<title>${h(tab.title)}</title>
${iconmeta}
</head>
<body>
${prefix}: <a id='B' href=${a(tab.url)}>B</a>
<script>
var B=document.getElementById('B');
B.innerText=document.title;
/* Guard: Prevent 'focus' from firing twice
* and save some characters by using 0 being
* falsy.
* When 'noAutoReload' is set, disable it from the start.
* */
var G=${noAutoReload ? '1' : '0'};
addEventListener('focus',function(){
if(!G++){
if(history.length>1){
history.back();
} else {
location.href=B.href;
}
}
});
</script>
</body>
</html>`
/* We can safely replace spaces now, because
* those in href are protected by encodeURI. */
.replace(/\/\*(.|\n)*?\*\//g,'')
.replace(/>\s+/g,'>')
.replace(/\s+</g,'<')
.replace(/;\s+/g,';')
.replace(/{\s+/g,'{')
.replace(/\s+}/g,'}')
.replace(/\s+/g,' ')
/* Hashes (#) break the data URI and need to be encoded. */
.replace(/#/g, '%23');
debug.log('suspendPageSource = ', suspendPageSource);
const dataURI = `data:text/html;charset=UTF8,${suspendPageSource}`;
// Saveguard: Suspend only suspendable tabs / don't nest suspending.
let msg = null;
const opt = await options;
const diffMinutes = (new Date().getTime() - tabs.lastActive(tab).getTime())/60000.0;
const protocol = new URL(tab.url).protocol;
let doSuspend = false;
if(tab.active && ! force) {
msg = ' A';
} else if(tab.pinned && ! opt.suspendPinnedTabs){
msg = ' P';
if(force){
console.log('SUSPEND_ACTION_REJECTED\n '+
`Cannot force suspending pinned tabs, when the option is disabled.`);
}
} else if (! force && diffMinutes <= opt.waitMinutes){
msg = ' Y';
} else if (! tabs.suspendProtocols.has(protocol)){
// Protected protocols cannot be forced.
msg = 'D ';
if(force){
console.log('SUSPEND_ACTION_REJECTED\n '+
`Cannot force suspending ${JSON.stringify(protocol)}-URI`);
}
} else if(force){
msg = 'F';
doSuspend = `Forced suspend.`;
} else {
msg = 'S ';
doSuspend = `Suspended for Age: ${diffMinutes} > ${opt.waitMinutes} minutes.`;
}
if(doSuspend){
console.log('SUSPEND_ACTION\n ' + doSuspend + '\n ' + tabs.format(tab));
chrome.tabs.update(tab.id, {url:dataURI});
}
return msg + ' ' + tabs.format(tab);
};
return Object.freeze(tabs);
})();
//// ====== SOTT CORE ================================================
const sott = (function(){
const sott = {};
let _currentDaemon = null;
sott.initDaemon = function(){
/** Start Periodically running background function. */
let counter = 0; // Just for debug purposes.
daemonInternal();
if(_currentDaemon){
window.clearInterval(_currentDaemon);
}
options.then(function(opt){
window.setInterval(daemonInternal, opt.pollInterval*1000);
});
async function daemonInternal(){
debug.log(`DAEMON_EXECUTE #${++counter}`);
const report = await tabs.map(tabs.suspendSafe);
if(await debug.isDevel()){
debug.log(
'SUSPEND_CHECK_REPORT\n'
+'(S)uspend this normally or (F)orced or (A)ctive'
+' (P)inned (Y)oung (D)on\'t suspend or already suspended\n'
+`options ${JSON.stringify(await options)}\n`
+report.join('\n'));
}
}
};
return Object.freeze(sott);
})();
//// ====== TEMPORARY ================================================
const temp = (function(){
if(location.hash.split('&')[0] != '#debugpage'){
debug.log('temp module executed only on #debugpage');
return null;
}
const temp = {};
window.addEventListener('load',async function tempOnLoad(){
document.body.style.borderStyle = 'dashed';
const screenshot1 = await html2canvas(document.body);
document.body.appendChild(screenshot1);
const screenshot2 = document.createElement('canvas');
screenshot2.width = 200;
screenshot2.height = 150;
const c = screenshot2.getContext('2d');
c.drawImage(screenshot1, 0, 0, screenshot2.width, screenshot2.height);
debug.log(`image size = ${screenshot2.toDataURL().length}`);
const img = document.createElement('img');
img.src = screenshot2.toDataURL();
img.style.width = '100%';
img.style.height = '100%';
document.body.append(img);
chrome.storage.local.set({'screenshot2':screenshot2.toDataURL()});
});
return temp;
})();
//// ====== END ======================================================