forked from lunu-bounir/clipboard-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.js
More file actions
83 lines (79 loc) · 1.88 KB
/
monitor.js
File metadata and controls
83 lines (79 loc) · 1.88 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
/* globals manager, prefs */
'use strict';
const monitor = {
id: 'desktop.clipboard.manager',
port: null
};
monitor.observe = () => {
monitor.port = chrome.runtime.connectNative(monitor.id);
monitor.port.onDisconnect.addListener(async () => {
console.error('native client exited unexpectedly or it is not installed');
const win = await chrome.windows.getCurrent();
chrome.windows.create({
url: 'data/installer/index.html',
width: 800,
height: 600,
left: win.left + Math.round((win.width - 800) / 2),
top: win.top + Math.round((win.height - 600) / 2),
type: 'popup'
});
});
monitor.port.onMessage.addListener(r => {
if (r) {
if (r.result) {
manager.add({
body: r.result
});
}
else if (r.error) {
console.error(r.error);
}
monitor.port.disconnect();
monitor.observe();
}
});
const request = {
method: 'read-next'
};
if (prefs['max-buffer-size']) {
request.max = prefs['max-buffer-size'];
}
monitor.port.postMessage(request);
};
monitor.install = async () => {
const prefs = await chrome.storage.local.get({
monitor: 'native'
});
if (chrome.scripting) {
await chrome.scripting.unregisterContentScripts();
}
if (prefs.monitor === 'native') {
monitor.observe();
}
else {
if (monitor.port) {
monitor.port.disconnect();
}
await chrome.scripting.registerContentScripts([{
'id': 'main',
'js': ['/data/inject.js'],
'matches': ['*://*/*'],
'allFrames': true,
'matchOriginAsFallback': true,
'runAt': 'document_start'
}]);
}
};
monitor.remove = () => {
if (monitor.port) {
monitor.port.disconnect();
}
if (chrome.scripting) {
chrome.scripting.unregisterContentScripts();
}
};
chrome.storage.onChanged.addListener(ps => {
if (ps.monitor) {
monitor.install();
}
});