feat(browser-extension): improve popup script performance and adapt it to send events to the content script in order to avoid refreshing pages when enabling/disabling the extension
This commit is contained in:
parent
ca57b55514
commit
345cfc6f88
@ -4,11 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
const chromeUrl = 'https://chrome.google.com/webstore/detail/djcbfpkdhdkaflcigibkbpboflaplabg';
|
const chromeUrl = 'https://chrome.google.com/webstore/detail/djcbfpkdhdkaflcigibkbpboflaplabg';
|
||||||
|
|
||||||
/**
|
|
||||||
* @description Shortcut to send messages to background script
|
|
||||||
*/
|
|
||||||
const dispatch = chrome.runtime.sendMessage;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Edge Add-ons link
|
* @description Edge Add-ons link
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@ -48,9 +43,9 @@ const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Extension state
|
* @description Extension state
|
||||||
* @type {{ enabled: boolean }}
|
* @type {{ enabled: boolean, tabId: number | undefined }}
|
||||||
*/
|
*/
|
||||||
let state = { enabled: true };
|
let state = { enabled: true, tabId: undefined };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @async
|
* @async
|
||||||
@ -58,12 +53,14 @@ let state = { enabled: true };
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async function handleContentLoaded() {
|
async function handleContentLoaded() {
|
||||||
const tab = await dispatch({ type: 'GET_TAB' });
|
const tab = await chrome.runtime.sendMessage({ type: 'GET_TAB' });
|
||||||
|
|
||||||
hostname = tab?.url
|
hostname = tab?.url
|
||||||
? new URL(tab.url).hostname.split('.').slice(-3).join('.').replace('www.', '')
|
? new URL(tab.url).hostname.split('.').slice(-3).join('.').replace('www.', '')
|
||||||
: undefined;
|
: undefined;
|
||||||
state = (await dispatch({ hostname, type: 'GET_HOSTNAME_STATE' })) ?? state;
|
|
||||||
|
const next = await chrome.runtime.sendMessage({ hostname, type: 'GET_HOSTNAME_STATE' });
|
||||||
|
state = { ...(next ?? state), tabId: tab?.id };
|
||||||
|
|
||||||
const hostTextElement = document.getElementById('host');
|
const hostTextElement = document.getElementById('host');
|
||||||
hostTextElement.innerText = hostname ?? 'unknown';
|
hostTextElement.innerText = hostname ?? 'unknown';
|
||||||
@ -112,11 +109,13 @@ async function handleLinkRedirect(event) {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async function handlePowerToggle(event) {
|
async function handlePowerToggle(event) {
|
||||||
state = { enabled: !state.enabled };
|
const element = event.currentTarget;
|
||||||
dispatch({ hostname, state, type: 'SET_HOSTNAME_STATE' });
|
const next = { enabled: !state.enabled };
|
||||||
if (state.enabled) event.currentTarget.setAttribute('data-value', 'on');
|
|
||||||
else event.currentTarget.setAttribute('data-value', 'off');
|
chrome.runtime.sendMessage({ hostname, state: next, type: 'SET_HOSTNAME_STATE' });
|
||||||
await chrome.tabs.reload({ bypassCache: true });
|
chrome.tabs.sendMessage(state.tabId, { type: next.enabled ? 'RUN' : 'RESTORE' });
|
||||||
|
element.setAttribute('disabled', 'true');
|
||||||
|
element.setAttribute('data-value', next.enabled ? 'on' : 'off');
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user