Skip to content

Commit 93ad3a7

Browse files
author
jdev082
committed
feat(bookmarks): initial implementation of importing bookmarks from HTML
Signed-off-by: jdev082 <jdev082@jdev.eu.org>
1 parent 5ee526d commit 93ad3a7

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

main/main.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-enable no-undef */
22
/* eslint-enable no-unused-vars */
33
const { ElectronBlocker } = require('@cliqz/adblocker-electron');
4-
const { app, BrowserWindow, Menu, session, ipcMain } = require('electron');
4+
const { app, BrowserWindow, Menu, session, ipcMain, dialog } = require('electron');
55
const path = require('path');
66
const fs = require('fs');
77
const https = require('https');
88
const contextMenu = require('electron-context-menu');
9+
const parse = require('bookmarks-parser')
910

1011
if (require('electron-squirrel-startup')) app.quit();
1112

@@ -144,7 +145,41 @@ const template = [{
144145
click: function () {
145146
mainWindow.webContents.toggleDevTools();
146147
},
148+
},
149+
{
150+
label: 'htmlmarks',
151+
accelerator: 'CmdOrCtrl+H',
152+
click: function() {
147153

154+
dialog.showOpenDialog({
155+
properties: ['openFile', 'multiSelections']
156+
}).then(result => {
157+
if (!result.canceled) {
158+
const filePaths = result.filePaths;
159+
const file = filePaths[0]
160+
try {
161+
const buf = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' });
162+
parse(buf, function(e,r) {
163+
console.log(e)
164+
console.log(r['bookmarks'][0].children)
165+
marks = r['bookmarks'][0].children
166+
for (var i = 0; i < marks.length; i++)
167+
{
168+
url = marks[i]['url'];
169+
title = marks[i]['title']
170+
js = `progBookmarkTab("${url}", "${title}")`
171+
console.log(url)
172+
mainWindow.webContents.executeJavaScript(js)
173+
}
174+
})
175+
} catch {
176+
return;
177+
}
178+
}
179+
}).catch(err => {
180+
console.error('Error opening file dialog:', err);
181+
});
182+
}
148183
}];
149184

150185
app.on('web-contents-created', (e, contents) => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"dependencies": {
3131
"@cliqz/adblocker-electron": "^1.27.3",
32+
"bookmarks-parser": "^1.5.0",
3233
"cross-fetch": "^4.0.0",
3334
"electron-context-menu": "^3.6.1",
3435
"electron-squirrel-startup": "^1.0.1",

src/bookmarks.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ function bookmarkCurrentTab() {
5050
storeBookmarks();
5151
}
5252

53+
function progBookmarkTab(u, t) {
54+
updateBookmarksVar();
55+
const toPush = {
56+
title: t,
57+
url: u,
58+
}
59+
bookmarks.push(toPush);
60+
storeBookmarks();
61+
}
62+
5363
/**
5464
* Updates the bookmarks variable and displays the bookmarks
5565
*/

0 commit comments

Comments
 (0)