2017-11-12 22:00:40 +01:00
|
|
|
|
// Copyright (c) 2017 kroppy. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) license
|
|
|
|
|
// that can be found at https://creativecommons.org/licenses/by-nc-nd/4.0/
|
|
|
|
|
|
2018-07-03 20:36:38 +02:00
|
|
|
|
|
2018-05-22 02:11:29 +02:00
|
|
|
|
function ExportGroup(groupId, filename, save_to_manager) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
let GroupToSave = { group: tt.groups[groupId], folders: {}, tabs: [] };
|
2018-05-22 02:11:29 +02:00
|
|
|
|
document.querySelectorAll("#" + groupId + " .folder").forEach(function(s) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
if (tt.folders[s.id]) {
|
|
|
|
|
GroupToSave.folders[s.id] = tt.folders[s.id];
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-03-13 14:39:34 +01:00
|
|
|
|
});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
let Tabs = document.querySelectorAll("#" + groupId + " .tab");
|
2018-03-13 14:39:34 +01:00
|
|
|
|
if (Tabs.length > 0) {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
let lastId = parseInt(Tabs[Tabs.length - 1].id);
|
|
|
|
|
Tabs.forEach(function(s) {
|
2018-03-13 14:39:34 +01:00
|
|
|
|
chrome.tabs.get(parseInt(s.id), function(tab) {
|
|
|
|
|
if ((tab.url).startsWith("www") || (tab.url).startsWith("http") || (tab.url).startsWith("ftp")) {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
(GroupToSave.tabs).push({
|
|
|
|
|
id: tab.id,
|
|
|
|
|
parent: s.parentNode.parentNode.id,
|
|
|
|
|
index: Array.from(s.parentNode.children).indexOf(s),
|
|
|
|
|
expand: (s.classList.contains("c") ? "c" : (s.classList.contains("o") ? "o" : "")),
|
|
|
|
|
url: tab.url
|
|
|
|
|
});
|
2018-03-13 14:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
if (tab.id == lastId) {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (filename) {
|
|
|
|
|
SaveFile(filename, "tt_group", GroupToSave);
|
|
|
|
|
}
|
|
|
|
|
if (save_to_manager) {
|
|
|
|
|
AddGroupToStorage(GroupToSave, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ExportGroup, filename: "+filename+", groupId: "+groupId+", save_to_manager: "+save_to_manager);
|
|
|
|
|
}
|
2018-03-13 14:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-12 22:00:40 +01:00
|
|
|
|
});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
} else {
|
|
|
|
|
if (filename) {
|
|
|
|
|
SaveFile(filename, "tt_group", GroupToSave);
|
|
|
|
|
}
|
|
|
|
|
if (save_to_manager) {
|
|
|
|
|
AddGroupToStorage(GroupToSave, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ExportGroup, filename: "+filename+", groupId: "+groupId+", save_to_manager: "+save_to_manager);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 14:39:34 +01:00
|
|
|
|
}
|
2017-11-12 22:00:40 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
|
|
|
|
function ImportGroup(recreate_group, save_to_manager) {
|
|
|
|
|
let file = document.getElementById("file_import");
|
2017-11-12 22:00:40 +01:00
|
|
|
|
let fr = new FileReader();
|
|
|
|
|
if (file.files[0] == undefined) return;
|
|
|
|
|
fr.readAsText(file.files[0]);
|
|
|
|
|
fr.onload = function() {
|
|
|
|
|
let data = fr.result;
|
2018-05-22 02:11:29 +02:00
|
|
|
|
let group = JSON.parse(data);
|
2018-03-13 14:39:34 +01:00
|
|
|
|
file.parentNode.removeChild(file);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (recreate_group) {
|
|
|
|
|
RecreateGroup(group);
|
|
|
|
|
}
|
|
|
|
|
if (save_to_manager) {
|
|
|
|
|
AddGroupToStorage(group, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ImportGroup, recreate_group: "+recreate_group+", save_to_manager: "+save_to_manager);
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AddGroupToStorage(group, add_to_manager) {
|
|
|
|
|
chrome.storage.local.get(null, function(storage) {
|
|
|
|
|
if (storage["hibernated_groups"] == undefined) {
|
|
|
|
|
let hibernated_groups = [];
|
|
|
|
|
hibernated_groups.push(group);
|
|
|
|
|
chrome.storage.local.set({ hibernated_groups: hibernated_groups });
|
|
|
|
|
if (add_to_manager) {
|
|
|
|
|
AddGroupToManagerList(group);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let hibernated_groups = storage["hibernated_groups"];
|
|
|
|
|
hibernated_groups.push(group);
|
|
|
|
|
chrome.storage.local.set({ hibernated_groups: hibernated_groups });
|
|
|
|
|
if (add_to_manager) {
|
|
|
|
|
AddGroupToManagerList(group);
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: AddGroupToStorage, add_to_manager: "+add_to_manager);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function RecreateGroup(LoadedGroup) {
|
|
|
|
|
let NewFolders = {};
|
|
|
|
|
let RefsTabs = {};
|
|
|
|
|
let NewTabs = [];
|
|
|
|
|
let NewGroupId = AddNewGroup(LoadedGroup.group.name, LoadedGroup.group.font);
|
|
|
|
|
SetActiveGroup(NewGroupId, false, false);
|
|
|
|
|
for (var folder in LoadedGroup.folders) {
|
|
|
|
|
let newId = GenerateNewFolderID();
|
|
|
|
|
NewFolders[folder] = { id: newId, parent: NewGroupId, index: (LoadedGroup.folders[folder].index), name: (LoadedGroup.folders[folder].name), expand: (LoadedGroup.folders[folder].expand) };
|
|
|
|
|
}
|
|
|
|
|
for (var folder in NewFolders) {
|
|
|
|
|
if (NewFolders[LoadedGroup.folders[folder].parent]) {
|
|
|
|
|
NewFolders[folder].parent = NewFolders[LoadedGroup.folders[folder].parent].id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(LoadedGroup.tabs).forEach(function(Tab) {
|
|
|
|
|
chrome.tabs.create({ url: Tab.url, active: false }, function(new_tab) {
|
|
|
|
|
if (new_tab) {
|
|
|
|
|
RefsTabs[Tab.id] = new_tab.id;
|
|
|
|
|
Tab.id = new_tab.id;
|
|
|
|
|
NewTabs.push(Tab);
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
let nt = document.getElementById(new_tab.id);
|
|
|
|
|
let NewGroupTabs = document.getElementById("ct" + NewGroupId);
|
|
|
|
|
if (nt != null && NewGroupTabs != null) {
|
|
|
|
|
NewGroupTabs.appendChild(nt);
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
2018-07-03 20:36:38 +02:00
|
|
|
|
|
|
|
|
|
if (browserId != "O") {
|
|
|
|
|
chrome.runtime.sendMessage({command: "discard_tab", tabId: new_tab.id});
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
if (NewTabs.length == LoadedGroup.tabs.length - 1) {
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
NewTabs.forEach(function(LTab) {
|
|
|
|
|
if (LTab.parent == LoadedGroup.group.id) {
|
|
|
|
|
LTab.parent = NewGroupId;
|
|
|
|
|
}
|
|
|
|
|
if (NewFolders[LTab.parent]) {
|
|
|
|
|
LTab.parent = NewFolders[LTab.parent].id;
|
|
|
|
|
}
|
|
|
|
|
if (RefsTabs[LTab.parent]) {
|
|
|
|
|
LTab.parent = RefsTabs[LTab.parent];
|
2018-03-13 14:39:34 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
});
|
|
|
|
|
setTimeout(function() {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
RcreateTreeStructure({}, NewFolders, NewTabs);
|
2018-03-13 14:39:34 +01:00
|
|
|
|
}, 1000);
|
2017-11-12 22:00:40 +01:00
|
|
|
|
setTimeout(function() {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
RcreateTreeStructure({}, NewFolders, NewTabs);
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}, 2000);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
setTimeout(function() {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
RcreateTreeStructure({}, NewFolders, NewTabs);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}, 5000);
|
|
|
|
|
}, 2000);
|
|
|
|
|
}
|
2017-11-12 22:00:40 +01:00
|
|
|
|
});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: RecreateGroup");
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 22:00:40 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function ExportSession(name, save_to_file, save_to_manager, save_to_autosave_manager) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
chrome.windows.getAll({ windowTypes: ['normal'], populate: true }, function(win) {
|
|
|
|
|
chrome.runtime.sendMessage({ command: "get_browser_tabs" }, function(t) {
|
|
|
|
|
let tabs = Object.assign({}, t);
|
|
|
|
|
chrome.runtime.sendMessage({ command: "get_windows" }, function(w) {
|
|
|
|
|
let windows = Object.assign({}, w);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
let warn = true;
|
|
|
|
|
let ExportWindows = [];
|
2018-07-03 20:36:38 +02:00
|
|
|
|
win.forEach(function(CWin) {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (CWin.tabs.length > 0) {
|
|
|
|
|
windows[CWin.id]["id"] = CWin.id;
|
|
|
|
|
windows[CWin.id]["tabs"] = [];
|
|
|
|
|
CWin.tabs.forEach(function(CTab) {
|
|
|
|
|
if ((CTab.url).startsWith("www") || (CTab.url).startsWith("http") || (CTab.url).startsWith("ftp")) {
|
|
|
|
|
windows[CWin.id]["tabs"].push({ id: CTab.id, url: CTab.url, parent: tabs[CTab.id].parent, index: tabs[CTab.id].index, expand: tabs[CTab.id].expand });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ExportWindows.push(windows[CWin.id]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (save_to_file) {
|
|
|
|
|
SaveFile(name, "tt_session", ExportWindows);
|
|
|
|
|
}
|
|
|
|
|
if (save_to_manager) {
|
|
|
|
|
AddSessionToStorage(ExportWindows, name, true);
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (save_to_autosave_manager) {
|
|
|
|
|
AddAutosaveSessionToStorage(ExportWindows, name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ExportSession, name: "+name+", save_to_file: "+save_to_file+", save_to_manager: "+save_to_manager+", save_to_autosave_manager: "+save_to_autosave_manager);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 22:00:40 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
|
|
|
|
function ImportSession(recreate_session, save_to_manager, merge_session) {
|
|
|
|
|
let file = document.getElementById("file_import");
|
2017-11-12 22:00:40 +01:00
|
|
|
|
let fr = new FileReader();
|
|
|
|
|
if (file.files[0] == undefined) return;
|
2018-05-22 02:11:29 +02:00
|
|
|
|
fr.readAsText(file.files[file.files.length - 1]);
|
|
|
|
|
|
2017-11-12 22:00:40 +01:00
|
|
|
|
fr.onload = function() {
|
|
|
|
|
let data = fr.result;
|
2018-03-13 14:39:34 +01:00
|
|
|
|
file.parentNode.removeChild(file);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
|
|
|
|
let LoadedSession = JSON.parse(data);
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ImportSession, recreate_session: "+recreate_session+", merge_session: "+merge_session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (recreate_session) {
|
|
|
|
|
RecreateSession(LoadedSession);
|
|
|
|
|
}
|
|
|
|
|
if (merge_session) {
|
|
|
|
|
ImportMergeTabs(LoadedSession);
|
|
|
|
|
}
|
|
|
|
|
if (save_to_manager) {
|
|
|
|
|
AddSessionToStorage(LoadedSession, (file.files[file.files.length - 1].name).replace(".tt_session", ""), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AddSessionToStorage(session, name, add_to_manager) {
|
|
|
|
|
chrome.storage.local.get(null, function(storage) {
|
|
|
|
|
if (storage.saved_sessions == undefined) {
|
|
|
|
|
let saved_sessions = [];
|
|
|
|
|
saved_sessions.push({ name: name, session: session });
|
|
|
|
|
chrome.storage.local.set({ saved_sessions: saved_sessions });
|
|
|
|
|
if (add_to_manager) {
|
|
|
|
|
AddSessionToManagerList(saved_sessions[saved_sessions.length - 1]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let saved_sessions = storage.saved_sessions;
|
|
|
|
|
saved_sessions.push({ name: name, session: session });
|
|
|
|
|
|
|
|
|
|
chrome.storage.local.set({ saved_sessions: saved_sessions });
|
|
|
|
|
if (add_to_manager) {
|
|
|
|
|
AddSessionToManagerList(saved_sessions[saved_sessions.length - 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: AddSessionToStorage, name: "+name+", add_to_manager: "+add_to_manager);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AddAutosaveSessionToStorage(session, name) {
|
|
|
|
|
chrome.storage.local.get(null, function(storage) {
|
|
|
|
|
if (storage.saved_sessions_automatic == undefined) {
|
|
|
|
|
let s = [];
|
|
|
|
|
s.push({ name: name, session: session });
|
|
|
|
|
chrome.storage.local.set({ saved_sessions_automatic: s });
|
|
|
|
|
} else {
|
|
|
|
|
let s = storage.saved_sessions_automatic;
|
|
|
|
|
s.unshift({ name: name, session: session });
|
|
|
|
|
if (s[opt.autosave_max_to_keep]) {
|
|
|
|
|
s.splice(opt.autosave_max_to_keep, (s.length - opt.autosave_max_to_keep));
|
|
|
|
|
}
|
|
|
|
|
chrome.storage.local.set({ saved_sessions_automatic: s });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: AddAutosaveSessionToStorage, name: "+name);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function RecreateSession(LoadedSession) {
|
|
|
|
|
let RefsTabs = {};
|
|
|
|
|
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: RecreateSession");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadedSession.forEach(function(LWin) {
|
|
|
|
|
let NewTabs = [];
|
|
|
|
|
let urls = [];
|
|
|
|
|
(LWin.tabs).forEach(function(Tab) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
urls.push(Tab.url);
|
|
|
|
|
NewTabs.push(Tab);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
});
|
2018-07-03 20:36:38 +02:00
|
|
|
|
|
|
|
|
|
chrome.windows.create({ url: urls /* , discarded: true */ }, function(new_window) {
|
|
|
|
|
chrome.runtime.sendMessage({command: "save_groups", windowId: new_window.id, groups: LWin.groups});
|
|
|
|
|
chrome.runtime.sendMessage({command: "save_folders", windowId: new_window.id, folders: LWin.folders});
|
|
|
|
|
|
|
|
|
|
for (let tInd = 0; tInd < new_window.tabs.length; tInd++) {
|
|
|
|
|
RefsTabs[NewTabs[tInd].id] = new_window.tabs[tInd].id;
|
|
|
|
|
NewTabs[tInd].id = new_window.tabs[tInd].id;
|
|
|
|
|
}
|
|
|
|
|
for (let tInd = 0; tInd < new_window.tabs.length; tInd++) {
|
|
|
|
|
if (RefsTabs[NewTabs[tInd].parent] != undefined) {
|
|
|
|
|
NewTabs[tInd].parent = RefsTabs[NewTabs[tInd].parent];
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
}
|
|
|
|
|
for (let tInd = 0; tInd < new_window.tabs.length; tInd++) {
|
|
|
|
|
if (NewTabs[tInd].parent == "pin_list") {
|
|
|
|
|
chrome.tabs.update(new_window.tabs[tInd].id, { pinned: true });
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
chrome.runtime.sendMessage({command: "update_tab", tabId: new_window.tabs[tInd].id, tab: {index: NewTabs[tInd].index, expand: NewTabs[tInd].expand, parent: NewTabs[tInd].parent}});
|
|
|
|
|
|
|
|
|
|
// if (browserId != "O") {
|
|
|
|
|
// chrome.runtime.sendMessage({command: "discard_tab", tabId: new_window.tabs[tInd].id});
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (browserId != "O") {
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
chrome.runtime.sendMessage({command: "discard_window", windowId: new_window.id});
|
|
|
|
|
}, urls.length * 300);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 22:00:40 +01:00
|
|
|
|
});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
});
|
2017-11-12 22:00:40 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
2018-07-03 20:36:38 +02:00
|
|
|
|
// groups and folders are in object, just like tt.groups and tt.folders, but tabs are in array of treetabs objects
|
|
|
|
|
function RcreateTreeStructure(groups, folders, tabs) {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (opt.debug) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
log("f: RcreateTreeStructure");
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 20:36:38 +02:00
|
|
|
|
ShowStatusBar({show: true, spinner: true, message: "Quick check and recreating structure..."});
|
|
|
|
|
if (groups && Object.keys(groups).length > 0) {
|
|
|
|
|
for (var group in groups) {
|
|
|
|
|
tt.groups[groups[group].id] = Object.assign({}, groups[group]);
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
AppendGroups(tt.groups);
|
|
|
|
|
}
|
|
|
|
|
if (folders && Object.keys(folders).length > 0) {
|
|
|
|
|
for (var folder in folders) {
|
|
|
|
|
tt.folders[folders[folder].id] = Object.assign({}, folders[folder]);
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
AppendFolders(tt.folders);
|
|
|
|
|
}
|
|
|
|
|
let bgtabs = {};
|
|
|
|
|
tabs.forEach(function(Tab) {
|
|
|
|
|
if (Tab.parent == "pin_list") {
|
|
|
|
|
chrome.tabs.update(Tab.id, { pinned: true });
|
|
|
|
|
}
|
|
|
|
|
if (Tab.parent != "") {
|
2018-03-13 14:39:34 +01:00
|
|
|
|
let tb = document.getElementById(Tab.id);
|
2018-05-22 02:11:29 +02:00
|
|
|
|
let tbp = document.getElementById("ct" + Tab.parent);
|
2018-07-03 20:36:38 +02:00
|
|
|
|
if (tb && tbp) {
|
2018-03-13 14:39:34 +01:00
|
|
|
|
tbp.appendChild(tb);
|
|
|
|
|
if (Tab.expand != "") {
|
|
|
|
|
tb.classList.add(Tab.expand);
|
|
|
|
|
}
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
bgtabs[Tab.id] = { index: Tab.index, parent: Tab.parent, expand: Tab.expand };
|
2018-07-03 20:36:38 +02:00
|
|
|
|
}
|
2018-01-01 19:50:56 +01:00
|
|
|
|
});
|
2018-07-03 20:36:38 +02:00
|
|
|
|
RearrangeTreeTabs(bgtabs, false);
|
|
|
|
|
RearrangeFolders(true);
|
|
|
|
|
UpdateBgGroupsOrder();
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
RefreshExpandStates();
|
|
|
|
|
RefreshCounters();
|
|
|
|
|
tt.schedule_update_data++;
|
|
|
|
|
SaveFolders();
|
|
|
|
|
}, 3000);
|
|
|
|
|
// ShowStatusBar({show: true, spinner: true, message: "Sorting"});
|
|
|
|
|
// ShowStatusBar(false, "Wait just a little more...");
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
|
|
|
|
function ImportMergeTabs(LoadedSession) {
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ImportMergeTabs");
|
|
|
|
|
}
|
|
|
|
|
let RefsWins = {};
|
|
|
|
|
let RefsTabs = {};
|
|
|
|
|
for (let LWI = 0; LWI < LoadedSession.length; LWI++) { // clear previous window ids
|
|
|
|
|
LoadedSession[LWI].id = "";
|
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
ShowStatusBar({show: true, spinner: true, message: "Loaded Tree structure..."});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
chrome.windows.getAll({ windowTypes: ['normal'], populate: true }, function(cw) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
for (let CWI = 0; CWI < cw.length; CWI++) { // Current Windows
|
|
|
|
|
|
|
|
|
|
for (let LWI = 0; LWI < LoadedSession.length; LWI++) { // Loaded Windows
|
|
|
|
|
|
|
|
|
|
if (LoadedSession[LWI].id == "") {
|
|
|
|
|
|
|
|
|
|
let tabsMatch = 0;
|
|
|
|
|
for (let CTI = 0; CTI < cw[CWI].tabs.length; CTI++) { // loop Tabs of CWI Window
|
|
|
|
|
for (let LTI = 0; LTI < LoadedSession[LWI].tabs.length; LTI++) { // loop Tabs of Loaded Window
|
|
|
|
|
if (cw[CWI].tabs[CTI].url == LoadedSession[LWI].tabs[LTI].url) {
|
|
|
|
|
RefsTabs[LoadedSession[LWI].tabs[LTI].id] = cw[CWI].tabs[CTI].id;
|
|
|
|
|
LoadedSession[LWI].tabs[LTI].id = cw[CWI].tabs[CTI].id;
|
|
|
|
|
LoadedSession[LWI].tabs[LTI].url = "";
|
|
|
|
|
tabsMatch++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-11-12 22:00:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ImportMergeTabs, tabsMatch: "+tabsMatch);
|
|
|
|
|
}
|
|
|
|
|
if (tabsMatch > LoadedSession[LWI].tabs.length * 0.6) {
|
|
|
|
|
LoadedSession[LWI].id = cw[CWI].id;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadedSession.forEach(function(w) {
|
|
|
|
|
if (w.id == "") { // missing window, lets make one
|
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: ImportMergeTabs, missing window");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let NewTabs = [];
|
|
|
|
|
let urls = [];
|
|
|
|
|
(w.tabs).forEach(function(Tab) {
|
|
|
|
|
urls.push(Tab.url);
|
|
|
|
|
NewTabs.push(Tab);
|
|
|
|
|
});
|
|
|
|
|
chrome.windows.create({ url: urls }, function(new_window) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
chrome.runtime.sendMessage({command: "save_groups", windowId: new_window.id, groups: w.groups});
|
|
|
|
|
chrome.runtime.sendMessage({command: "save_folders", windowId: new_window.id, folders: w.folders});
|
|
|
|
|
|
2018-05-22 02:11:29 +02:00
|
|
|
|
for (let tInd = 0; tInd < new_window.tabs.length; tInd++) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
if (NewTabs[tInd]) {
|
|
|
|
|
RefsTabs[NewTabs[tInd].id] = new_window.tabs[tInd].id;
|
|
|
|
|
NewTabs[tInd].id = new_window.tabs[tInd].id;
|
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
for (let tInd = 0; tInd < NewTabs.length; tInd++) {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (RefsTabs[NewTabs[tInd].parent] != undefined) {
|
|
|
|
|
NewTabs[tInd].parent = RefsTabs[NewTabs[tInd].parent];
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
|
|
|
|
|
for (let tInd = 0; tInd < new_window.tabs.length; tInd++) {
|
|
|
|
|
if (NewTabs[tInd].parent == "pin_list") {
|
|
|
|
|
chrome.tabs.update(new_window.tabs[tInd].id, { pinned: true });
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
chrome.runtime.sendMessage({command: "update_tab", tabId: new_window.tabs[tInd].id, tab: {index: NewTabs[tInd].index, expand: NewTabs[tInd].expand, parent: NewTabs[tInd].parent}});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let done = 3;
|
|
|
|
|
var Append = setInterval(function() {
|
|
|
|
|
chrome.runtime.sendMessage({command: "remote_update", groups: w.groups, folders: w.folders, tabs: NewTabs, windowId: w.id });
|
|
|
|
|
if (done < 0) { clearInterval(Append); }
|
|
|
|
|
done--;
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}, 2000);
|
|
|
|
|
});
|
|
|
|
|
} else { // window exists, lets add missing tabs
|
|
|
|
|
let NewTabs = [];
|
2018-07-03 20:36:38 +02:00
|
|
|
|
let RefsTabs = {};
|
|
|
|
|
|
|
|
|
|
chrome.runtime.sendMessage({command: "get_folders", windowId: w.id}, function(f) {
|
|
|
|
|
chrome.runtime.sendMessage({command: "get_groups", windowId: w.id}, function(g) {
|
|
|
|
|
if (Object.keys(w.groups).length > 0) {
|
|
|
|
|
for (var group in w.groups) {
|
|
|
|
|
if (group != "" && group != "undefined" && w.groups[group] != undefined) {
|
|
|
|
|
g[w.groups[group].id] = Object.assign({}, w.groups[group]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-01 19:50:56 +01:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
if (Object.keys(w.folders).length > 0) {
|
|
|
|
|
for (var folder in w.folders) {
|
|
|
|
|
if (folder != "" && folder != "undefined" && w.folders[folder] != undefined) {
|
|
|
|
|
w.folders[w.folders[folder].id] = Object.assign({}, w.folders[folder]);
|
|
|
|
|
}
|
2017-11-12 22:00:40 +01:00
|
|
|
|
}
|
2018-07-03 20:36:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Object.keys(g).length > 0) {
|
|
|
|
|
for (var groupId in g) {
|
|
|
|
|
w.groups[groupId] = Object.assign({}, g[groupId]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Object.keys(f).length > 0) {
|
|
|
|
|
for (var folderId in f) {
|
|
|
|
|
w.folders[folderId] = Object.assign({}, f[folderId]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.runtime.sendMessage({command: "save_groups", windowId: w.id, groups: g});
|
|
|
|
|
chrome.runtime.sendMessage({command: "save_folders", windowId: w.id, folders: f});
|
|
|
|
|
chrome.runtime.sendMessage({ command: "remote_update", groups: w.groups, folders: w.folders, tabs: [], windowId: w.id });
|
|
|
|
|
|
|
|
|
|
if (w.id == tt.CurrentWindowId) {
|
|
|
|
|
RcreateTreeStructure(w.groups, w.folders, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(w.tabs).forEach(function(Tab) {
|
|
|
|
|
if (Tab.url != "") { // missing tab, lets make one
|
|
|
|
|
chrome.tabs.create({ url: Tab.url, pinned: (Tab.parent == "pin_list" ? true : false), windowId: w.id }, function(tab) {
|
|
|
|
|
RefsTabs[Tab.id] = tab.id;
|
|
|
|
|
Tab.id = tab.id;
|
|
|
|
|
NewTabs.push(Tab);
|
|
|
|
|
chrome.runtime.sendMessage({command: "update_tab", tabId: tab.id, tab: {index: Tab.index, expand: Tab.expand, parent: Tab.parent}});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
NewTabs.push(Tab);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
ShowStatusBar({show: true, spinner: true, message: "Finding reference tabs..."});
|
|
|
|
|
for (let tInd = 0; tInd < NewTabs.length; tInd++) {
|
|
|
|
|
if (RefsTabs[NewTabs[tInd].parent] != undefined) {
|
|
|
|
|
NewTabs[tInd].parent = RefsTabs[NewTabs[tInd].parent];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 4000);
|
|
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
for (let tInd = 0; tInd < NewTabs.length; tInd++) {
|
|
|
|
|
chrome.runtime.sendMessage({command: "update_tab", tabId: NewTabs[tInd].id, tab: {index: NewTabs[tInd].index, expand: NewTabs[tInd].expand, parent: NewTabs[tInd].parent}});
|
|
|
|
|
}
|
|
|
|
|
let done = 10;
|
|
|
|
|
var Append = setInterval(function() {
|
|
|
|
|
ShowStatusBar({show: true, spinner: true, message: "Finding other windows to add tabs..."});
|
|
|
|
|
|
|
|
|
|
if (w.id == tt.CurrentWindowId) {
|
|
|
|
|
RcreateTreeStructure(w.groups, w.folders, NewTabs);
|
|
|
|
|
} else {
|
|
|
|
|
chrome.runtime.sendMessage({command: "remote_update", groups: w.groups, folders: w.folders, tabs: NewTabs, windowId: w.id });
|
|
|
|
|
}
|
|
|
|
|
if (done < 0) {
|
|
|
|
|
ShowStatusBar({show: true, spinner: false, message: "All done.", hideTimeout: 2000});
|
|
|
|
|
clearInterval(Append);
|
|
|
|
|
}
|
|
|
|
|
done--;
|
|
|
|
|
}, 500);
|
|
|
|
|
}, 6000);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-22 02:11:29 +02:00
|
|
|
|
}
|
2017-11-12 22:00:40 +01:00
|
|
|
|
});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function StartAutoSaveSession() {
|
|
|
|
|
if (opt.autosave_interval > 0 && opt.autosave_max_to_keep > 0) {
|
2018-07-03 20:36:38 +02:00
|
|
|
|
tt.AutoSaveSession = setInterval(function() {
|
2018-05-22 02:11:29 +02:00
|
|
|
|
if (opt.debug) {
|
|
|
|
|
log("f: AutoSaveSession, loop time is: "+opt.autosave_interval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let d = new Date();
|
2018-07-03 20:36:38 +02:00
|
|
|
|
let newName = d.toLocaleString().replace("/", ".").replace("/", ".").replace(":", "꞉").replace(":", "꞉");
|
|
|
|
|
ExportSession(newName, false, false, true);
|
|
|
|
|
|
|
|
|
|
ShowStatusBar({show: true, spinner: false, message: "Autosave: "+newName, hideTimeout: 1000});
|
2018-05-22 02:11:29 +02:00
|
|
|
|
|
|
|
|
|
if (document.getElementById("manager_window").style.top != "-500px") {
|
|
|
|
|
chrome.storage.local.get(null, function(storage) {
|
|
|
|
|
ReAddSessionAutomaticToManagerList(storage);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, opt.autosave_interval * 60000);
|
|
|
|
|
}
|
2017-11-12 22:00:40 +01:00
|
|
|
|
}
|