-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
background.js
537 lines (471 loc) · 18 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//初回起動時にオプションページを表示して設定を初期化
browser.runtime.onInstalled.addListener(function () {
browser.tabs.create({
url: "options/options.html#information",
active: false
});
});
let S = new settingsObj()
var sessions = [];
var sessionStartTime = Date.now();
//起動時の設定
initSettings().then(function () {
updateAutoName();
setStorage();
setAutoSave();
autoSaveWhenClose()
.then(openLastSession);
browser.storage.onChanged.addListener(setAutoSave);
browser.tabs.onActivated.addListener(replacePage);
browser.windows.onFocusChanged.addListener(replacePage);
//ウィンドウを閉じたときに保存
browser.tabs.onUpdated.addListener(onUpdate);
browser.tabs.onCreated.addListener(autoSaveWhenClose);
browser.tabs.onRemoved.addListener(autoSaveWhenClose);
browser.windows.onCreated.addListener(autoSaveWhenClose);
});
function returnReplaceParamater(url) {
let paramater = {};
if (url.indexOf(browser.runtime.getURL("replaced/replaced.html")) === 0) {
paramater.isReplaced = true;
let paras = url.split('?')[1].split('&');
for (let p of paras) {
paramater[p.split('=')[0]] = decodeURIComponent(p.split('=')[1]);
}
} else {
paramater.isReplaced = false;
}
return paramater;
}
function replacePage() {
if (!IsOpeningSession) {
browser.tabs.query({
active: true,
currentWindow: true
}).then(function (info) {
if (info[0].status != "complete") {
setTimeout(replacePage, 500);
}
let paramater = returnReplaceParamater(info[0].url);
if (paramater.isReplaced && paramater.state == "redirect") {
browser.tabs.update({
url: paramater.url
}).catch(function () {
//失敗時
browser.tabs.update({
url: "replaced/replaced.html" +
"?state=open_faild" +
"&title=" + encodeURIComponent(paramater.title) +
"&url=" + encodeURIComponent(paramater.url) +
"&favIconUrl=" + encodeURIComponent(paramater.favIconUrl)
})
})
}
})
}
}
//設定の初期化
function initSettings(value) {
return new Promise(function (resolve, reject) {
browser.storage.local.get(["sessions"], function (value) {
//sessions初期化
if (value.sessions != undefined) sessions = value.sessions;
else sessions = [];
S.init()
.then(() => {
resolve();
});
});
})
}
//過去のバージョンのautosaveのセッション名を変更
function updateAutoName() {
for (let i in sessions) {
if (sessions[i].tag.indexOf("auto winClose") != -1) {
if (sessions[i].name === "Auto Saved - Window was closed")
sessions[i].name = browser.i18n.getMessage("winCloseSessionName");
} else if (sessions[i].tag.indexOf("auto regular") != -1) {
if (sessions[i].name === "Auto Saved - Regularly")
sessions[i].name = browser.i18n.getMessage("regularSaveSessionName");
}
}
}
//セッションを読み出す
function loadSessions() {
browser.storage.local.get(["sessions"], function (value) {
sessions = value.sessions;
});
}
//セッションを保存
function setStorage() {
browser.storage.local.set({
'sessions': sessions
});
}
let BeforeSettings = {};
//ifAutoSaveとautoSaveIntervalに変更があったらtrue
function isChangeAutoSaveSettings() {
return new Promise(function (resolve, reject) {
browser.storage.local.get(["Settings"], function (value) {
if (JSON.stringify(BeforeSettings) != JSON.stringify(value.Settings)) {
if (BeforeSettings.ifAutoSave != value.Settings.ifAutoSave || BeforeSettings.autoSaveInterval != value.Settings.autoSaveInterval) {
resolve(true);
}
}
BeforeSettings = value.Settings;
resolve(false);
});
})
}
let autoSaveTimer;
function startAutoSave() {
autoSaveTimer = setInterval(function () {
let name = browser.i18n.getMessage("regularSaveSessionName");
let tag = "auto regular";
saveSession(name, tag).then(() => {
removeOverLimit("regular");
});
}, S.get().autoSaveInterval * 60 * 1000);
}
function stopAutoSave() {
clearInterval(autoSaveTimer);
}
async function setAutoSave() {
if (await isChangeAutoSaveSettings()) {
stopAutoSave();
if (S.get().ifAutoSave) {
startAutoSave();
}
}
}
function onUpdate(tabId, changeInfo, tab) {
if (changeInfo.status == "complete") {
autoSaveWhenClose();
}
}
function autoSaveWhenClose() {
return new Promise((resolve, reject) => {
if (!IsOpeningSession && !IsSavingSession && (S.get().ifAutoSaveWhenClose || S.get().ifOpenLastSessionWhenStartUp)) {
saveSession(browser.i18n.getMessage("winCloseSessionName"), "auto winClose temp").then(function () {
removeOverLimit("winClose");
resolve();
});
}
})
};
function openLastSession() {
if (S.get().ifOpenLastSessionWhenStartUp) {
const winCloseSessions = (sessions.filter((element, index, array) => {
return (element.tag.includes("winClose") && !element.tag.includes("temp"));
}));
openSession(winCloseSessions[winCloseSessions.length - 1], true);
}
}
function removeOverLimit(tagState) {
let limit;
if (tagState == "regular") limit = S.get().autoSaveLimit;
else if (tagState == "winClose") limit = parseInt(S.get().autoSaveWhenCloseLimit) + 1; //temp分
//定期保存を列挙
let autoSavedArray = [];
for (let i in sessions) {
if (sessions[i].tag.indexOf(tagState) != -1) {
autoSavedArray.push(i);
}
}
//上限を超えている場合は削除
if (autoSavedArray.length > limit) {
let removeNum = autoSavedArray.length - limit;
let removeSessions = autoSavedArray.slice(0, removeNum);
for (let i of removeSessions) {
removeSession(i);
}
}
}
IsSavingSession = false;
function saveSession(name, tag) {
IsSavingSession = true;
return new Promise(function (resolve, reject) {
loadCurrentSesssion(name, tag).then(function (session) {
if (tag.indexOf("winClose") != -1) {
showSessionWhenWindowClose(session);
sessions.push(session);
} else if (tag.indexOf("regular") != -1) {
if (ifChangedAutoSaveSession(session)) sessions.push(session);
} else {
sessions.push(session);
}
setStorage();
IsSavingSession = false;
resolve();
})
})
}
function loadCurrentSesssion(name, tag) {
return new Promise(function (resolve, reject) {
let session = {};
browser.tabs.query({}).then(function (tabs) {
session.windows = {};
session.tabsNumber = 0;
session.name = name;
session.date = new Date();
session.tag = tag;
session.sessionStartTime = sessionStartTime;
//windouwsとtabのセット
for (let tab of tabs) {
//プライベートタブを無視
if (!S.get().ifSavePrivateWindow) {
if (tab.incognito) continue;
}
if (session.windows[tab.windowId] == undefined) session.windows[tab.windowId] = {};
//replacedPageなら元のページを保存
let paramater = returnReplaceParamater(tab.url)
if (paramater.isReplaced) {
tab.url = paramater.url;
}
session.windows[tab.windowId][tab.id] = tab;
session.tabsNumber++;
}
if (tabs.length > 0) resolve(session);
else reject();
})
})
}
//前回の自動保存からタブが変わっているか判定
//自動保存する必要があればtrue
function ifChangedAutoSaveSession(session) {
let lastAutoNumber = -1;
for (let i in sessions) {
if (sessions[i].tag.indexOf("regular") != -1) lastAutoNumber = i;
}
//自動保存が無ければtrue
if (lastAutoNumber == -1) return true;
//前回保存時のセッション
let lastItems = [];
for (let win in sessions[lastAutoNumber].windows) {
lastItems.push(win);
for (let tab in sessions[lastAutoNumber].windows[win]) {
id = sessions[lastAutoNumber].windows[win][tab].id;
url = sessions[lastAutoNumber].windows[win][tab].url;
lastItems.push(id, url);
}
}
//現在のセッション
let newItems = []
for (let win in session.windows) {
newItems.push(win);
for (let tab in session.windows[win]) {
id = session.windows[win][tab].id;
url = session.windows[win][tab].url;
newItems.push(id, url);
}
}
//前回保存時とタブが異なればtrue
return lastItems.toString() != newItems.toString();
}
//ウィンドウを閉じたときの自動保存が有効になっている時,セッションは常に非表示の状態で一時保存される
//一時保存されたセッションを現在のセッションと比較してウィンドウの削除かFirefoxの再起動を確認したら表示する
function showSessionWhenWindowClose(session) {
//sessionsを新しいものから走査
for (let i = sessions.length - 1; i >= 0; i--) {
if (sessions[i].tag.indexOf('temp') != -1) {
let showFlag = false;
let currentSession = Object.keys(session.windows);
let oldSession = Object.keys(sessions[i].windows);
//oldSessionに現在存在しないウィンドウがあれば(保存が必要なら)showFlag=true
for (let os of oldSession) {
for (let cs of currentSession) {
if (os == cs) break;
if (cs == currentSession[currentSession.length - 1]) showFlag = true;
}
}
//sessionStartTimeが異なればFirefoxの再起動されたと見なしshowFlag=true
if (sessions[i].sessionStartTime != session.sessionStartTime) showFlag = true;
//保存が必要ならクラスからtempを削除し表示する
if (showFlag) {
sessions[i].tag = "auto winClose";
break;
}
//不要ならtempの項目を更新
else {
sessions.splice(i, 1);
}
}
}
}
function openSession(session, openCurrentWindow = false) {
let countFlag = 0;
let p = Promise.resolve();
tabList = {};
for (let win in session.windows) { //ウィンドウごと
//console.log(session.windows[win]);
const firstTab = session.windows[win][Object.keys(session.windows[win])[0]];
let createData = {
incognito: firstTab.incognito
};
p = p.then(function () {
if (countFlag == 0 && (!S.get().ifOpenNewWindow || openCurrentWindow)) { //一つ目のウィンドウは現在のウィンドウに上書き
countFlag = 1;
return removeNowOpenTabs().then(function (currentWindow) {
return createTabs(session, win, currentWindow);
});
} else {
return browser.windows.create(createData).then(function (currentWindow) {
return createTabs(session, win, currentWindow);
});
}
})
}
}
let IsOpeningSession = false;
//ウィンドウとタブを閉じてcurrentWindowを返す
function removeNowOpenTabs() {
return new Promise(function (resolve, reject) {
browser.windows.getAll({}).then(function (windows) {
for (let win in windows) {
if (windows[win].focused == false) { //非アクティブのウィンドウを閉じる
browser.windows.remove(windows[win].id);
} else {
browser.tabs.query({
currentWindow: true
}).then(function (tabs) {
for (let tab of tabs) {
if (tab.index != 0) browser.tabs.remove(tab.id); //アクティブウィンドウのタブを閉じる
}
})
}
}
browser.windows.getAll({
populate: true
}).then(function (currentWindow) {
resolve(currentWindow[0]);
});
});
})
}
//現在のウィンドウにタブを生成
function createTabs(session, win, currentWindow) {
return new Promise(function (resolve, reject) {
IsOpeningSession = true;
let sortedTabs = [];
for (let tab in session.windows[win]) {
sortedTabs[session.windows[win][tab].index] = session.windows[win][tab];
}
//console.log(sortedTabs);
let firstTabId = currentWindow.tabs[0].id;
let tabNumber = 0;
let p = Promise.resolve();
for (let tab of sortedTabs) { //タブごと
p = p.then(function () {
tabNumber++;
return openTab(session, win, currentWindow, tab.id);
}).then(function () {
if (tabNumber == 1) {
browser.tabs.remove(firstTabId);
}
if (tabNumber == Object.keys(session.windows[win]).length) {
IsOpeningSession = false;
replacePage();
resolve();
}
});
}
})
}
tabList = {};
//実際にタブを開く
function openTab(session, win, currentWindow, tab) {
//console.log("open", session.windows[win][tab]);
return new Promise(function (resolve, reject) {
let property = session.windows[win][tab];
let createOption = {
active: property.active,
cookieStoreId: property.cookieStoreId,
index: property.index,
pinned: property.pinned,
url: property.url,
windowId: currentWindow.id
}
if (property.cookieStoreId == "firefox-private") delete property.cookieStoreId;
//supported FF57++
if (S.get().ifSupportTst) {
createOption.openerTabId = tabList[property.openerTabId];
openDelay = 150;
} else {
openDelay = 0;
}
//Lazy loading
if (S.get().ifLazyLoading) {
createOption.url = "replaced/replaced.html" +
"?state=redirect" +
"&title=" + encodeURIComponent(property.title) +
"&url=" + encodeURIComponent(property.url) +
"&favIconUrl=" + encodeURIComponent(property.favIconUrl);
}
setTimeout(function () {
browser.tabs.create(createOption).then(function (newTab) {
tabList[property.id] = newTab.id;
resolve();
}, function () { //タブオープン失敗時
createOption.url = "replaced/replaced.html" +
"?state=open_faild" +
"&title=" + encodeURIComponent(property.title) +
"&url=" + encodeURIComponent(property.url) +
"&favIconUrl=" + encodeURIComponent(property.favIconUrl)
browser.tabs.create(createOption).then(function (newTab) {
tabList[property.id] = newTab.id;
resolve();
}, function () { //失敗時(多分起こらないけど念のため)
resolve();
})
});
}, openDelay) //ツリー型タブの処理を待つ
})
}
//indexに従ってタブを移動
function moveTabsInIndex(currentWindow) {
return new Promise(function (resolve, reject) {
browser.tabs.query({
windowId: currentWindow.id
}).then(function (openTabs) {
for (let tab in openTabs) {
browser.tabs.move(openTabs[tab].id, {
index: openTabs[tab].index
});
}
}).then(function () {
resolve();
});
})
}
function removeSession(number) {
sessions.splice(number, 1);
setStorage();
}
function renameSession(sessionNo, name) {
sessions[sessionNo].name = name;
setStorage();
}
//popupからのリクエスト
browser.runtime.onMessage.addListener(function (request) {
switch (request.message) {
case "save":
name = request.name;
saveSession(name, "user");
break;
case "open":
openSession(sessions[request.number]);
break;
case "remove":
removeSession(request.number);
break;
case "rename":
renameSession(request.sessionNo, request.name);
break;
case "import":
loadSessions();
break;
}
});