Skip to content

Commit

Permalink
work on #291, add replace uuid into moz-extension urls to id of this …
Browse files Browse the repository at this point in the history
…extension, for better restoring tabs between computers
  • Loading branch information
drive4ik committed Oct 4, 2024
1 parent e93fb84 commit a777919
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions addon/src/js/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Storage from './storage.js';
import * as Cache from './cache.js';
import * as Containers from './containers.js';
import * as Bookmarks from './bookmarks.js';
import * as Management from './management.js';
// import Messages from './messages.js';
// import JSON from './json.js';
import * as Tabs from './tabs.js';
Expand Down Expand Up @@ -501,6 +502,8 @@ export async function archiveToggle(groupId) {
if (group.isArchive) {
group.isArchive = false;

Management.replaceMozExtensionTabUrls(group.tabs, 'uuid');

await backgroundSelf.createTabsSafe(setNewTabsParams(group.tabs, group), true);

group.tabs = [];
Expand All @@ -516,6 +519,8 @@ export async function archiveToggle(groupId) {
({group, groups} = await load(groupId, true));
}

Management.replaceMozExtensionTabUrls(group.tabs, 'id');

tabsToRemove = group.tabs;

group.isArchive = true;
Expand Down
45 changes: 40 additions & 5 deletions addon/src/js/management.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,48 @@ export function getIgnoredConflictedExtensions() {
} */

export function getExtensionByUUID(uuid, extensionsStorage = extensions) {
if (!uuid) {
return;
if (isUUID(uuid)) {
for (let i in extensionsStorage) {
if (extensionsStorage[i]?.hostPermissions?.some(url => url.includes(uuid))) {
return extensionsStorage[i];
}
}
}
}

const UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

export function isUUID(uuid) {
return UUID_REGEXP.test(uuid);
}

const MOZ_EXTENSION_URL_REGEXP = /^moz-extension:\/\/([^\/]+)/;

export function UUIDtoId(uuid, extensionsStorage = extensions) {
return getExtensionByUUID(uuid, extensionsStorage)?.id;
}

export function idToUUID(id, extensionsStorage = extensions) {
if (extensionsStorage[id]) {
for (const url of extensionsStorage[id].hostPermissions) {
const [, uuid] = MOZ_EXTENSION_URL_REGEXP.exec(url) ?? [];

if (uuid) {
return uuid;
}
}
}
}

export function replaceMozExtensionTabUrls(tabs, replaceTo, extensionsStorage = extensions) {
const func = replaceTo === 'id' ? UUIDtoId : idToUUID;

for (let i in extensionsStorage) {
if (extensionsStorage[i]?.hostPermissions?.some(url => url.includes(uuid))) {
return extensionsStorage[i];
for (const tab of tabs) {
if (tab.url.startsWith('moz-extension')) {
tab.url = tab.url.replace(MOZ_EXTENSION_URL_REGEXP, (match, value) => {
value = func(value, extensionsStorage) ?? value;
return `moz-extension://${value}`;
});
}
}
}
4 changes: 4 additions & 0 deletions addon/src/stg-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3590,6 +3590,10 @@ async function runMigrateForData(data, applyToCurrentInstance = true) {
group.exportToBookmarks = group.exportToBookmarksWhenAutoBackup;
delete group.exportToBookmarksWhenAutoBackup;
delete group.bookmarkId;

if (group.isArchive) {
Management.replaceMozExtensionTabUrls(group.tabs, 'id');
}
});

delete data.defaultGroupProps.leaveBookmarksOfClosedTabs;
Expand Down

0 comments on commit a777919

Please sign in to comment.