Skip to content

Commit

Permalink
Handle hotfix loading from GitHub Pages (#8143)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Dec 10, 2024
1 parent 62f91b2 commit 260c72e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions source/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import optionsStorage, {hasToken} from './options-storage.js';
import isDevelopmentVersion from './helpers/is-development-version.js';
import {doesBrowserActionOpenOptions} from './helpers/feature-utils.js';
import {styleHotfixes} from './helpers/hotfix.js';
import {fetchText} from './helpers/isomorphic-fetch.js';

const {version} = chrome.runtime.getManifest();

Expand All @@ -34,6 +35,7 @@ handleMessages({
async closeTab(_: any, {tab}: chrome.runtime.MessageSender) {
void chrome.tabs.remove(tab!.id!);
},
fetchText,
async fetchJSON(url: string) {
const response = await fetch(url);
return response.json();
Expand Down
6 changes: 2 additions & 4 deletions source/helpers/hotfix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {any as concatenateTemplateLiteralTag} from 'code-tag';

import type {RGHOptions} from '../options-storage.js';
import isDevelopmentVersion from './is-development-version.js';
import {isomorphicFetchText} from './isomorphic-fetch.js';

const {version: currentVersion} = chrome.runtime.getManifest();

Expand All @@ -23,12 +24,9 @@ function parseCsv(content: string): string[][] {

async function fetchHotfix(path: string): Promise<string> {
// Use GitHub Pages host because the API is rate-limited
const request = await fetch(`https://refined-github.github.io/yolo/${path}`, {
return isomorphicFetchText(`https://refined-github.github.io/yolo/${path}`, {
cache: 'no-store', // Disable caching altogether
});

const contents = await request.text();
return contents.trim();
}

type HotfixStorage = Array<[FeatureID, string, string]>;
Expand Down
16 changes: 16 additions & 0 deletions source/helpers/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {isWebPage} from 'webext-detect';
import {messageRuntime} from 'webext-msg';

export async function fetchText(url: string, options: RequestInit): Promise<string> {
const response = await fetch(url, options);
return response.ok
? response.text()
: ''; // Likely a 404. Either way the response isn't the CSS we expect #8142
}

export async function isomorphicFetchText(url: string, options: RequestInit): Promise<string> {
return isWebPage()
// Firefox CSP issue: https://github.com/refined-github/refined-github/issues/8144
? messageRuntime({fetchString: {url, options}})
: fetchText(url, options);
}

0 comments on commit 260c72e

Please sign in to comment.