mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
bug fixes
Co-authored-by: Avad3 <Avad3@users.noreply.github.com>
This commit is contained in:
parent
a39a2657c6
commit
47b59945a9
23 changed files with 385 additions and 327 deletions
37
src/shared/rewriters/url.ts
Normal file
37
src/shared/rewriters/url.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { rewriteJs } from "./js";
|
||||
|
||||
function canParseUrl(url: string, origin?: URL) {
|
||||
try {
|
||||
new URL(url, origin);
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// something is broken with this but i didn't debug it
|
||||
export function encodeUrl(url: string, origin?: URL) {
|
||||
if (!origin) {
|
||||
origin = new URL(self.__scramjet$config.codec.decode(location.href.slice((location.origin + self.__scramjet$config.prefix).length)));
|
||||
}
|
||||
|
||||
if (url.startsWith("javascript:")) {
|
||||
return "javascript:" + rewriteJs(url.slice("javascript:".length));
|
||||
} else if (/^(#|mailto|about|data)/.test(url)) {
|
||||
return url;
|
||||
} else if (canParseUrl(url, origin)) {
|
||||
return location.origin + self.__scramjet$config.prefix + self.__scramjet$config.codec.encode(new URL(url, origin).href);
|
||||
}
|
||||
}
|
||||
|
||||
// something is also broken with this but i didn't debug it
|
||||
export function decodeUrl(url: string) {
|
||||
if (/^(#|about|data|mailto|javascript)/.test(url)) {
|
||||
return url;
|
||||
} else if (canParseUrl(url)) {
|
||||
return self.__scramjet$config.codec.decode(url.slice((location.origin + self.__scramjet$config.prefix).length))
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue