Revamp popup settings

This commit is contained in:
Solninja A 2025-01-27 17:26:25 +10:00
parent f38086593b
commit 981f6da829
6 changed files with 115 additions and 28 deletions

View File

@ -345,6 +345,6 @@ browser.contextMenus.create({
}); });
// Run code when the context menu is clicked // Run code when the context menu is clicked
browser.contextMenus.onClicked.addListener( () => { browser.contextMenus.onClicked.addListener( (info) => {
browser.windows.create({url: "/popup/popup.html", type: "popup"}); browser.windows.create({url: `/popup/popup.html?url=${info.pageUrl}`, type: "popup"});
}); });

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Chhoto URL", "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.", "description": "An unofficial extension for shortening URLs using the Chhoto URL API. Requires a Chhoto URL instance.",
"icons": { "icons": {
"16": "icons/chhoto-url-16.png", "16": "icons/chhoto-url-16.png",

View File

@ -150,6 +150,16 @@
<p id="message2" class="note">Inputted value is not a number.</p> <p id="message2" class="note">Inputted value is not a number.</p>
</div> </div>
<div class="settings checkbox">
<h2>Popup Settings</h2>
<label>
Automatically insert long URL into the popup
<input type="checkbox" id="auto-insert-popup">
<p class="note">This will only autofill the long URL if the page's protocol is supported (see the allowed protocols setting).</p>
</label>
</div>
<script type="application/javascript" src="/lib/browser-polyfill.min.js"></script> <script type="application/javascript" src="/lib/browser-polyfill.min.js"></script>
<script type="application/javascript" src="/options/options.js"></script> <script type="application/javascript" src="/options/options.js"></script>
</body> </body>

View File

@ -40,6 +40,7 @@ const generateWithTitleEle = document.getElementById("generate-with-title");
const titleWordLimitLabelEle = document.getElementById("title-word-limit-label"); const titleWordLimitLabelEle = document.getElementById("title-word-limit-label");
const titleWordLimitEle = document.getElementById("title-word-limit"); const titleWordLimitEle = document.getElementById("title-word-limit");
const message2Ele = document.getElementById("message2"); const message2Ele = document.getElementById("message2");
const autoInsertPopupEle = document.getElementById("auto-insert-popup");
// Get the browser storage // Get the browser storage
const browserStorage = browser.storage.local; 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 || ""; hostKeyEle.value = chhotoHost || "";
apiKeyEle.value = chhotoKey || ""; apiKeyEle.value = chhotoKey || "";
@ -191,16 +206,25 @@ function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWit
browserStorage.set({ titleWordLimit: titleWordLimit }); 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 // Initialize a list of protocols that are allowed if unset. This needs
// to be synced with the initialization code in background.js#validateURL. // to be synced with the initialization code in background.js#validateURL.
allowedProtocols = new Set(allowedProtocols); allowedProtocols = new Set(allowedProtocols);
// Update the checkboxes to display the correct value
AllowHttpEle.checked = allowedProtocols.has("http:"); AllowHttpEle.checked = allowedProtocols.has("http:");
AllowHttpsEle.checked = allowedProtocols.has("https:"); AllowHttpsEle.checked = allowedProtocols.has("https:");
AllowFileEle.checked = allowedProtocols.has("file:"); AllowFileEle.checked = allowedProtocols.has("file:");
AllowFtpEle.checked = allowedProtocols.has("ftp:"); AllowFtpEle.checked = allowedProtocols.has("ftp:");
generateWithTitleEle.checked = generateWithTitle; generateWithTitleEle.checked = generateWithTitle;
autoInsertPopupEle.checked = autoInsertPopup;
// Set default display // Set default display
let display = "none"; let display = "none";

View File

@ -34,7 +34,7 @@
cursor: pointer; cursor: pointer;
} }
#generate { .generate-button {
border: black 1px solid; border: black 1px solid;
border-radius: 7px; border-radius: 7px;
background-color: white; background-color: white;
@ -45,7 +45,7 @@
} }
#close:hover, #close:hover,
#generate:hover { .generate-button:hover {
background-color: #bbb; background-color: #bbb;
} }
@ -61,7 +61,7 @@
} }
input { input {
min-width: 150px; min-width: 250px;
min-height: 17px; min-height: 17px;
padding: 5px; padding: 5px;
border: 2px solid black; border: 2px solid black;
@ -96,10 +96,11 @@
<button id="close" type="button">&#10006; Close</button> <button id="close" type="button">&#10006; Close</button>
<form id="generate">
<div class="generate"> <div class="generate">
<label> <label>
Short URL Short URL
<input required type="text" id="shorturl" placeholder="interesting-short-url"> <input autofocus required type="text" id="shorturl" placeholder="interesting-short-url">
<p id="message"></p> <p id="message"></p>
</label> </label>
</div> </div>
@ -112,10 +113,11 @@
</div> </div>
<div class="generate"> <div class="generate">
<label> <label>
<input id="generate" type="button" value="Shorten!"> <input class="generate-button" type="submit" value="Shorten!">
<p id="message3">Error: Required fields are missing.</p> <p id="message3">Error: Required fields are missing.</p>
</label> </label>
</div> </div>
</form>
<script type="application/javascript" src="/lib/browser-polyfill.min.js"></script> <script type="application/javascript" src="/lib/browser-polyfill.min.js"></script>

View File

@ -41,6 +41,56 @@ const generateEle = document.querySelector("#generate");
let shorturl; let shorturl;
let longurl; 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() { async function close() {
try { try {
const windowId = (await browser.windows.getCurrent()).id; const windowId = (await browser.windows.getCurrent()).id;
@ -162,7 +212,8 @@ function sendRequest(page) {
} }
// If the generate button was clicked // 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 // Ensure both fields have been filled out, and the long URL is valid
if ( shorturl !== undefined && longurl !== undefined && !message2Ele.classList.contains("warning") ) { if ( shorturl !== undefined && longurl !== undefined && !message2Ele.classList.contains("warning") ) {