Skip to content

Commit 780901a

Browse files
authored
Merge pull request #267 from qerionx/main
Even better path handling
2 parents 7e69bec + 9501b26 commit 780901a

9 files changed

Lines changed: 59 additions & 32 deletions

File tree

public/loadersw.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/portal/uv.config.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const k = new TextEncoder().encode(btoa(new Date().toISOString().slice(0, 10) + location.host).split('').reverse().join('').slice(6.7));
2+
const path = location.pathname.substring(0, location.pathname.indexOf('/', 1));
23
self.__uv$config = {
3-
prefix: "/portal/k12/",
4+
prefix: path + "/portal/k12/",
45
encodeUrl: s => {
56
if (!s) return s;
67
try {
@@ -28,11 +29,11 @@ self.__uv$config = {
2829
return new TextDecoder().decode(o) + s.slice(h);
2930
} catch { return decodeURIComponent(s); }
3031
},
31-
handler: "/portal/uv.handler.js",
32-
client: "/portal/uv.client.js",
33-
bundle: "/portal/uv.bundle.js",
34-
config: "/portal/uv.config.js",
35-
sw: "/portal/uv.sw.js"
32+
handler: path + "/portal/uv.handler.js",
33+
client: path + "/portal/uv.client.js",
34+
bundle: path + "/portal/uv.bundle.js",
35+
config: path + "/portal/uv.config.js",
36+
sw: path + "/portal/uv.sw.js"
3637
};
3738

3839
self.console = new Proxy({}, {

public/s_sw.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/sw.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/loader/Omnibox.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const Omnibox = () => {
4747
const navigate = useNavigate();
4848
const { iframeUrls } = loaderStore();
4949

50-
const isProcied = (url) => url?.includes('/portal/k12/') || url?.includes('/ham/');
50+
const isProcied = (url) => url?.includes('portal/k12/') || url?.includes('ham/');
5151
const isNewTab = (url) => !url || url === 'tabs://new' || url.endsWith('/new');
5252

5353
const updateIcon = (url) => {

src/pages/NotFound.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const NotFound = () => {
1919
};
2020

2121
useEffect(() => {
22-
if (loc.pathname.includes('/ham/') || loc.pathname.includes('/portal/k12/')) {
22+
if (loc.pathname.includes('ham/') || loc.pathname.includes('portal/k12/')) {
2323
return;
2424
}
2525
nav('/');
2626
}, [loc, nav]);
2727

28-
if (loc.pathname.includes('/ham/') || loc.pathname.includes('/portal/k12/')) {
28+
if (loc.pathname.includes('ham/') || loc.pathname.includes('portal/k12/')) {
2929
return (
3030
<div
3131
className="min-h-screen flex flex-col items-center justify-center p-6"

src/utils/hooks/loader/useReg.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import store from './useLoaderStore';
88
export default function useReg() {
99
const { options } = useOptions();
1010
const ws = `${location.protocol == 'http:' ? 'ws:' : 'wss:'}//${location.host}/wisp/`;
11-
const sws = [
11+
const sws = isStaticBuild ? [
12+
{ path: new URL('./sw.js', location.href).href, scope: new URL('./portal/k12/', location.href).href },
13+
{ path: new URL('./s_sw.js', location.href).href, scope: new URL('./ham/', location.href).href }
14+
] : [
1215
{ path: new URL('/sw.js', location.origin).href, scope: new URL('/portal/k12/', location.origin).href },
1316
{ path: new URL('/s_sw.js', location.origin).href, scope: new URL('/ham/', location.origin).href }
1417
];
@@ -18,7 +21,9 @@ export default function useReg() {
1821
const init = async () => {
1922
if (!window.scr) {
2023
const script = document.createElement('script');
21-
script.src = '/eggs/scramjet.all.js';
24+
script.src = isStaticBuild
25+
? new URL('./eggs/scramjet.all.js', location.href).pathname
26+
: '/eggs/scramjet.all.js';
2227
await new Promise((resolve, reject) => {
2328
script.onload = resolve;
2429
script.onerror = reject;
@@ -28,12 +33,19 @@ export default function useReg() {
2833

2934
const { ScramjetController } = $scramjetLoadController();
3035

36+
const hamPrefix = isStaticBuild
37+
? new URL('./ham/', location.href).pathname
38+
: '/ham/';
39+
const eggsPath = isStaticBuild
40+
? new URL('./eggs/', location.href).pathname
41+
: '/eggs/';
42+
3143
window.scr = new ScramjetController({
32-
prefix: "/ham/",
44+
prefix: hamPrefix,
3345
files: {
34-
wasm: '/eggs/scramjet.wasm.wasm',
35-
all: '/eggs/scramjet.all.js',
36-
sync: '/eggs/scramjet.sync.js',
46+
wasm: eggsPath + 'scramjet.wasm.wasm',
47+
all: eggsPath + 'scramjet.all.js',
48+
sync: eggsPath + 'scramjet.sync.js',
3749
},
3850
flags: { rewriterLogs: false, scramitize: false, cleanErrors: true, sourcemaps: true },
3951
codec: makecodec()
@@ -52,7 +64,10 @@ export default function useReg() {
5264
}
5365
}
5466

55-
const connection = new BareMuxConnection(new URL('/baremux/worker.js', location.href).href);
67+
const baremuxPath = isStaticBuild
68+
? new URL('./baremux/worker.js', location.href).href
69+
: new URL('/baremux/worker.js', location.origin).href;
70+
const connection = new BareMuxConnection(baremuxPath);
5671
isStaticBuild && setWispStatus('init');
5772
let socket = null;
5873
try {
@@ -62,7 +77,10 @@ export default function useReg() {
6277
}
6378
isStaticBuild && (!socket ? setWispStatus(false) : setWispStatus(true));
6479

65-
await connection.setTransport('/libcurl/index.mjs', [
80+
const libcurlPath = isStaticBuild
81+
? new URL('./libcurl/index.mjs', location.href).pathname
82+
: '/libcurl/index.mjs';
83+
await connection.setTransport(libcurlPath, [
6684
{
6785
wisp:
6886
options.wServer != null && options.wServer !== ''

src/utils/hooks/loader/utils.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,39 @@ const scrwlist = new Set([
2929
]);
3030

3131
export const process = (input, decode = false, prType, engine = "https://www.google.com/search?q=") => {
32+
const upwefix = isStaticBuild
33+
? new URL('./portal/k12/', location.href).pathname
34+
: '/portal/k12/';
35+
const eggowaffle = isStaticBuild
36+
? new URL('./ham/', location.href).pathname
37+
: '/ham/';
38+
3239
let prefix;
3340

3441
switch (prType) {
3542
case 'uv':
36-
prefix = '/portal/k12/';
43+
prefix = upwefix;
3744
break;
3845
case 'scr':
39-
prefix = '/ham/';
46+
prefix = eggowaffle;
4047
break;
4148
default:
4249
const url = check(input, engine);
4350
const match = [...scrwlist].some(d => url.includes(d));
44-
prefix = match ? '/ham/' : '/portal/k12/';
51+
prefix = match ? eggowaffle : upwefix;
4552
}
4653

4754
if (decode) {
48-
const uvPart = input.split('/portal/k12/')[1];
49-
const scrPart = input.split('/ham/')[1];
55+
const uvPart = input.split(upwefix)[1];
56+
const scrPart = input.split(eggowaffle)[1];
5057
const decoded = uvPart ? mango.dnc(uvPart) : scrPart ? mango.dnc(scrPart) : input;
5158
return decoded.endsWith('/') ? decoded.slice(0, -1) : decoded;
5259
} else {
5360
const final = check(input, engine);
5461
if (!final || final.trim() === '') {
5562
return null;
5663
}
57-
const encoded = prefix === '/ham/' ? mango.enc(final) : mango.enc(final);
64+
const encoded = prefix === eggowaffle ? mango.enc(final) : mango.enc(final);
5865
return `${location.protocol}//${location.host}${prefix}${encoded}`;
5966
}
6067
};

src/utils/localGmLoader.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,13 @@ class LocalGmLoader {
172172
let existing = regs.find(r => r.active?.scriptURL.includes('/loadersw.js'));
173173

174174
if (!existing) {
175-
const swPath = isStaticBuild
176-
? new URL('./loadersw.js', location.href).href
177-
: new URL('/loadersw.js', location.origin).href;
175+
const swPath = isStaticBuild
176+
? './loadersw.js'
177+
: '/loadersw.js';
178178
const swScope = isStaticBuild
179-
? new URL('./', location.href).href
179+
? './'
180180
: '/';
181+
181182
const reg = await navigator.serviceWorker.register(swPath, {
182183
scope: swScope,
183184
updateViaCache: 'none'
@@ -232,8 +233,8 @@ class LocalGmLoader {
232233
const existing = await this.getGm(gmName);
233234

234235
const gameUrl = isStaticBuild
235-
? new URL(`./game/${gmName}/index.html`, location.href).href
236-
: new URL(`/game/${gmName}/index.html`, location.origin).href;
236+
? `./game/${gmName}/index.html`
237+
: `/game/${gmName}/index.html`;
237238

238239
if (existing) {
239240
await this.updateLastPlayed(gmName);

0 commit comments

Comments
 (0)