From f19841839c826c766fe659ae92784f0e01dc2796 Mon Sep 17 00:00:00 2001 From: Unknown Person <104310243+unknownpersonog@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:52:46 +0530 Subject: [PATCH] **Bug Fixes and Additions** Following additions have been made: - Tabs are now at menu level - Adblocker, downloads and help have been moved inside submenu of Menu. - Pause and Cancel Downloads - Added copy url for downloads and changed the current download history menu - A shortcut to open download history (Cmd or Ctrl +J) Following bug fixes have been made: - Downloader was missing in previous version due to checking mistakes in code. Added it back. - Progress bar now shows percentage only till two decimals - Sometimes adblocker enabling and disabling failed to work, it has been fixed. - Error handling for some errors - Added auto checking blocker state --- main.js | 325 +++++++++++++++++++++++++++++++++++++++------------ package.json | 3 +- 2 files changed, 251 insertions(+), 77 deletions(-) diff --git a/main.js b/main.js index 6782165..82fbe59 100644 --- a/main.js +++ b/main.js @@ -18,9 +18,9 @@ const fs = require("fs").promises; let win; const store = new Store(); -let { adblockerEnabled } = store.get("adBlockerState") - ? store.get("adBlockerState") - : true; +if (!store.get("adBlockerState")) { + store.set("adBlockerState", true); +} let blocker; // Declare blocker outside of createWindow function let views = []; @@ -29,9 +29,6 @@ async function askPrompt() { title: "Enter URL", label: "URL:", value: "https://google.com", - inputAttrs: { - type: "url", - }, type: "input", }).then((r) => { if (r === null) { @@ -42,6 +39,7 @@ async function askPrompt() { } }); } + async function createWindow() { // Only create a new blocker if one doesn't already exist if (!blocker) { @@ -78,7 +76,9 @@ async function createWindow() { url ? createTab(url) : app.quit(); updateMenu(); // Initial menu setup - + if (store.get("adBlockerState") === true) { + blocker.enableBlockingInSession(session.defaultSession); + } app.on("window-all-closed", () => { if (process.platform !== "darwin") { app.quit(); @@ -92,8 +92,8 @@ async function createWindow() { }); } -let activeViewIndex = 0; - +let activeViewIndex = 0; +let downloadItem; function createTab(url) { if (views.length >= 5) { dialog.showErrorBox("Error", "Cannot open more than 5 tabs."); @@ -127,7 +127,88 @@ function createTab(url) { view.webContents.on("page-title-updated", (event, title) => { win.setTitle(title); + updateMenu(); }); + + view.webContents.session.on("will-download", (event, item, webContents) => { + downloadItem = item; // Store the download item + + // Create a new progress bar + let progressBar = new ProgressBar({ + indeterminate: false, + text: "Downloading...", + detail: "Wait...", + }); + progressBar + .on("completed", function () { + progressBar.detail = "Task completed. Exiting..."; + }) + .on("aborted", function (value) { + console.info(`Progress bar closed at ${value}`); + dialog.showMessageBox({ + title: "Progress Bar Closed", + message: "Please use menu to track progress!", + }); + }) + .on("progress", function (value) { + progressBar.detail = `Downloaded ${value.toFixed( + 2, + )} out of ${progressBar + .getOptions() + .maxValue.toFixed( + 2, + )}...\n To pause or cancel download check the menu!`; + }); + + item.on("updated", (event, state) => { + if (state === "interrupted") { + dialog.showMessageBox({ + type: "info", + title: "Info", + message: "Download is interrupted but can be resumed", + }); + } else if (state === "progressing") { + if (item.isPaused()) { + progressBar.detail = + "Download paused. Check the menu to resume or cancel."; + } else { + // Update the progress bar value + let totalBytes = item.getTotalBytes(); + if (totalBytes > 0) { + let progress = (item.getReceivedBytes() / totalBytes) * 100; + progressBar.value = progress; + } else { + // If totalBytes is not available, switch to indeterminate mode + progressBar.options.indeterminate = true; + } + } + } + }); + + item.once("done", (event, state) => { + if (state === "completed") { + progressBar.setCompleted(); + let downloadHistory = store.get("downloadHistory") || []; + downloadHistory.push({ + url: item.getURL(), + filename: item.getFilename(), + fileSize: item.getTotalBytes(), + }); + store.set("downloadHistory", downloadHistory); + } else { + dialog.showErrorBox("Error", `Download failed: ${state}`); + progressBar.close(); + } + }); + }); + + view.webContents.on("did-fail-load", (event, errorCode, errorDescription) => { + console.error( + "Oops! Something went wrong!", + "Error Code: " + errorCode + " : " + errorDescription, + ); + }); + return view; } @@ -169,19 +250,22 @@ function changeTab(index) { const size = win.getContentSize(); view.setBounds({ x: 0, y: 0, width: size[0], height: size[1] }); win.setBrowserView(views[index]); - + const title = view.webContents.getTitle(); + win.setTitle(title); activeViewIndex = index; // Update the active view index here } function updateMenu() { const tabsMenu = views.map((view, index) => ({ - label: `Tab ${index + 1}`, - accelerator: process.platform === "darwin" ? `Command+${index + 1}` : `Ctrl+${index + 1}`, + label: `${view.webContents.getTitle()}`, + accelerator: + process.platform === "darwin" + ? `Command+${index + 1}` + : `Ctrl+${index + 1}`, click() { changeTab(index); }, })); - const tabsCloseMenu = views.map((view, index) => ({ label: `Tab ${index + 1}`, click() { @@ -223,6 +307,137 @@ function updateMenu() { }, ], }, + { + label: "Adblocker", + submenu: [ + { + label: "Enable", + type: "radio", + checked: store.get("adBlockerState"), + click(menuItem) { + if (!store.get("adBlockerState")) { + blocker.enableBlockingInSession(session.defaultSession); + store.set("adBlockerState", !store.get("adBlockerState")); + } + }, + }, + { + label: "Disable", + type: "radio", + checked: !store.get("adBlockerState"), + click(menuItem) { + if (store.get("adBlockerState")) { + blocker.disableBlockingInSession(session.defaultSession); + store.set("adBlockerState", !store.get("adBlockerState")); + } + }, + }, + ], + }, + { + label: "Download", + submenu: [ + { + label: "Show Download History", + accelerator: process.platform === "darwin" ? "Command+J" : "Ctrl+J", + click() { + let downloadHistory = store.get("downloadHistory"); + let message = "Download History:\n"; + downloadHistory.forEach((download, index) => { + message += `${index + 1}. ${download.filename} (${( + download.fileSize / + (1000 * 1000) + ).toFixed(2)}MB)\n`; + }); + + dialog + .showMessageBox({ + type: "info", + title: "Download History", + message: message, + buttons: ["OK", "Copy URL"], + }) + .then((result) => { + if (result.response === 1) { + // If 'Copy URL' button is clicked + // Create an options object for the dropdown menu + let options = {}; + downloadHistory.forEach((download, index) => { + options[index] = `${download.filename} (${( + download.fileSize / + (1000 * 1000) + ).toFixed(2)}MB)`; + }); + + prompt({ + title: "Copy URL", + label: "Select a download:", + type: "select", + selectOptions: options, + }).then((result) => { + if (result !== null) { + let index = parseInt(result); + clipboard.writeText(downloadHistory[index].url); + } + }); + } + }); + }, + }, + { + label: "Pause/Resume Download", + click() { + if (downloadItem) { + if (downloadItem.isPaused()) { + downloadItem.resume(); + } else { + downloadItem.pause(); + } + } + }, + }, + { + label: "Show Progress", + click() { + let progress = + (downloadItem.getReceivedBytes() / + downloadItem.getTotalBytes()) * + 100; + dialog.showMessageBox({ + title: "Current Progress", + message: `${progress.toFixed( + 2, + )}% Completed \n Reopen to track new progress! `, + }); + }, + }, + { + label: "Cancel Download", + click() { + if (downloadItem) { + downloadItem.cancel(); + } + }, + }, + ], + }, + + { + label: "Help", + submenu: [ + { + label: "About", + click() { + dialog.showMessageBox({ + type: "info", + title: "About", + message: `Version: 0.0.5!\nThis is a browser developed by UnknownVPS using Electron.`, + buttons: ["OK"], + }); + }, + }, + ], + }, { label: "Quit", accelerator: process.platform === "darwin" ? "Command+Q" : "Ctrl+Q", @@ -297,74 +512,33 @@ function updateMenu() { ], }, { - label: "Adblocker", - submenu: [ - { - label: "Enable", - type: "checkbox", - checked: store.get("adBlockerState"), - click(menuItem) { - if (!adblockerEnabled) { - blocker.enableBlockingInSession(session.defaultSession); - adblockerEnabled = !adblockerEnabled; - store.set("adBlockerState", adblockerEnabled); - menuItem.menu.items[1].checked = false; - } else { - menuItem.menu.items[0].checked = true; - } - }, - }, - { - label: "Disable", - type: "checkbox", - checked: !store.get("adBlockerState"), - click(menuItem) { - if (adblockerEnabled) { - blocker.disableBlockingInSession(session.defaultSession); - adblockerEnabled = !adblockerEnabled; - store.set("adBlockerState", adblockerEnabled); - menuItem.menu.items[0].checked = false; - } else { - menuItem.menu.items[1].checked = true; - } - }, - }, - ], + type: "separator", }, { - label: "Download", - submenu: [ - { - label: "Show Download History", - click() { - dialog.showMessageBox({ - message: - "Download history: " + - JSON.stringify(store.get("downloadHistory")), - }); - }, - }, - ], + label: "Tabs:", }, + ]; + if (views.length >= 1) { + for (let index = 0; index < views.length; index++) { + template.push({ + label: tabsMenu[index].label.substring(0, 10), + click() { + changeTab(index); + }, + }); - { - label: "Help", - submenu: [ - { - label: "About", - click() { - dialog.showMessageBox({ - type: "info", - title: "About", - message: `Version: 0.0.4!\nThis is a browser developed by UnknownVPS using Electron.`, - buttons: ["OK"], - }); - }, + template.push({ + label: "×", + click() { + closeTab(index); }, - ], - }, - ]; + }); + template.push({ + type: "separator", + }); + } + } const menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); } @@ -382,3 +556,4 @@ app.on("activate", () => { createWindow(); } }); + diff --git a/package.json b/package.json index 74b4fcb..1a3c8fc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "UVS_Browser", "productName": "UVS_Browser", - "version": "0.0.4", + "version": "0.0.5", "description": "Electron based Browser for increasing browsing speed while including support modern day tools.", "homepage": "https://github.com/unknownpersonog", "main": "main.js", @@ -42,7 +42,6 @@ "electron": "^27.1.0", "electron-builder": "^24.6.4", "electron-forge-maker-appimage": "github:Marcus10110/electron-forge-maker-appimage", - "electron-packager": "^17.1.2", "yarn": "^1.22.21" } }