libredirect/pages/popup/popup.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-10-07 13:59:31 +02:00
'use strict';
2019-10-30 06:48:38 +01:00
let nitterInstance = document.querySelector('#nitterInstance');
let invidiousInstance = document.querySelector('#invidiousInstance');
2020-02-01 04:17:51 +01:00
let bibliogramInstance = document.querySelector('#bibliogramInstance');
let disableNitter = document.querySelector('#disableNitter');
let disableInvidious = document.querySelector('#disableInvidious');
let disableBibliogram = document.querySelector('#disableBibliogram');
let version = document.querySelector('#version');
2019-10-07 13:59:31 +02:00
chrome.storage.sync.get(
2020-02-01 04:17:51 +01:00
[
'nitterInstance',
'invidiousInstance',
'bibliogramInstance',
'disableNitter',
'disableInvidious',
'disableBibliogram'
],
2019-10-30 06:48:38 +01:00
result => {
nitterInstance.value = result.nitterInstance || '';
invidiousInstance.value = result.invidiousInstance || '';
2020-02-01 04:17:51 +01:00
bibliogramInstance.value = result.bibliogramInstance || '';
disableNitter.checked = !result.disableNitter;
disableInvidious.checked = !result.disableInvidious;
disableBibliogram.checked = !result.disableBibliogram;
2019-10-07 13:59:31 +02:00
}
);
version.textContent = chrome.runtime.getManifest().version;
2019-10-30 06:48:38 +01:00
function debounce(func, wait, immediate) {
let timeout;
2020-01-14 10:48:37 +01:00
return () => {
2019-10-30 06:48:38 +01:00
let context = this, args = arguments;
2020-01-14 10:48:37 +01:00
let later = () => {
2019-10-30 06:48:38 +01:00
timeout = null;
if (!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
let nitterInstanceChange = debounce(() => {
if (nitterInstance.checkValidity()) {
chrome.storage.sync.set({
nitterInstance: nitterInstance.value ? new URL(nitterInstance.value).origin : ''
});
}
2019-10-30 06:48:38 +01:00
}, 500);
nitterInstance.addEventListener('input', nitterInstanceChange);
let invidiousInstanceChange = debounce(() => {
if (invidiousInstance.checkValidity()) {
chrome.storage.sync.set({
invidiousInstance: invidiousInstance.value ? new URL(invidiousInstance.value).origin : ''
});
}
2019-10-30 06:48:38 +01:00
}, 500);
invidiousInstance.addEventListener('input', invidiousInstanceChange);
2020-02-01 04:17:51 +01:00
let bibliogramInstanceChange = debounce(() => {
if (bibliogramInstance.checkValidity()) {
chrome.storage.sync.set({
bibliogramInstance: bibliogramInstance.value ? new URL(bibliogramInstance.value).origin : ''
});
}
2020-02-01 04:17:51 +01:00
}, 500);
bibliogramInstance.addEventListener('input', bibliogramInstanceChange);
2019-10-30 06:48:38 +01:00
disableNitter.addEventListener('change', event => {
2019-10-07 13:59:31 +02:00
chrome.storage.sync.set({ disableNitter: !event.target.checked });
});
2019-10-30 06:48:38 +01:00
disableInvidious.addEventListener('change', event => {
2019-10-07 13:59:31 +02:00
chrome.storage.sync.set({ disableInvidious: !event.target.checked });
});
2020-02-01 04:17:51 +01:00
disableBibliogram.addEventListener('change', event => {
chrome.storage.sync.set({ disableBibliogram: !event.target.checked });
});