8.0.4 #226
@ -1,6 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* @typedef {Object} ExtensionIssue
|
* @typedef {Object} ExtensionIssue
|
||||||
* @property {number} [expiresIn]
|
|
||||||
* @property {string[]} [flags]
|
* @property {string[]} [flags]
|
||||||
* @property {string} [url]
|
* @property {string} [url]
|
||||||
*/
|
*/
|
||||||
@ -9,7 +8,6 @@
|
|||||||
* @typedef {Object} ExtensionState
|
* @typedef {Object} ExtensionState
|
||||||
* @property {ExtensionIssue} [issue]
|
* @property {ExtensionIssue} [issue]
|
||||||
* @property {boolean} on
|
* @property {boolean} on
|
||||||
* @property {string} [updateAvailable]
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (typeof browser === 'undefined') {
|
if (typeof browser === 'undefined') {
|
||||||
@ -84,7 +82,7 @@ const script = browser.scripting;
|
|||||||
* @description Default value for extension state
|
* @description Default value for extension state
|
||||||
* @type {ExtensionState}
|
* @type {ExtensionState}
|
||||||
*/
|
*/
|
||||||
const stateByDefault = { issue: { expiresIn: 0 }, on: true };
|
const stateByDefault = { issue: undefined, on: true };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description The storage to use
|
* @description The storage to use
|
||||||
@ -152,14 +150,26 @@ async function getTab() {
|
|||||||
* @returns {Promise<ExtensionState>}
|
* @returns {Promise<ExtensionState>}
|
||||||
*/
|
*/
|
||||||
async function getState(hostname) {
|
async function getState(hostname) {
|
||||||
const keys = [hostname, 'updateAvailable'];
|
const { [hostname]: state = stateByDefault } = await storage.get(hostname);
|
||||||
const { [hostname]: state = stateByDefault, updateAvailable } = await storage.get(keys);
|
|
||||||
|
|
||||||
if ((state.issue && Date.now() > state.issue.expiresIn) || !state.issue?.expiresIn) {
|
state.issue = await refreshIssue(hostname);
|
||||||
state.issue = await refreshIssue(hostname);
|
|
||||||
|
return { ...stateByDefault, ...state };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @async
|
||||||
|
* @description Get latest version available for this extension
|
||||||
|
* @returns {Promise<string>}
|
||||||
|
*/
|
||||||
|
async function getLatestVersion() {
|
||||||
|
try {
|
||||||
|
const { data } = await requestManager.fetch(`${apiUrl}/version/`);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
} catch {
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...stateByDefault, ...state, updateAvailable };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -193,20 +203,7 @@ async function refreshIssue(hostname, attempt = 1) {
|
|||||||
if (attempt <= 3) {
|
if (attempt <= 3) {
|
||||||
try {
|
try {
|
||||||
const { data = {} } = await requestManager.fetch(`${apiUrl}/issues/${hostname}/`);
|
const { data = {} } = await requestManager.fetch(`${apiUrl}/issues/${hostname}/`);
|
||||||
|
await updateStore(hostname, { issue: { flags: data.flags, url: data.url } });
|
||||||
if (Object.keys(data).length === 0) {
|
|
||||||
await updateStore(hostname, { issue: { expiresIn: Date.now() + 8 * 60 * 60 * 1000 } });
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const issue = {
|
|
||||||
expiresIn: Date.now() + 4 * 60 * 60 * 1000,
|
|
||||||
flags: data.flags,
|
|
||||||
url: data.url,
|
|
||||||
};
|
|
||||||
|
|
||||||
await updateStore(hostname, { issue });
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch {
|
} catch {
|
||||||
@ -301,6 +298,9 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
|
|||||||
callback(exclusionList);
|
callback(exclusionList);
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
case 'GET_LATEST_VERSION':
|
||||||
|
getLatestVersion().then(callback);
|
||||||
|
return true;
|
||||||
case 'GET_STATE':
|
case 'GET_STATE':
|
||||||
if (hostname) {
|
if (hostname) {
|
||||||
getState(hostname).then(callback);
|
getState(hostname).then(callback);
|
||||||
@ -366,15 +366,9 @@ browser.runtime.onInstalled.addListener((details) => {
|
|||||||
suppressLastError
|
suppressLastError
|
||||||
);
|
);
|
||||||
|
|
||||||
if (details.reason === 'update') refreshData();
|
if (details.reason === 'update') {
|
||||||
storage.remove('updateAvailable');
|
refreshData();
|
||||||
});
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @description Listen to available updates
|
|
||||||
*/
|
|
||||||
browser.runtime.onUpdateAvailable.addListener((details) => {
|
|
||||||
storage.set({ updateAvailable: details.version }, suppressLastError);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
* @typedef {Object} ExtensionState
|
* @typedef {Object} ExtensionState
|
||||||
* @property {ExtensionIssue} [issue]
|
* @property {ExtensionIssue} [issue]
|
||||||
* @property {boolean} on
|
* @property {boolean} on
|
||||||
* @property {string} [updateAvailable]
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,6 +122,17 @@ async function handleContentLoaded() {
|
|||||||
await updateDatabaseVersion();
|
await updateDatabaseVersion();
|
||||||
|
|
||||||
const { exclusions } = (await dispatch({ hostname, type: 'GET_DATA' })) ?? {};
|
const { exclusions } = (await dispatch({ hostname, type: 'GET_DATA' })) ?? {};
|
||||||
|
const currentVersion = browser.runtime.getManifest().version;
|
||||||
|
const latestVersion = await dispatch({ type: 'GET_LATEST_VERSION' });
|
||||||
|
const updateAvailable = currentVersion !== latestVersion;
|
||||||
|
|
||||||
|
if (updateAvailable) {
|
||||||
|
const updateBanner = document.getElementById('update-banner');
|
||||||
|
updateBanner.removeAttribute('aria-hidden');
|
||||||
|
|
||||||
|
const updateBannerUrl = document.getElementById('update-banner-url');
|
||||||
|
updateBannerUrl.href += `/tag/${latestVersion}`;
|
||||||
|
}
|
||||||
|
|
||||||
if (exclusions?.domains.some((x) => url.hostname.match(x.replaceAll(/\*/g, '[^ ]*')))) {
|
if (exclusions?.domains.some((x) => url.hostname.match(x.replaceAll(/\*/g, '[^ ]*')))) {
|
||||||
const supportBanner = document.getElementById('support-banner');
|
const supportBanner = document.getElementById('support-banner');
|
||||||
@ -130,6 +140,12 @@ async function handleContentLoaded() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const powerButtonElement = document.getElementById('power-option');
|
||||||
|
powerButtonElement?.addEventListener('click', handlePowerToggle);
|
||||||
|
powerButtonElement?.removeAttribute('disabled');
|
||||||
|
if (state.on) powerButtonElement?.setAttribute('data-value', 'on');
|
||||||
|
else powerButtonElement?.setAttribute('data-value', 'off');
|
||||||
|
|
||||||
if (state.issue?.url) {
|
if (state.issue?.url) {
|
||||||
const issueBanner = document.getElementById('issue-banner');
|
const issueBanner = document.getElementById('issue-banner');
|
||||||
issueBanner.removeAttribute('aria-hidden');
|
issueBanner.removeAttribute('aria-hidden');
|
||||||
@ -144,24 +160,9 @@ async function handleContentLoaded() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.updateAvailable) {
|
|
||||||
const updateBanner = document.getElementById('update-banner');
|
|
||||||
updateBanner.removeAttribute('aria-hidden');
|
|
||||||
|
|
||||||
const updateBannerUrl = document.getElementById('update-banner-url');
|
|
||||||
updateBannerUrl.href += `/tag/${state.updateAvailable}`;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cancelButtonElement = document.getElementsByClassName('report-cancel-button')[0];
|
const cancelButtonElement = document.getElementsByClassName('report-cancel-button')[0];
|
||||||
cancelButtonElement?.addEventListener('click', handleCancelClick);
|
cancelButtonElement?.addEventListener('click', handleCancelClick);
|
||||||
|
|
||||||
const powerButtonElement = document.getElementById('power-option');
|
|
||||||
powerButtonElement?.addEventListener('click', handlePowerToggle);
|
|
||||||
powerButtonElement?.removeAttribute('disabled');
|
|
||||||
if (state.on) powerButtonElement?.setAttribute('data-value', 'on');
|
|
||||||
else powerButtonElement?.setAttribute('data-value', 'off');
|
|
||||||
|
|
||||||
const reasonInputElement = document.getElementById('report-input-reason');
|
const reasonInputElement = document.getElementById('report-input-reason');
|
||||||
reasonInputElement?.addEventListener('input', handleInputChange);
|
reasonInputElement?.addEventListener('input', handleInputChange);
|
||||||
reasonInputElement?.addEventListener('keydown', handleInputKeyDown);
|
reasonInputElement?.addEventListener('keydown', handleInputKeyDown);
|
||||||
@ -250,7 +251,7 @@ async function handleLinkRedirect(event) {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
async function handlePowerToggle() {
|
async function handlePowerToggle() {
|
||||||
const next = { on: !state.on };
|
const next = { ...state, on: !state.on };
|
||||||
|
|
||||||
await dispatch({ hostname, state: next, type: 'UPDATE_STORE' });
|
await dispatch({ hostname, state: next, type: 'UPDATE_STORE' });
|
||||||
await browser.tabs.reload(state.tabId, { bypassCache: true });
|
await browser.tabs.reload(state.tabId, { bypassCache: true });
|
||||||
|
Loading…
Reference in New Issue
Block a user