forked from thomaswilburn/Caret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
171 lines (147 loc) · 5.01 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
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
/*
The background process is responsible for opening Caret windows in response to
app launches, choosing a file in the Files app on Chrome OS, and external
messages.
*/
var mainWindow = null;
var pending = null;
var files = [];
var commands = [];
var openWindow = function() {
//if window exists, re-use it
if (mainWindow) {
//attach any new files to the window, and re-trigger "open from launch"
mainWindow.contentWindow.launchData = files;
mainWindow.contentWindow.require(["command"], function(c) {
c.fire("session:open-launch");
});
mainWindow.focus();
mainWindow.drawAttention();
files = [];
pending = null;
return;
}
//otherwise, open a new window
var defaults = {
width: 800,
height: 600,
left: 50,
top: 50
};
chrome.app.window.create("main.html", {
bounds: defaults,
id: "caret:main",
frame: "none",
minWidth: 640,
minHeight: 480
}, function(win) {
mainWindow = win;
win.contentWindow.launchData = files;
win.contentWindow.launchCommands = commands;
mainWindow.onClosed.addListener(function() {
mainWindow = null;
chrome.storage.local.remove("isOpen");
});
files = [];
commands = [];
pending = null;
chrome.storage.local.set({isOpen: true});
});
}
var launch = function(launchData) {
if (launchData && launchData.items) files.push.apply(files, launchData.items);
//we delay opening the actual window to give multiple file events time to fire
if (pending !== null) return;
pending = setTimeout(openWindow, 250);
};
chrome.app.runtime.onLaunched.addListener(launch);
var onMessage = function(message, sender, sendResponse) {
//main window will pick up the message, if it's open
//we also allow extensions to suppress launch behavior for spurious messages
if (mainWindow || message.quiet || !message.command) {
//if it is open, and the command is loud enough, flash the window
if (mainWindow && message.command && !message.quiet) {
mainWindow.drawAttention();
}
return;
}
commands.push({
message: message,
sender: sender,
sendResponse: sendResponse
});
//as with files, delay to accumulate multiple messages
if (pending !== null) return;
pending = setTimeout(openWindow, 250);
};
chrome.runtime.onMessageExternal.addListener(onMessage);
//relaunch on reboot, if the window was open at shutdown
var checkRestart = function() {
chrome.storage.local.get("isOpen", function(data) {
if (data.isOpen) launch();
});
};
chrome.app.runtime.onRestarted.addListener(checkRestart);
checkRestart();
// setup for launcher context menus
// currently this is just for emergency reset
chrome.contextMenus.create({
title: "Emergency Reset",
contexts: [ "launcher" ],
id: chrome.runtime.id + ":factory-reset"
}, function() {
if (chrome.runtime.lastError) console.log(chrome.runtime.lastError);
});
window.emergencyReset = function() {
if (mainWindow) mainWindow.close();
var cleared = {
local: false,
sync: false
};
var check = function(storage) {
cleared[storage] = true;
if (cleared.local && cleared.sync) {
chrome.notifications.create("app:factory-reset-complete", {
type: "basic",
iconUrl: "icon-128.png",
title: "Emergency Reset Complete",
message: "Caret has been reset to the default settings."
}, function() {});
}
};
chrome.storage.local.clear(check.bind(null, "local"));
chrome.storage.sync.clear(check.bind(null, "sync"));
};
chrome.contextMenus.onClicked.addListener(function(data) {
if (data.menuItemId != chrome.runtime.id + ":factory-reset") return;
emergencyReset();
});
var installNotification = "upgraded";
window.showUpdateNotification = function(manifest = chrome.runtime.getManifest()) {
chrome.notifications.create(installNotification, {
type: "basic",
iconUrl: "icon-128.png",
title: chrome.i18n.getMessage("notificationUpdated"),
message: chrome.i18n.getMessage("notificationUpdatedDetail", [manifest.version]),
isClickable: true
}, function(id) { installNotification = id });
};
chrome.runtime.onInstalled.addListener(function(e) {
if (!e.previousVersion) return;
// set a flag for launch updates
window.updateVersion = e.previousVersion;
var manifest = chrome.runtime.getManifest();
// at some point, we should use these.
var [major, minor, build] = e.previousVersion.split(".");
if (e.previousVersion != manifest.version) {
chrome.storage.sync.get("updateNotifications", function(data) {
if (data.updateNotifications && data.updateNotifications != "background") return;
//let the user know
showUpdateNotification(manifest);
});
}
});
chrome.notifications.onClicked.addListener(function(id) {
if (id != installNotification) return;
window.open("https://github.com/thomaswilburn/Caret/blob/master/changelog.rst", "target=_blank");
});