79 lines
2 KiB
JavaScript
79 lines
2 KiB
JavaScript
'use strict';
|
|
if (window.browser === undefined) {
|
|
window.browser = window.chrome;
|
|
}
|
|
|
|
function errorLogger(action) {
|
|
function log(error) {
|
|
console.log(`Error: ${action}:`, error);
|
|
}
|
|
return log;
|
|
}
|
|
|
|
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 && settings.instance !== '') {
|
|
redirects.push(createRedirect(1, '||twitter.com', transform));
|
|
redirects.push(createRedirect(2, '||x.com', transform));
|
|
}
|
|
return 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');
|
|
|
|
enabled.checked = settings.enabled;
|
|
instance.value = settings.instance;
|
|
username.value = settings.username;
|
|
password.value = settings.password;
|
|
|
|
for (let element of document.getElementsByClassName('control')) {
|
|
element.addEventListener('change', () => {
|
|
settings.enabled = enabled.checked;
|
|
settings.instance = instance.value;
|
|
settings.username = username.value;
|
|
settings.password = password.value;
|
|
browser.storage.local.set(settings)
|
|
.then(
|
|
() => updateRedirects(settings),
|
|
errorLogger('browser.storage.local.set')
|
|
).catch(errorLogger('updateRedirects'));
|
|
});
|
|
}
|
|
}
|
|
|
|
browser.storage.local.get({
|
|
enabled: false,
|
|
instance: '',
|
|
username: '',
|
|
password: '',
|
|
}).then(setupSettingsForm, errorLogger('browser.storage.local.get'));
|