feat(browser-extension): add issue banner to popup giving user some information about what's wrong and where to find the issue easily

This commit is contained in:
wanhose 2024-10-05 17:20:32 +02:00
parent 69831ab07d
commit 07e03c8ff5
3 changed files with 199 additions and 129 deletions

View File

@ -29,6 +29,26 @@
</button>
</header>
<main>
<p aria-hidden="true" class="banner" id="issue-banner" role="alert">
<span id="issue-banner-text"></span>
<a aria-label="GitHub" id="issue-banner-url" target="_blank">
<svg
fill="none"
height="12"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
stroke="currentColor"
viewBox="0 0 24 24"
width="12"
>
<path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
<polyline points="15 3 21 3 21 9"></polyline>
<line x1="10" y1="14" x2="21" y2="3"></line>
</svg>
</a>
</p>
<div class="content">
<popup-button id="power-option" role="button" tabindex="0">
<svg
aria-hidden="true"
@ -46,7 +66,12 @@
</svg>
<span id="host"></span>
</popup-button>
<popup-button data-href="mailto:hello@wanhose.dev" id="help-option" role="link" tabindex="0">
<popup-button
data-href="mailto:hello@wanhose.dev"
id="help-option"
role="link"
tabindex="0"
>
<svg
aria-hidden="true"
fill="none"
@ -129,7 +154,9 @@
>
<polyline points="1 4 1 10 7 10"></polyline>
<polyline points="23 20 23 14 17 14"></polyline>
<path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15"></path>
<path
d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15"
></path>
</svg>
<svg
aria-hidden="true"
@ -152,6 +179,7 @@
<span id="extension-version"></span>
</popup-data>
</popup-data-container>
</div>
</main>
<footer></footer>
</body>

View File

@ -1,3 +1,15 @@
/**
* @typedef {Object} ExtensionState
* @property {boolean} on
* @property {ExtensionIssue} [issue]
*/
/**
* @typedef {Object} PopupState
* @extends {ExtensionState}
* @property {number} [tabId]
*/
if (typeof browser === 'undefined') {
browser = chrome;
}
@ -46,10 +58,10 @@ const isEdge = navigator.userAgent.indexOf('Edg') !== -1;
const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
/**
* @description Extension state
* @type {{ enabled: boolean, tabId: number | undefined }}
* @description Popup state
* @type {PopupState}
*/
let state = { enabled: true, tabId: undefined };
let state = { on: true };
/**
* @async
@ -63,9 +75,22 @@ async function handleContentLoaded() {
? new URL(tab.url).hostname.split('.').slice(-3).join('.').replace('www.', '')
: undefined;
const next = await browser.runtime.sendMessage({ hostname, type: 'GET_HOSTNAME_STATE' });
const next = await browser.runtime.sendMessage({ hostname, type: 'GET_STATE' });
state = { ...(next ?? state), tabId: tab?.id };
if (state.issue?.url) {
const issueBanner = document.getElementById('issue-banner');
issueBanner.removeAttribute('aria-hidden');
const issueBannerText = document.getElementById('issue-banner-text');
if (state.issue.flags.includes('wontfix'))
issueBannerText.innerText = browser.i18n.getMessage('popup_bannerIssueWontFix');
else issueBannerText.innerText = browser.i18n.getMessage('popup_bannerIssueOpen');
const issueBannerUrl = document.getElementById('issue-banner-url');
issueBannerUrl.setAttribute('href', state.issue.url);
}
const hostTextElement = document.getElementById('host');
hostTextElement.innerText = hostname ?? 'unknown';
@ -83,7 +108,7 @@ async function handleContentLoaded() {
const powerButtonElement = document.getElementById('power-option');
powerButtonElement?.addEventListener('click', handlePowerToggle);
if (state.enabled) powerButtonElement?.setAttribute('data-value', 'on');
if (state.on) powerButtonElement?.setAttribute('data-value', 'on');
else powerButtonElement?.setAttribute('data-value', 'off');
const rateButtonElement = document.getElementById('rate-option');
@ -150,9 +175,9 @@ async function handleLinkRedirect(event) {
* @returns {void}
*/
function handlePowerToggle() {
const next = { enabled: !state.enabled };
const next = { on: !state.on };
browser.runtime.sendMessage({ hostname, state: next, type: 'SET_HOSTNAME_STATE' });
browser.runtime.sendMessage({ hostname, state: next, type: 'UPDATE_STORE' });
browser.tabs.reload(state.tabId, { bypassCache: true });
window.close();
}

View File

@ -59,7 +59,24 @@ header {
padding: 0 16px;
}
main {
main > .banner {
background-color: #f39c12;
color: #c0392b;
margin: 0px;
padding: 16px;
&[aria-hidden='true'] {
display: none;
}
& #issue-banner-url {
color: inherit;
display: inline-block;
vertical-align: middle;
}
}
main > .content {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 16px;