Skip to content

Commit 149411b

Browse files
committed
Change add-on to be independent on system language
Builtin root node names "Bookmarks bar" and "Other bookmarks" assumed to always be in english. This fixes #2
1 parent f8b8bb2 commit 149411b

File tree

7 files changed

+115
-43
lines changed

7 files changed

+115
-43
lines changed

bookmark/BookmarkFormatterV2.js

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
11
import { Bookmark } from "./Bookmark.js";
2+
import { BuiltinBookmark } from "./BuiltinBookmark.js";
23

34
export class BookmarkFormatterV2
45
{
5-
#mapping = {
6-
"Firefox": {
7-
"Bookmarks Menu" : "${BOOKMARKS_MENU}",
8-
"Bookmarks Toolbar": "${BOOKMARKS_BAR}"
9-
},
10-
"Chrome": {
11-
"Other bookmarks" : "${BOOKMARKS_MENU}",
12-
"Bookmarks bar": "${BOOKMARKS_BAR}",
13-
}
14-
};
15-
16-
#mappingIn = {};
17-
#mappingOut = {};
18-
6+
#builtinBookmark = new BuiltinBookmark();
197
#version = 2;
208

21-
constructor()
22-
{
23-
const mapping = this.#mapping[BROWSER_VENDOR];
24-
this.#mappingOut = mapping;
25-
this.#mappingIn = this.#reverseMappingOf(mapping);
26-
}
27-
28-
#reverseMappingOf(mapping)
9+
async init()
2910
{
30-
let reverse = {};
31-
for (let key of Object.keys(mapping))
32-
{
33-
reverse[mapping[key]] = key;
34-
}
35-
return reverse;
11+
await this.#builtinBookmark.init();
3612
}
3713

3814
read(rawJson)
@@ -72,13 +48,11 @@ export class BookmarkFormatterV2
7248

7349
#expandRootNodeIn(path)
7450
{
75-
let key = path[1];
76-
path[1] = this.#mappingIn[key];
51+
path[1] = this.#builtinBookmark.expand(path[1]);
7752
}
7853

7954
#substituteRootNodeIn(path)
8055
{
81-
let key = path[1];
82-
path[1] = this.#mappingOut[key];
56+
path[1] = this.#builtinBookmark.substitute(path[1]);
8357
}
8458
}

bookmark/BookmarkImporter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ export class BookmarkImporter
8585
const key = path.join("/");
8686
if (key in this.#cachedLeafNode)
8787
{
88-
console.debug("Using cached path: " + path);
88+
console.debug(`Using cached path: ${key}`);
8989
return this.#cachedLeafNode[key];
9090
}
9191
else
9292
{
93-
console.debug("Walking tree to find path: " + path);
93+
console.debug(`Walking tree to find path: ${key}`);
9494
const folder = await this.#getOrCreatePath(path);
9595
this.#cachedLeafNode[key] = folder;
9696
return folder;

bookmark/BookmarkTreeImport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ export class BookmarkTreeImport
4747
let key = path.join("/");
4848
if (key in this.#cachedPaths)
4949
{
50-
console.debug("Using cached path: " + path);
50+
console.debug(`Using cached path: ${key}`);
5151
return this.#cachedPaths[key];
5252
}
5353
else
5454
{
55+
console.debug(`Parsing path: ${key}`);
5556
let leafNode = this.#getOrCreatePath(path);
5657
this.#cachedPaths[key] = leafNode;
5758
return leafNode;
@@ -60,7 +61,6 @@ export class BookmarkTreeImport
6061

6162
#getOrCreatePath(path)
6263
{
63-
console.debug("Parsing path: " + path);
6464
let root = this.#elementRoot.querySelector("ul");
6565
if (root == null)
6666
{

bookmark/BrowserVendor.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Global
2+
const BROWSER_FIREFOX = "Firefox";
3+
const BROWSER_CHROME = "Chrome";
24
var BROWSER_VENDOR = "undefined";
35

46
if (typeof chrome !== "undefined")
57
{
68
if (typeof browser !== "undefined")
79
{
8-
BROWSER_VENDOR = "Firefox";
10+
BROWSER_VENDOR = BROWSER_FIREFOX;
911
}
1012
else
1113
{
12-
BROWSER_VENDOR = "Chrome";
14+
BROWSER_VENDOR = BROWSER_CHROME;
1315
}
1416
}

bookmark/BuiltinBookmark.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* The two built-in root node's name differs between Firefox and Chrome, and
3+
* also depend on the system language. In english the names is:
4+
*
5+
* - "Bookmarks Toolbar" in Mozilla Firefox and "Bookmarks bar" in Chrome,
6+
* which is substituted with "${BOOKMARKS_BAR}".
7+
* - "Bookmarks Menu" in Mozilla Firefox and "Other bookmarks" in Chrome,
8+
* which is substituted with "${BOOKMARKS_MENU}".
9+
*/
10+
export class BuiltinBookmark
11+
{
12+
#mapping = {};
13+
#mappingReverse = {};
14+
15+
async init()
16+
{
17+
this.#createMappingFor(
18+
await this.#getBookmarkNameFrom(this.#getNodeIdForBookmarksBar()),
19+
"${BOOKMARKS_BAR}");
20+
this.#createMappingFor(
21+
await this.#getBookmarkNameFrom(this.#getNodeIdForBookmarksMenu()),
22+
"${BOOKMARKS_MENU}");
23+
}
24+
25+
substitute(name)
26+
{
27+
return this.#mapping[name];
28+
}
29+
30+
expand(substitutedName)
31+
{
32+
return this.#mappingReverse[substitutedName];
33+
}
34+
35+
#createMappingFor(name, substitutedName)
36+
{
37+
console.debug(`Create mapping for \"${name}\" : ${substitutedName}`);
38+
this.#mapping[name] = substitutedName;
39+
this.#mappingReverse[substitutedName] = name;
40+
}
41+
42+
async #getBookmarkNameFrom(id)
43+
{
44+
const bookmarkNode = await browser.bookmarks.get(id);
45+
if (bookmarkNode.length == 1)
46+
{
47+
return bookmarkNode[0].title;
48+
}
49+
else
50+
{
51+
throw `Failed to find bookmark root node with id: ${id}`;
52+
}
53+
}
54+
55+
#getNodeIdForBookmarksBar()
56+
{
57+
if (BROWSER_VENDOR == BROWSER_FIREFOX)
58+
{
59+
return "toolbar_____";
60+
}
61+
else if (BROWSER_VENDOR == BROWSER_CHROME)
62+
{
63+
return "1";
64+
}
65+
else
66+
{
67+
throw `Unsupported browser vendor: ${BROWSER_VENDOR}`
68+
}
69+
}
70+
71+
#getNodeIdForBookmarksMenu()
72+
{
73+
if (BROWSER_VENDOR == BROWSER_FIREFOX)
74+
{
75+
return "menu________";
76+
}
77+
else if (BROWSER_VENDOR == BROWSER_CHROME)
78+
{
79+
return "2";
80+
}
81+
else
82+
{
83+
throw `Unsupported browser vendor: ${BROWSER_VENDOR}`;
84+
}
85+
}
86+
}

bookmark/TabViewExport.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ export class TabViewExport
3939
this.#bookmarkTree = null;
4040
}
4141

42-
#exportBookmarks()
42+
async #exportBookmarks()
4343
{
4444
if (this.#tree && this.#bookmarkTree)
4545
{
4646
const exporter = new BookmarkExporter();
4747
exporter.setBookmarkTree(this.#bookmarkTree)
4848
exporter.setSelection(this.#tree.getSelectedBookmarksId());
4949
let formatter = new BookmarkFormatterV2();
50+
await formatter.init();
5051
let bookmarks = formatter.write(exporter.export());
5152
let fileWriter = new BookmarkFile();
5253
fileWriter.setVersion(formatter.getVersion());

bookmark/TabViewImport.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class TabViewImport
3636
await this.#fileReader.load();
3737
this.#tree = new BookmarkTreeImport();
3838
this.#tree.setElementRoot(document.getElementById(this.#containerId));
39-
this.#tree.setBookmarks(this.#getLoadedBookmarks());
39+
const loadedBookmarks = await this.#getLoadedBookmarks();
40+
this.#tree.setBookmarks(loadedBookmarks);
4041
this.#tree.render();
4142
}
4243

@@ -55,7 +56,8 @@ export class TabViewImport
5556
importer.setSelection(selectedBookmarks);
5657
try
5758
{
58-
await importer.import(this.#getLoadedBookmarks());
59+
const loadedBookmarks = await this.#getLoadedBookmarks();
60+
await importer.import(loadedBookmarks);
5961
const statistics = importer.getImportedStatistics();
6062
this.#showImportedStatus(
6163
statistics.newBookmarks,
@@ -67,12 +69,12 @@ export class TabViewImport
6769
}
6870
}
6971

70-
#getLoadedBookmarks()
72+
async #getLoadedBookmarks()
7173
{
7274
const version = this.#fileReader.getVersion();
7375
const formatter = {
7476
"1": new BookmarkFormatterV1(),
75-
"2": new BookmarkFormatterV2(),
77+
"2": await this.#newBookmarkFormatterV2(),
7678
};
7779
if (version in formatter)
7880
{
@@ -85,6 +87,13 @@ export class TabViewImport
8587
}
8688
}
8789

90+
async #newBookmarkFormatterV2()
91+
{
92+
const formatter = new BookmarkFormatterV2();
93+
await formatter.init();
94+
return formatter;
95+
}
96+
8897
#showImportedStatus(newBookmarks, existingBookmarks)
8998
{
9099
const type = "success";

0 commit comments

Comments
 (0)