-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
302 lines (246 loc) · 8.07 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
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
var ghostifyBookmarks;
var ghostBookmarks;
var currentTab;
var isHidden;
var _debug;
var urlsToGhostify; urlsToGhostify = []; // TODO distinguish promise not fulfilled/arrived yet (undefined) and empty.
var urlGhosts; urlGhosts = [];
function debug(message)
{
if (!_debug)
return
console.log(message);
}
function onError(error) // TODO Reuse in options.js using browser.extension.getBackgroundPage().onError.
{
console.log(`Error: ${error}`);
}
function init()
{
// request and hopefully get the options:
var gettingUrlsToGhostify = browser.storage.local.get("urlsToGhostify")
.then(
function(item1) {
var urlsToGhostifyCandidates = [];
urlsToGhostifyCandidates = item1.urlsToGhostify.trim().split(/\s+/);
urlsToGhostify = urlsToGhostifyCandidates;
}
, onError
).then(updateGhostifyBookmarks, onError);
var gettingUrlGhosts = browser.storage.local.get("urlGhosts")
.then(
function(item2) {
/*alert(uneval(item2));*/
var urlGhostsCandidates = [];
urlGhostsCandidates = item2.urlGhosts.trim().split(/\s+/);
urlGhosts = urlGhostsCandidates;
}
, onError
).then(updateGhostBookmarks, onError);
var gettingIsHidden = browser.storage.local.get("isHidden").then(
function(item3) {
/*alert(uneval(item3));*/
// store the bookmarks that are to be ghostified:
isHidden = item3.isHidden;
}
, onError
);
var gettingDebug = browser.storage.local.get("debug").then(
function(item4) {
_debug = item4.debug;
}
, onError
);
Promise.all([gettingUrlsToGhostify, gettingUrlGhosts, gettingIsHidden])
// TODO Is this now giving the itemX or the result of the updateXBookmark promise?
//.spread(function(item1, item2, item3) {
// debug(item1, item2, item3);
// swapBookmarks(item1, item2, item3);
//};
.then(
exchangeBookmarks, onError
);
}
function updateIcon()
{
browser.browserAction.setIcon({
path: isHidden ? {
19: "icons/star-filled-19.png",
38: "icons/star-filled-38.png"
} : {
19: "icons/star-empty-19.png",
38: "icons/star-empty-38.png"
}
//,tabId: currentTab.id
});
}
function hide()
{
isHidden = true;
// Required because it is unknown when the browser terminates/resets and persistence is desired.
browser.storage.local.set({
isHidden: true
});
updateIcon();
}
function unhide()
{
isHidden = false;
// Required because it is unknown when the browser terminates/resets and persistence is desired.
browser.storage.local.set({
isHidden: false
});
updateIcon();
}
/*
* Workaround variable invalid/not given as snapshot/copy when promise resolves.
*/
function swapBookmarks(urlsToGhostifyKeys)
{
// serial asynchronous call, see stackoverflow.com/questions/24660096/correct-way-to-write-loops-for-promise#answer-24985483
return urlsToGhostifyKeys.reduce(function(promise, index) {
return promise.then(function() {
var bookmark = ghostifyBookmarks[index];
var ghostBookmark = ghostBookmarks[index];
var urlToGhostify = urlsToGhostify[index];
var urlGhost = urlGhosts[index] || ""; // Replacing by no url is supported if bookmark validator not prevents.
// The following is kept as feedback for when the bookmark data is loaded. (Ghost bookmarks must not necessarily exist.)
if (!bookmark)
{
debug("Error: No bookmark loaded: " + bookmark);
debug("ghostifyBookmarks: " + uneval(ghostifyBookmarks) + "ghostBookmarks: " + uneval(ghostBookmarks));
debug("urlsToGhostify: " + uneval(urlsToGhostify) + " urlGhosts: " + uneval(urlGhosts));
return;
}
// URLs to replace a bookmark need not to have a bookmark set for it.
if (!ghostBookmark)
{
//urlGhost is the correct term because a ghostBookmark is optional. ghostBookmarkUrl = ghostBookmark.url
var creating = browser.bookmarks.create({title: bookmark.title, url: urlGhost});
creating.then(function(ghostBookmarkNew) {
debug("Created ghost bookmark as a copy of bookmark: " + ghostBookmarkNew);
swap_bookmarks(bookmark, ghostBookmarkNew);
});
}
else
{
swap_bookmarks(bookmark, ghostBookmark);
}
});
}, Promise.resolve());
}
/*
* Toggle the bookmark on the current page.
*/
function exchangeBookmarks()
{
if (urlsToGhostify.length < 1 || urlGhosts.length < 1)
{
debug('No options loaded yet or are empty.');
browser.runtime.openOptionsPage();
return;
}
var indices = [];
for (var index = 0; index < urlsToGhostify.length; ++index)
{
indices[index] = index;
}
//return
swapBookmarks(indices).then(function() {
debug("Swapped bookmarks for indices " + indices);
}, onError);
// Toggle hidden flag no matter when the promises are fulfilled: (Prior to that, the promises changed it
// and the next loop iteration reacted but should not!)
if (isHidden)
{
unhide();
}
else
{
hide();
}
}
function swap_bookmarks(bookmark, ghostBookmark)
{
//if (!isHidden)
//{
//// Hide original data / swap bookmark data:
// or browser.bookmarks.remove(currentBookmark.id);
//}
//else // currently hidden => show
//{
//// Recreate bookmark data according to original state:
//var creating = browser.bookmarks.create({title: currentTab.title, url: currentTab.url});
//creating.then(function(bookmark) {
//currentBookmark = bookmark;
//});
//debug("isHidden: " + isHidden + " was: true => did reset bookmark: " + uneval(bookmark));
//debug("isHidden: " + isHidden + " was: true => did reset ghost bookmark: " + uneval(ghostBookmark));
//}
// Swap the urls because else urls may get lost.
debug("b: isHidden: " + isHidden + " toggle from " + uneval(bookmark));
var updating = browser.bookmarks.update(bookmark.id, {title: ghostBookmark.title, url: ghostBookmark.url});
updating.then(function(b) {
debug("[b] isHidden: " + isHidden + " toggle from (see approx. b) " + uneval(bookmark) + " to " + uneval(b));
});
debug("gB: isHidden: " + isHidden + " toggle from " + uneval(bookmark));
var updating = browser.bookmarks.update(ghostBookmark.id, {title: bookmark.title, url: bookmark.url});
updating.then(function(b) {
debug("[gB] isHidden: " + isHidden + " toggle from (see approx. gB) " + uneval(ghostBookmark) + " to " + uneval(b));
});
}
browser.browserAction.onClicked.addListener(init);
function loadGhostifyBookmarks(arr)
{
// serial asynchronous call, see stackoverflow.com/questions/24660096/correct-way-to-write-loops-for-promise#answer-24985483
return arr.reduce(function(promise, arr_entry) {
return promise.then(function() {
return updateGhostifyBookmark(arr_entry);
});
}, Promise.resolve());
}
function updateGhostifyBookmarks()
{
// Without this line, it leads to TypeError: ghostifyBookmarks[index] = bookmarks[0]; in updateGhostifyBookmark:
ghostifyBookmarks = [];
var indices = [];
for (var i = 0; i < urlsToGhostify.length; ++i)
indices[i] = i;
return loadGhostifyBookmarks(indices).then(function() {
debug("Loaded bookmarks for " + urlsToGhostify);
}, onError);
}
function updateGhostifyBookmark(index)
{
var searching = browser.bookmarks.search({url: urlsToGhostify[index]});
return searching.then((bookmarks) => {
debug(index + ": updateGhostifyBookmarks promise fulfilled: " + uneval(bookmarks));
if (bookmarks.length < 1)
return;
ghostifyBookmarks[index] = bookmarks[0];
}, onError);
}
function updateGhostBookmarks()
{
ghostBookmarks = [];
// ghosts:
for (var i = 0; i < urlGhosts.length; ++i)
{
var searching = browser.bookmarks.search({url: urlGhosts[i]});
searching.then((bookmarks) => {
if (bookmarks.length < 1)
return;
debug(i + ": <- problem!! correct: " + ghostBookmarks.length + ": updateGhostBookmarks promise fulfilled: " + uneval(bookmarks));
ghostBookmarks[ghostBookmarks.length] = bookmarks[0];
}, onError);
}
}
function onMessage(request, sender, sendResponse)
{
// ensure valid loaded ghostifyBookmarks in case the options are updated but toggle action is not triggered.
// FIXME This message never arrives because the promise is never fulfilled. Just like with the bookmark promises.
if (request.message == "urls_stored")
init();
sendResponse({response: "Called init function. Thanks for the information."});
}
// TODO listen for bookmarks.onCreated and bookmarks.onRemoved once Bug 1221764 lands