Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const rollup = {
targets: [
{src: './source/manifest.json', dest: 'distribution'},
{src: './source/*.+(html|png)', dest: 'distribution/assets'},
{src: './source/background-loader.js', dest: 'distribution/assets'},
{src: './source/options-preflight.js', dest: 'distribution/assets'},
],
}),
Expand Down
19 changes: 19 additions & 0 deletions source/background-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function storeBackgroundLoadError(error) {
localStorage.backgroundLoadErrors ??= '';
localStorage.backgroundLoadErrors += `${error?.stack ?? error?.message ?? String(error)}\n\n`;
}

function backgroundLoadErrorListener(event) {
storeBackgroundLoadError(event.error ?? event.message);
}

globalThis.addEventListener('error', backgroundLoadErrorListener);

try {
// eslint-disable-next-line import-x/extensions -- The loader is copied to `distribution/assets`, where the built file is `background.js`.
await import('./background.js');
globalThis.removeEventListener('error', backgroundLoadErrorListener);
} catch (error) {
storeBackgroundLoadError(error);
throw error;
}
2 changes: 2 additions & 0 deletions source/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,5 @@ chrome.runtime.onInstalled.addListener(async () => {
// Call after the reset above just in case we nuked Safari's base permissions
await showWelcomePage();
});

delete localStorage.backgroundLoadErrors;
4 changes: 2 additions & 2 deletions source/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
},
"options_page": "assets/options.html",
"background": {
"service_worker": "assets/background.js",
"service_worker": "assets/background-loader.js",
"type": "module",
"scripts": [
"assets/background.js"
"assets/background-loader.js"
]
},
"content_scripts": [
Expand Down
7 changes: 7 additions & 0 deletions source/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,10 @@ summary:hover {
border: 1px solid var(--rgh-red);
background: color-mix(in srgb, var(--rgh-red) 10%, transparent);
}

.js-background-fail-error {
overflow: auto;
white-space: pre-wrap;
margin: 1em 0 0;
font-size: 12px;
}
9 changes: 6 additions & 3 deletions source/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
<!-- Captures and ignores native enter-to-submit action -->
<button hidden>Capture Submit</button>

<p hidden class="js-background-fail-banner">
It seems that the background page failed to load. This breaks some features. Please <a href="https://github.com/refined-github/refined-github/issues/new?template=1_bug_report.yml">report it</a>.
</p>
<div hidden class="js-background-fail-banner">
<p>
It seems that the background page failed to load. This breaks some features. Please <a href="https://github.com/refined-github/refined-github/issues/new?template=1_bug_report.yml">report it</a>.
</p>
<pre hidden class="js-background-fail-error"></pre>
</div>

<details id="token">
<summary><strong>🔑 Personal token</strong></summary>
Expand Down
16 changes: 10 additions & 6 deletions source/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {isChrome, isFirefox} from 'webext-detect';
import type {SyncedForm} from 'webext-options-sync-per-domain';
import 'webext-bugs/target-blank';

import {messageRuntime} from 'webext-msg';

import {startFeatureIdentification} from './helpers/bisect.js';
import clearCacheHandler from './helpers/clear-cache-handler.js';
import {doesBrowserActionOpenOptions} from './helpers/feature-utils.js';
Expand Down Expand Up @@ -104,10 +102,16 @@ async function fetchHotfixes(event: MouseEvent): Promise<void> {
}
}

async function validateBackgroundPage(): Promise<void> {
if (await messageRuntime({ping: true}) !== 'pong') {
$('.js-background-fail-banner').hidden = false;
function validateBackgroundPage(): void {
const backgroundLoadErrors = localStorage.backgroundLoadErrors?.trim();
if (!backgroundLoadErrors) {
return;
}

const errorField = $('.js-background-fail-error');
errorField.textContent = backgroundLoadErrors;
errorField.hidden = false;
$('.js-background-fail-banner').hidden = false;
}

async function generateDom(): Promise<void> {
Expand Down Expand Up @@ -138,7 +142,7 @@ async function generateDom(): Promise<void> {
// Show stored CSS hotfixes
void showStoredCssHotfixes();

void validateBackgroundPage();
validateBackgroundPage();
}

function addEventListeners(): void {
Expand Down
Loading