Skip to content

Commit b1f0b45

Browse files
committed
Add a settings page.
Quick option to turn on and off the redirection
1 parent b1a987c commit b1f0b45

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

app/_locales/en/messages.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
"appDescription": {
1111
"message": "Always use WayBack machine (archive.org) for reddit links",
1212
"description": "The description of the application"
13+
},
14+
"actionTitle": {
15+
"message": "Reddit to WBM",
16+
"description": "Tooltip for browser action icon"
1317
}
1418
}

app/html/settings.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
</head>
6+
7+
<body>
8+
<div>
9+
<input type="checkbox" id="enableRWBM" name="enableRWBM" value="enabled" />
10+
<label for="enableRWBM">Enable redirect to WBM</label>
11+
</div>
12+
<script src="../scripts/settings.js"></script>
13+
</body>
14+
</html>

app/manifest.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "__MSG_appName__",
33
"short_name": "__MSG_appShortName__",
44
"description": "__MSG_appDescription__",
5-
"version": "1.0.0",
5+
"version": "2.0.0",
66
"manifest_version": 2,
77
"default_locale": "en",
88
"icons": {
@@ -11,13 +11,23 @@
1111
},
1212
"permissions": [
1313
"*://*.reddit.com/*",
14-
"https://web.archive.org/*",
14+
"*://web.archive.org/*",
15+
"storage",
1516
"webRequest",
1617
"webRequestBlocking"
1718
],
1819
"background": {
1920
"scripts": [
2021
"scripts/background.js"
2122
]
23+
},
24+
"page_action": {
25+
"default_title": "__MSG_actionTitle__",
26+
"default_popup": "html/settings.html",
27+
"default_icon": "images/icon-128.png",
28+
"show_matches": ["*://*.reddit.com/*", "https://web.archive.org/*"]
29+
},
30+
"options_ui": {
31+
"page": "html/settings.html"
2232
}
2333
}

app/scripts/background.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
browser.webRequest.onBeforeRequest.addListener(
2-
checkWBM,
3-
{ urls: ["*://*.reddit.com/*"], types: ["main_frame"] },
4-
["blocking"]
5-
);
6-
71
browser.webRequest.onBeforeRequest.addListener(
82
(details) => { return { upgradeToSecure: true } },
93
{ urls: ["http://*.archive.org/*"], types: ["main_frame"] },
104
["blocking"]
115
);
126

7+
browser.storage.local.get('enableRWBM').then((value) => {
8+
if (!value.enableRWBM) setRWBMEnabled(true);
9+
});
10+
11+
browser.storage.local.onChanged.addListener((changes) => {
12+
if(changes.enableRWBM) setRWBMEnabled(changes.enableRWBM.newValue);
13+
});
14+
15+
function setRWBMEnabled(enabled: boolean) {
16+
browser.webRequest.onBeforeRequest.removeListener(checkWBM);
17+
18+
if (enabled) {
19+
browser.webRequest.onBeforeRequest.addListener(
20+
checkWBM,
21+
{ urls: ["*://*.reddit.com/*"], types: ["main_frame"] },
22+
["blocking"]
23+
);
24+
}
25+
}
26+
1327
async function checkWBM(details: browser.webRequest._OnBeforeRequestDetails): Promise<browser.webRequest.BlockingResponse> {
1428
if (details.tabId === -1 || details.method !== "GET") {
1529
return {};

app/scripts/settings.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
(() => {
3+
const enableRWBM = document.getElementById('enableRWBM') as HTMLInputElement;
4+
if (!enableRWBM) return;
5+
enableRWBM.addEventListener('change', (event) => {
6+
browser.storage.local.set({
7+
enableRWBM: (event.target as HTMLInputElement).checked
8+
});
9+
});
10+
11+
browser.storage.local.get('enableRWBM').then((value) => {
12+
enableRWBM.checked = value.enableRWBM ?? true;
13+
});
14+
})();

0 commit comments

Comments
 (0)