-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
76 lines (54 loc) · 2.22 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
(function(global) {
/// handles each instance of the options page
global.initOptionsView = async window => {
const { document, } = window;
const request = document.querySelector('#request');
const output = document.querySelector('#output');
const content = document.querySelector('#content');
const start = document.querySelector('#start'), stop = document.querySelector('#stop');
const Native = await global.require.async('node_modules/native-ext/');
log('NativeExt library loaded');
/// #request access to NativeExt
request.onclick = () => Native.requestPermission({ message: 'Wanna do great stuff', })
.then(status => log('requestPermission result:', status));
/// #start & #stop watching file #path
start.onclick = async () => { try {
start.disabled = true;
let path = document.querySelector('#path').value;
log('Blocking until the permission is granted ...');
await Native.do(async process => {
const { homedir, browser, } = await process.require(require.resolve('/utils.node.js'));
path = path.replace(/^~(?=[\\/])/, () => homedir);
log('Current browser', browser);
log('Loading', path);
const fs = await process.require(require.resolve('/fs.node.js'));
content.textContent = await fs.readFile(path, 'utf-8');
log('Watching the file. Changes should be updated in the textarea automatically');
fs.watch(path, { }, onChange);
start.style.display = 'none'; stop.style.display = 'unset';
await new Promise((onclick, onExit) => {
stop.onclick = onclick;
process.onExit(onExit);
window.onunload = () => onExit(new Error('Window closed'));
});
log('Closing file watcher');
fs.unwatch(onChange);
async function onChange(type, name) {
log('File', name, type +'d');
content.textContent = await fs.readFile(path, 'utf-8');
}
});
} catch(error) {
log('ERROR:', error.message, '\n', error.stack);
} finally {
start.disabled = false; start.style.display = 'unset';
stop.style.display = 'none'; stop.onclick = null;
content.textContent = '';
} };
/// logs stuff to the #output box
function log(...args) {
console.log('#output +=', ...args);
output.textContent += args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') +'\n';
}
};
})(this);