Skip to content

Commit

Permalink
work on #291
Browse files Browse the repository at this point in the history
  • Loading branch information
drive4ik committed Aug 26, 2023
1 parent 1ff0629 commit a853130
Show file tree
Hide file tree
Showing 14 changed files with 1,022 additions and 314 deletions.
10 changes: 1 addition & 9 deletions addon/src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@
"message": "Help",
"description": "Help"
},
"autoBackupCloudEnableTitle": {
"syncEnableTitle": {
"message": "Enable auto backup to the cloud",
"description": "Enable auto backup to the cloud"
},
Expand All @@ -1245,14 +1245,6 @@
"message": "Check token",
"description": "Check Github gist personal access token"
},
"githubGistIdTitle": {
"message": "Github backup gist Id",
"description": "Github backup gist Id"
},
"findOrCreateGithubGistId": {
"message": "Find or create gist Id",
"description": "Find or create gist Id"
},
"githubGistBackupDescription": {
"message": "Simple Tab Groups backup file for cloud sync",
"description": "Simple Tab Groups backup file for cloud sync"
Expand Down
33 changes: 33 additions & 0 deletions addon/src/js/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ export async function removeTabGroup(tabId) {
return browser.sessions.removeTabValue(tabId, 'groupId').catch(() => {});
}

// syncId
async function loadSyncId(tabId) {
if (tabs[tabId]) {
if (tabs[tabId].syncId) {
return tabs[tabId].syncId;
}

return tabs[tabId].syncId = await browser.sessions.getTabValue(tabId, 'syncId');
}
}

export async function setSyncId(tabId, syncId) {
if (syncId) {
if (tabs[tabId]) {
tabs[tabId].syncId = syncId;
} else {
tabs[tabId] = {syncId};
}

return browser.sessions.setTabValue(tabId, 'syncId', syncId);
}
}

export async function removeSyncId(tabId) {
delete tabs[tabId]?.syncId;
return browser.sessions.removeTabValue(tabId, 'syncId').catch(() => {});
}


// favIconUrl
async function loadTabFavIcon(tabId) {
if (tabs[tabId]) {
Expand Down Expand Up @@ -176,6 +205,7 @@ export async function loadTabSession(tab, includeFavIconUrl = true, includeThumb

await Promise.all([
loadTabGroup(tab.id),
loadSyncId(tab.id),
includeFavIconUrl ? loadTabFavIcon(tab.id) : null,
includeThumbnail ? loadTabThumbnail(tab.id) : null,
]);
Expand All @@ -186,6 +216,7 @@ export async function loadTabSession(tab, includeFavIconUrl = true, includeThumb
export async function setTabSession(tab) {
await Promise.all([
setTabGroup(tab.id, tab.groupId),
loadSyncId(tab.id, tab.syncId),
setTabFavIcon(tab.id, tab.favIconUrl),
setTabThumbnail(tab.id, tab.thumbnail),
]);
Expand All @@ -195,6 +226,7 @@ export async function setTabSession(tab) {

export function applySession(toObj, fromObj) {
fromObj.groupId && (toObj.groupId = fromObj.groupId);
fromObj.syncId && (toObj.syncId = fromObj.syncId);
fromObj.favIconUrl && (toObj.favIconUrl = fromObj.favIconUrl);
fromObj.thumbnail && (toObj.thumbnail = fromObj.thumbnail);

Expand All @@ -208,6 +240,7 @@ export function applyTabSession(tab) {
export async function removeTabSession(tabId) {
return Promise.all([
removeTabGroup(tabId),
removeSyncId(tabId),
removeTabFavIcon(tabId),
removeTabThumbnail(tabId),
]);
Expand Down
25 changes: 17 additions & 8 deletions addon/src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,26 @@ export const EXTENSIONS_WHITE_LIST = Object.freeze({
},
});

export const GIT_GIST_FILE_NAME_PARTS = Object.freeze({
start: 'STG-',
end: '.json',
});

export const DEFAULT_SYNC_OPTIONS = Object.freeze({
version: MANIFEST.version,
githubGistToken: '',
githubGistFileName: 'STG-backup.json',
githubGistFileName: GIT_GIST_FILE_NAME_PARTS.start + 'backup' + GIT_GIST_FILE_NAME_PARTS.end,
githubGistId: '',
});

const IS_AVAILABLE_SYNC_STORAGE = browser.storage.sync instanceof Object;
export const SYNC_STORAGE_FSYNC = 'ff-sync';
export const SYNC_STORAGE_LOCAL = 'local';

export const DEFAULT_OPTIONS = Object.freeze({
version: MANIFEST.version,
groups: [],
lastCreatedGroupPosition: 0,
lastCreatedGroupPosition: 0, // TODO remove

// options

Expand Down Expand Up @@ -328,12 +337,12 @@ export const DEFAULT_OPTIONS = Object.freeze({
autoBackupFolderName: '',
autoBackupByDayIndex: true,

autoBackupCloudEnable: true,
autoBackupCloudTimeStamp: 1,
autoBackupCloudIntervalKey: AUTO_BACKUP_INTERVAL_KEY.days, // minutes, hours, days
autoBackupCloudIntervalValue: 1,
autoBackupCloudIncludeTabFavIcons: true,
// ...DEFAULT_SYNC_OPTIONS,
syncEnable: true,
syncOptionsLocation: IS_AVAILABLE_SYNC_STORAGE ? SYNC_STORAGE_FSYNC : SYNC_STORAGE_LOCAL,
syncId: 1,
syncIntervalKey: AUTO_BACKUP_INTERVAL_KEY.days, // minutes, hours, days
syncIntervalValue: 1,
syncTabFavIcons: true,

theme: 'auto', // auto, light, dark

Expand Down
28 changes: 28 additions & 0 deletions addon/src/js/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,34 @@ export function getAll(withDefaultContainer, containersStorage = containers) {
return containers;
}

export function getToExport(storageData, containersStorage = containers) {
const containersToExport = new Set;

storageData.groups.forEach(group => {
group.tabs.forEach(tab => {
if (!isTemporary(tab.cookieStoreId, undefined, undefined, containersStorage)) {
containersToExport.add(tab.cookieStoreId);
}
});

containersToExport.add(group.newTabContainer);

group.catchTabContainers.forEach(cookieStoreId => containersToExport.add(cookieStoreId));
group.excludeContainersForReOpen.forEach(cookieStoreId => containersToExport.add(cookieStoreId));
});

containersToExport.delete(Constants.DEFAULT_COOKIE_STORE_ID);
containersToExport.delete(Constants.TEMPORARY_CONTAINER);

const result = {};

[...containersToExport]
.filter(Boolean)
.forEach(cookieStoreId => result[cookieStoreId] = {...containersStorage[cookieStoreId]});

return result;
}

export async function removeUnusedTemporaryContainers(tabs, containersStorage = containers) {
const log = logger.start('removeUnusedTemporaryContainers');

Expand Down
42 changes: 33 additions & 9 deletions addon/src/js/mixins/sync-cloud.mixin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@

// import * as Constants from '/js/constants.js';
import Logger from '/js/logger.js';
import {GithubGist} from '/js/sync/cloud/cloud.js';
import Messages from '/js/messages.js';
// import * as Storage from '/js/storage.js';
// import * as Groups from '/js/groups.js';
// import * as Utils from '/js/utils.js';
// import * as Cache from '/js/cache.js';
// import JSON from '/js/json.js';
// import * as SyncStorage from '/js/sync/sync-storage.js';
// import {GithubGist, syncData} from '/js/sync/cloud/cloud.js';

const logger = new Logger('cloud mixin');

Expand All @@ -13,12 +20,29 @@ export default {
// },
methods: {
async syncCloud() {
// logger.log('')
const result = [];
console.time('cloud');
const GithubGistCloud = new GithubGist(localStorage.token, 'STG-backup.json');
result.push(await GithubGistCloud.findGistId());
result.push(await GithubGistCloud.getGist());
// const log = logger.start('sync button');
console.clear();
let res2 = await Messages.sendMessageModule('BG.cloudSync');
console.debug('res2', res2);
// const result = [];
// console.time('cloud');
// const syncOptions = await SyncStorage.get();
// const GithubGistCloud = new GithubGist(syncOptions.githubGistToken, syncOptions.githubGistFileName, syncOptions.githubGistId);
// console.debug(await GithubGistCloud.findGistId());
// let localData = await Storage.get();
// let {groups} = await Groups.load(null, true);
// // const allTabs = Utils.concatTabs(groups);
// // await Promise.all(allTabs.map(tab => browser.sessions.setTabValue(tab.id, 'syncId', 16879931)))
// // console.debug('finish')
// // return;
// console.debug('groups:', groups);
// localData.groups = groups;
// let cloud = await GithubGistCloud.getGist();
// let cloudData = JSON.parse(cloud.content);
// console.debug('cloudData:', JSON.clone(cloudData));
// console.debug('before', JSON.clone(localData.groups), JSON.clone(cloudData.groups));
// await syncData(localData, cloudData);
// console.debug('after', localData.groups, cloudData.groups);
// const result = await GitHub.findFileId(localStorage.token, 'STG-backup.json')
// let id = await github.foundStgGistId();
// let data = await github.getGist();
Expand All @@ -34,8 +58,8 @@ export default {
// console.debug('gist id', id)
// console.debug('get gist data', data)
// console.debug('create gist data', createdata)
console.timeEnd('cloud');
console.debug('gist result', result)
// console.timeEnd('cloud');
// console.debug('gist result', result)
},
},
}
2 changes: 1 addition & 1 deletion addon/src/js/storage-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getKeysData(keys, defaultData) {
} else if (typeof keys === 'string') {
keysData = {[keys]: defaultData[keys]};
} else { // if keys is object
keysData = keys;
keysData = {...keys};
}

return keysData;
Expand Down
Loading

0 comments on commit a853130

Please sign in to comment.