diff --git a/bookmarks.json b/bookmarks.json index 0637a08..c1c7ba7 100644 --- a/bookmarks.json +++ b/bookmarks.json @@ -1 +1,17 @@ -[] \ No newline at end of file +[ + { + "title": "Google", + "url": "https://www.google.com", + "icon": "search" + }, + { + "title": "GitHub", + "url": "https://github.com", + "icon": "code" + }, + { + "title": "YouTube", + "url": "https://www.youtube.com", + "icon": "play_arrow" + } +] \ No newline at end of file diff --git a/main.js b/main.js index d478531..180f25a 100644 --- a/main.js +++ b/main.js @@ -375,6 +375,53 @@ ipcMain.on('homepage-changed', (event, url) => { console.log('[MAIN] homepage-changed →', url); }); +// Bookmark management +ipcMain.handle('load-bookmarks', async () => { + try { + const bookmarksPath = path.join(__dirname, 'bookmarks.json'); + if (fs.existsSync(bookmarksPath)) { + const data = fs.readFileSync(bookmarksPath, 'utf8'); + const bookmarks = JSON.parse(data); + console.log(`Loaded ${bookmarks.length} bookmarks from file`); + return bookmarks; + } + console.log('No bookmarks file found, starting with empty array'); + return []; + } catch (error) { + console.error('Error loading bookmarks:', error); + // Try to create a backup if the file is corrupted + const bookmarksPath = path.join(__dirname, 'bookmarks.json'); + const backupPath = path.join(__dirname, `bookmarks.backup.${Date.now()}.json`); + try { + if (fs.existsSync(bookmarksPath)) { + fs.copyFileSync(bookmarksPath, backupPath); + console.log(`Corrupted bookmarks file backed up to: ${backupPath}`); + } + } catch (backupError) { + console.error('Failed to create backup:', backupError); + } + return []; + } +}); + +ipcMain.handle('save-bookmarks', async (event, bookmarks) => { + try { + const bookmarksPath = path.join(__dirname, 'bookmarks.json'); + + // Create a backup before saving + if (fs.existsSync(bookmarksPath)) { + const backupPath = path.join(__dirname, 'bookmarks.backup.json'); + fs.copyFileSync(bookmarksPath, backupPath); + } + + fs.writeFileSync(bookmarksPath, JSON.stringify(bookmarks, null, 2)); + console.log(`Saved ${bookmarks.length} bookmarks to file`); + return true; + } catch (error) { + console.error('Error saving bookmarks:', error); + return false; + } +}); ipcMain.handle('clear-browser-data', async () => { try { diff --git a/preload.js b/preload.js index b804c96..ccf7d7d 100644 --- a/preload.js +++ b/preload.js @@ -17,6 +17,14 @@ const electronAPI = { console.error('IPC send error:', err); } }, + // Send message to embedding page (webview host) + sendToHost: (ch, ...args) => { + try { + return ipcRenderer.sendToHost(ch, ...args); + } catch (err) { + console.error('IPC sendToHost error:', err); + } + }, invoke: (ch, ...args) => { try { return ipcRenderer.invoke(ch, ...args); diff --git a/renderer/home.js b/renderer/home.js index 308424d..9e64858 100644 --- a/renderer/home.js +++ b/renderer/home.js @@ -1,7 +1,5 @@ import { icons as initialIcons, fetchAllIcons } from './icons.js'; -const BOOKMARKS_KEY = 'steamos_browser_bookmarks'; - const bookmarkList = document.getElementById('bookmarkList'); const titleInput = document.getElementById('titleInput'); const urlInput = document.getElementById('urlInput'); @@ -26,18 +24,44 @@ const searchEngines = { }; let selectedSearchEngine = 'google'; -let bookmarks = JSON.parse(localStorage.getItem(BOOKMARKS_KEY)) || []; +let bookmarks = []; -function saveBookmarks() { - localStorage.setItem(BOOKMARKS_KEY, JSON.stringify(bookmarks)); +// Load bookmarks from main via Electron IPC +// Load bookmarks via contextBridge API +async function loadBookmarks() { + try { + let data = []; + // Use bookmarksAPI if available + if (window.bookmarksAPI && typeof window.bookmarksAPI.load === 'function') { + data = await window.bookmarksAPI.load(); + } else if (window.electronAPI && typeof window.electronAPI.invoke === 'function') { + data = await window.electronAPI.invoke('load-bookmarks'); + } else { + console.error('No API available to load bookmarks'); + } + return Array.isArray(data) ? data : []; + } catch (error) { + console.error('Error loading bookmarks:', error); + return []; + } } +// Save bookmarks to main process +// Save bookmarks via contextBridge API +async function saveBookmarks() { + try { + await window.bookmarksAPI.save(bookmarks); + } catch (error) { + console.error('Error saving bookmarks:', error); + } +} + +// Render bookmarks function renderBookmarks() { - const list = JSON.parse(localStorage.getItem(BOOKMARKS_KEY) || '[]'); bookmarkList.innerHTML = ''; // Render each bookmark - list.forEach((b, index) => { + bookmarks.forEach((b, index) => { const box = document.createElement('div'); box.className = 'bookmark'; @@ -54,14 +78,21 @@ function renderBookmarks() { const close = document.createElement('button'); close.textContent = '×'; close.className = 'delete-btn'; - close.onclick = (e) => { + close.onclick = async (e) => { e.stopPropagation(); bookmarks.splice(index, 1); - saveBookmarks(); + await saveBookmarks(); renderBookmarks(); }; - box.onclick = () => window.location.href = b.url; + // Navigate via IPC to host page + box.onclick = () => { + if (window.electronAPI && typeof window.electronAPI.sendToHost === 'function') { + window.electronAPI.sendToHost('navigate', b.url); + } else { + console.error('Unable to send navigation IPC to host'); + } + }; box.appendChild(label); box.appendChild(close); @@ -121,14 +152,14 @@ renderIconGrid(); } })(); -saveBookmarkBtn.onclick = () => { +saveBookmarkBtn.onclick = async () => { const title = titleInput.value.trim(); const url = urlInput.value.trim(); const icon = selectedIcon; if (!title || !url) return; bookmarks.push({ title, url, icon }); - saveBookmarks(); + await saveBookmarks(); renderBookmarks(); titleInput.value = ''; @@ -181,5 +212,8 @@ searchInput.addEventListener('keydown', e => { if (e.key === 'Enter') searchBtn.click(); }); -// initial render from localStorage -renderBookmarks(); +// Load and render bookmarks immediately +(async () => { + bookmarks = await loadBookmarks(); + renderBookmarks(); +})(); diff --git a/renderer/index.html b/renderer/index.html index eb9769d..f4e836a 100644 --- a/renderer/index.html +++ b/renderer/index.html @@ -56,25 +56,19 @@ -