Skip to content

Commit 981f6da

Browse files
committed
Revamp popup settings
1 parent f380865 commit 981f6da

File tree

6 files changed

+115
-28
lines changed

6 files changed

+115
-28
lines changed

background/background.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,6 @@ browser.contextMenus.create({
345345
});
346346

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

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "Chhoto URL",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"description": "An unofficial extension for shortening URLs using the Chhoto URL API. Requires a Chhoto URL instance.",
66
"icons": {
77
"16": "icons/chhoto-url-16.png",

options/options.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ <h2>URL Generation</h2>
150150
<p id="message2" class="note">Inputted value is not a number.</p>
151151
</div>
152152

153+
<div class="settings checkbox">
154+
<h2>Popup Settings</h2>
155+
<label>
156+
Automatically insert long URL into the popup
157+
<input type="checkbox" id="auto-insert-popup">
158+
<p class="note">This will only autofill the long URL if the page's protocol is supported (see the allowed protocols setting).</p>
159+
</label>
160+
</div>
161+
162+
153163
<script type="application/javascript" src="/lib/browser-polyfill.min.js"></script>
154164
<script type="application/javascript" src="/options/options.js"></script>
155165
</body>

options/options.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const generateWithTitleEle = document.getElementById("generate-with-title");
4040
const titleWordLimitLabelEle = document.getElementById("title-word-limit-label");
4141
const titleWordLimitEle = document.getElementById("title-word-limit");
4242
const message2Ele = document.getElementById("message2");
43+
const autoInsertPopupEle = document.getElementById("auto-insert-popup");
4344

4445
// Get the browser storage
4546
const browserStorage = browser.storage.local;
@@ -164,7 +165,21 @@ titleWordLimitEle.oninput = (event) => {
164165
};
165166
}
166167

167-
function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWithTitle, titleWordLimit }) {
168+
// Automatically insert long URL into popup
169+
autoInsertPopupEle.onclick = () => {
170+
// Get browser storage
171+
browserStorage.get("autoInsertPopup").then(({ autoInsertPopup }) => {
172+
// Get value
173+
autoInsertPopup = autoInsertPopupEle.checked;
174+
175+
// Save value
176+
browserStorage.set({ autoInsertPopup });
177+
});
178+
};
179+
180+
181+
182+
function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWithTitle, titleWordLimit, autoInsertPopup }) {
168183
hostKeyEle.value = chhotoHost || "";
169184
apiKeyEle.value = chhotoKey || "";
170185

@@ -191,16 +206,25 @@ function setCurrentChoice({ chhotoHost, chhotoKey, allowedProtocols, generateWit
191206
browserStorage.set({ titleWordLimit: titleWordLimit });
192207
}
193208

209+
// If autoInsertPopup is undefined, set the default value
210+
if (autoInsertPopup === undefined) {
211+
autoInsertPopup = false;
212+
browserStorage.set({ autoInsertPopup: autoInsertPopup });
213+
}
214+
194215
// Initialize a list of protocols that are allowed if unset. This needs
195216
// to be synced with the initialization code in background.js#validateURL.
196217
allowedProtocols = new Set(allowedProtocols);
197218

219+
220+
// Update the checkboxes to display the correct value
198221
AllowHttpEle.checked = allowedProtocols.has("http:");
199222
AllowHttpsEle.checked = allowedProtocols.has("https:");
200223
AllowFileEle.checked = allowedProtocols.has("file:");
201224
AllowFtpEle.checked = allowedProtocols.has("ftp:");
202225

203226
generateWithTitleEle.checked = generateWithTitle;
227+
autoInsertPopupEle.checked = autoInsertPopup;
204228

205229
// Set default display
206230
let display = "none";

popup/popup.html

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
cursor: pointer;
3535
}
3636

37-
#generate {
37+
.generate-button {
3838
border: black 1px solid;
3939
border-radius: 7px;
4040
background-color: white;
@@ -45,7 +45,7 @@
4545
}
4646

4747
#close:hover,
48-
#generate:hover {
48+
.generate-button:hover {
4949
background-color: #bbb;
5050
}
5151

@@ -61,7 +61,7 @@
6161
}
6262

6363
input {
64-
min-width: 150px;
64+
min-width: 250px;
6565
min-height: 17px;
6666
padding: 5px;
6767
border: 2px solid black;
@@ -96,26 +96,28 @@ <h1>Generate a Chhoto URL</h1>
9696

9797
<button id="close" type="button">&#10006; Close</button>
9898

99-
<div class="generate">
100-
<label>
101-
Short URL
102-
<input required type="text" id="shorturl" placeholder="interesting-short-url">
103-
<p id="message"></p>
104-
</label>
105-
</div>
106-
<div class="generate">
107-
<label>
108-
Long URL
109-
<input required type="text" id="longurl" placeholder="https://example.com">
110-
<p id="message2">The provided URL is invalid.</p>
111-
</label>
112-
</div>
113-
<div class="generate">
114-
<label>
115-
<input id="generate" type="button" value="Shorten!">
116-
<p id="message3">Error: Required fields are missing.</p>
117-
</label>
118-
</div>
99+
<form id="generate">
100+
<div class="generate">
101+
<label>
102+
Short URL
103+
<input autofocus required type="text" id="shorturl" placeholder="interesting-short-url">
104+
<p id="message"></p>
105+
</label>
106+
</div>
107+
<div class="generate">
108+
<label>
109+
Long URL
110+
<input required type="text" id="longurl" placeholder="https://example.com">
111+
<p id="message2">The provided URL is invalid.</p>
112+
</label>
113+
</div>
114+
<div class="generate">
115+
<label>
116+
<input class="generate-button" type="submit" value="Shorten!">
117+
<p id="message3">Error: Required fields are missing.</p>
118+
</label>
119+
</div>
120+
</form>
119121

120122

121123
<script type="application/javascript" src="/lib/browser-polyfill.min.js"></script>

popup/popup.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,56 @@ const generateEle = document.querySelector("#generate");
4141
let shorturl;
4242
let longurl;
4343

44+
const requestParams = new URLSearchParams(window.location.search);
45+
const requestValue = requestParams.get('url');
46+
47+
/**
48+
* Automatically insert the long URL, if enabled
49+
*/
50+
51+
// If a URL was passed in the request
52+
if (requestValue) {
53+
browser.storage.local.get().then(( data ) => {
54+
if (data.autoInsertPopup !== undefined && data.autoInsertPopup) {
55+
let allowedProtocols;
56+
// Initialize a list of protocols that are allowed if unset.
57+
if (data.allowedProtocols === undefined) {
58+
allowedProtocols = new Set();
59+
allowedProtocols.add("http:");
60+
allowedProtocols.add("https:");
61+
allowedProtocols.add("ftp:");
62+
allowedProtocols.add("file:");
63+
browser.storage.local.set({ allowedProtocols: Array(...allowedProtocols) });
64+
} else {
65+
allowedProtocols = data.allowedProtocols;
66+
allowedProtocols = new Set(allowedProtocols);
67+
}
68+
69+
// Try and catch structure
70+
try {
71+
// Define the URL
72+
const url = new URL(requestValue);
73+
74+
// Ensure the URL has a valid protocol
75+
if (allowedProtocols.size > 0 && !allowedProtocols.has(url.protocol)) {
76+
throw new Error("The URL is invalid");
77+
};
78+
79+
////// If anything beyond this point is trigerred, the URL protocol is valid. //////
80+
81+
// Reassign the long url
82+
longURLEle.value = url;
83+
longurl = url;
84+
} catch (error) {
85+
console.log(`Error while auto inserting the long URL - ${error}`);
86+
};
87+
88+
};
89+
90+
});
91+
};
92+
93+
// Close function
4494
async function close() {
4595
try {
4696
const windowId = (await browser.windows.getCurrent()).id;
@@ -162,7 +212,8 @@ function sendRequest(page) {
162212
}
163213

164214
// If the generate button was clicked
165-
generateEle.addEventListener("click", () => {
215+
generateEle.addEventListener("submit", (event) => {
216+
event.preventDefault();
166217
// Ensure both fields have been filled out, and the long URL is valid
167218
if ( shorturl !== undefined && longurl !== undefined && !message2Ele.classList.contains("warning") ) {
168219

0 commit comments

Comments
 (0)