commit 881b5941a98a223ec00dbd291e07e63bba888497 Author: Juhani Krekelä Date: Sun Jul 14 22:17:43 2024 +0300 First commit diff --git a/dialog.html b/dialog.html new file mode 100644 index 0000000..07ed8e6 --- /dev/null +++ b/dialog.html @@ -0,0 +1,25 @@ + + + + + + +
+
+ Redirect configuration +
+
+ +
+
+ Authentication +
+
+
+
+
+ + + + + diff --git a/dialog.js b/dialog.js new file mode 100644 index 0000000..a5299a3 --- /dev/null +++ b/dialog.js @@ -0,0 +1,81 @@ +'use strict'; +if (window.browser === undefined) { + window.browser = window.chrome; +} + +function createRedirect(id, from, transform) { + return { + id: id, + action: { + type: "redirect", + redirect: { transform: transform }, + }, + condition: { + urlFilter: from, + resourceTypes: ["main_frame", "sub_frame"], + }, + }; +} + +function updateRedirects(settings) { + const transform = { + host: settings.instance, + }; + if (settings.username !== '') { + transform.username = settings.username; + } + if (settings.password !== '') { + transform.password = settings.password; + } + const redirects = []; + if (settings.enabled) { + redirects.push(createRedirect(1, '||twitter.com', transform)); + redirects.push(createRedirect(2, '||x.com', transform)); + } + browser.declarativeNetRequest.updateDynamicRules({ + addRules: redirects, + removeRuleIds: [1, 2], + }); +} + +function setupSettingsForm(settings) { + const enabled = document.getElementById('enabled'); + const instance = document.getElementById('instance'); + const username = document.getElementById('username'); + const password = document.getElementById('password'); + const submit = document.getElementById('submit'); + + enabled.checked = settings.enabled; + instance.value = settings.instance; + username.value = settings.username; + password.value = settings.password; + + document.getElementById('form').addEventListener('submit', (event) => { + event.preventDefault(); + settings.enabled = enabled.checked; + settings.instance = instance.value; + settings.username = username.value; + settings.password = password.value; + submit.disabled = true; + browser.storage.local.set(settings); + updateRedirects(settings); + }); + + for (let element of document.getElementsByClassName('control')) { + element.addEventListener('input', () => { + const different = + enabled.checked !== settings.enabled || + instance.value !== settings.instance || + username.value !== settings.username || + password.value !== settings.password; + submit.disabled = !different; + }); + } +} + +browser.storage.local.get({ + enabled: false, + instance: '', + username: '', + password: '', +}).then(setupSettingsForm); diff --git a/icon-16.png b/icon-16.png new file mode 100644 index 0000000..a17113d Binary files /dev/null and b/icon-16.png differ diff --git a/icon-32.png b/icon-32.png new file mode 100644 index 0000000..773d41a Binary files /dev/null and b/icon-32.png differ diff --git a/icon-64.png b/icon-64.png new file mode 100644 index 0000000..920fea1 Binary files /dev/null and b/icon-64.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..75b2f5b --- /dev/null +++ b/manifest.json @@ -0,0 +1,29 @@ +{ + "manifest_version": 3, + "name": "Private Nitter redirector", + "icons": { + "16": "icon-16.png", + "32": "icon-32.png", + "64": "icon-64.png" + }, + "description": "Redirect to and authenticate with a private Nitter instance", + "version": "1.0", + "permissions": [ + "declarativeNetRequestWithHostAccess", + "storage" + ], + "host_permissions": [ + "*://twitter.com/*", + "*://www.twitter.com/*", + "*://mobile.twitter.com/*", + "*://x.com/*" + ], + "action": { + "default_icon": { + "16": "icon-16.png", + "32": "icon-32.png", + "64": "icon-64.png" + }, + "default_popup": "dialog.html" + } +}