From 981f6da829b65a0bd9d3c71a3c7496b9142435de Mon Sep 17 00:00:00 2001 From: Solninja A <51935570+SolninjaA@users.noreply.github.com> Date: Mon, 27 Jan 2025 17:26:25 +1000 Subject: [PATCH] Revamp popup settings --- background/background.js | 4 +-- manifest.json | 2 +- options/options.html | 10 ++++++++ options/options.js | 26 +++++++++++++++++++- popup/popup.html | 48 +++++++++++++++++++----------------- popup/popup.js | 53 +++++++++++++++++++++++++++++++++++++++- 6 files changed, 115 insertions(+), 28 deletions(-) diff --git a/background/background.js b/background/background.js index 744c20e..6786baf 100644 --- a/background/background.js +++ b/background/background.js @@ -345,6 +345,6 @@ browser.contextMenus.create({ }); // Run code when the context menu is clicked -browser.contextMenus.onClicked.addListener( () => { - browser.windows.create({url: "/popup/popup.html", type: "popup"}); +browser.contextMenus.onClicked.addListener( (info) => { + browser.windows.create({url: `/popup/popup.html?url=${info.pageUrl}`, type: "popup"}); }); diff --git a/manifest.json b/manifest.json index 8e10005..4049eaf 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Chhoto URL", - "version": "1.2.0", + "version": "1.3.0", "description": "An unofficial extension for shortening URLs using the Chhoto URL API. Requires a Chhoto URL instance.", "icons": { "16": "icons/chhoto-url-16.png", diff --git a/options/options.html b/options/options.html index 79cf08c..a14861b 100644 --- a/options/options.html +++ b/options/options.html @@ -150,6 +150,16 @@

Inputted value is not a number.

+
+

Popup Settings

+ +
+ + diff --git a/options/options.js b/options/options.js index 9a7a239..104773d 100644 --- a/options/options.js +++ b/options/options.js @@ -40,6 +40,7 @@ const generateWithTitleEle = document.getElementById("generate-with-title"); const titleWordLimitLabelEle = document.getElementById("title-word-limit-label"); const titleWordLimitEle = document.getElementById("title-word-limit"); const message2Ele = document.getElementById("message2"); +const autoInsertPopupEle = document.getElementById("auto-insert-popup"); // Get the browser storage const browserStorage = browser.storage.local; @@ -164,7 +165,21 @@ titleWordLimitEle.oninput = (event) => { }; } -function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWithTitle, titleWordLimit }) { +// Automatically insert long URL into popup +autoInsertPopupEle.onclick = () => { + // Get browser storage + browserStorage.get("autoInsertPopup").then(({ autoInsertPopup }) => { + // Get value + autoInsertPopup = autoInsertPopupEle.checked; + + // Save value + browserStorage.set({ autoInsertPopup }); + }); +}; + + + +function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWithTitle, titleWordLimit, autoInsertPopup }) { hostKeyEle.value = chhotoHost || ""; apiKeyEle.value = chhotoKey || ""; @@ -191,16 +206,25 @@ function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWit browserStorage.set({ titleWordLimit: titleWordLimit }); } + // If autoInsertPopup is undefined, set the default value + if (autoInsertPopup === undefined) { + autoInsertPopup = false; + browserStorage.set({ autoInsertPopup: autoInsertPopup }); + } + // Initialize a list of protocols that are allowed if unset. This needs // to be synced with the initialization code in background.js#validateURL. allowedProtocols = new Set(allowedProtocols); + + // Update the checkboxes to display the correct value AllowHttpEle.checked = allowedProtocols.has("http:"); AllowHttpsEle.checked = allowedProtocols.has("https:"); AllowFileEle.checked = allowedProtocols.has("file:"); AllowFtpEle.checked = allowedProtocols.has("ftp:"); generateWithTitleEle.checked = generateWithTitle; + autoInsertPopupEle.checked = autoInsertPopup; // Set default display let display = "none"; diff --git a/popup/popup.html b/popup/popup.html index d7e041c..a2af4a7 100644 --- a/popup/popup.html +++ b/popup/popup.html @@ -34,7 +34,7 @@ cursor: pointer; } - #generate { + .generate-button { border: black 1px solid; border-radius: 7px; background-color: white; @@ -45,7 +45,7 @@ } #close:hover, - #generate:hover { + .generate-button:hover { background-color: #bbb; } @@ -61,7 +61,7 @@ } input { - min-width: 150px; + min-width: 250px; min-height: 17px; padding: 5px; border: 2px solid black; @@ -96,26 +96,28 @@ -
- -
-
- -
-
- -
+
+
+ +
+
+ +
+
+ +
+
diff --git a/popup/popup.js b/popup/popup.js index 38074d9..8def176 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -41,6 +41,56 @@ const generateEle = document.querySelector("#generate"); let shorturl; let longurl; +const requestParams = new URLSearchParams(window.location.search); +const requestValue = requestParams.get('url'); + +/** + * Automatically insert the long URL, if enabled + */ + +// If a URL was passed in the request +if (requestValue) { + browser.storage.local.get().then(( data ) => { + if (data.autoInsertPopup !== undefined && data.autoInsertPopup) { + let allowedProtocols; + // Initialize a list of protocols that are allowed if unset. + if (data.allowedProtocols === undefined) { + allowedProtocols = new Set(); + allowedProtocols.add("http:"); + allowedProtocols.add("https:"); + allowedProtocols.add("ftp:"); + allowedProtocols.add("file:"); + browser.storage.local.set({ allowedProtocols: Array(...allowedProtocols) }); + } else { + allowedProtocols = data.allowedProtocols; + allowedProtocols = new Set(allowedProtocols); + } + + // Try and catch structure + try { + // Define the URL + const url = new URL(requestValue); + + // Ensure the URL has a valid protocol + if (allowedProtocols.size > 0 && !allowedProtocols.has(url.protocol)) { + throw new Error("The URL is invalid"); + }; + + ////// If anything beyond this point is trigerred, the URL protocol is valid. ////// + + // Reassign the long url + longURLEle.value = url; + longurl = url; + } catch (error) { + console.log(`Error while auto inserting the long URL - ${error}`); + }; + + }; + + }); +}; + +// Close function async function close() { try { const windowId = (await browser.windows.getCurrent()).id; @@ -162,7 +212,8 @@ function sendRequest(page) { } // If the generate button was clicked -generateEle.addEventListener("click", () => { +generateEle.addEventListener("submit", (event) => { + event.preventDefault(); // Ensure both fields have been filled out, and the long URL is valid if ( shorturl !== undefined && longurl !== undefined && !message2Ele.classList.contains("warning") ) {