mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
fix minor bugs
This commit is contained in:
parent
809e69461e
commit
913b5aeb50
7 changed files with 34 additions and 27 deletions
|
@ -2,7 +2,7 @@ check back in 3 months when scramjet supports 23% of what UV supports
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
- Finish HTML rewriting
|
- Finish HTML rewriting
|
||||||
- \<meta> tag rewriting: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
|
- ~~\<meta> tag rewriting: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta~~
|
||||||
- Build script imports for scramjet scripts in DomHandler AST
|
- Build script imports for scramjet scripts in DomHandler AST
|
||||||
|
|
||||||
- example stringified output:
|
- example stringified output:
|
||||||
|
@ -15,13 +15,13 @@ TODO
|
||||||
- `<script type="importmap"></script>` rewriting
|
- `<script type="importmap"></script>` rewriting
|
||||||
- Make an array of all possible import values and pass the array onto the JS rewriter, then rewrite all the URLs inside of it
|
- Make an array of all possible import values and pass the array onto the JS rewriter, then rewrite all the URLs inside of it
|
||||||
- Fix URL rewriting
|
- Fix URL rewriting
|
||||||
- Pass the full the full url object arround instead of just the origin and if the url is relative append it to the url path before adding it to the origin
|
- ~~Pass the full the full url object arround instead of just the origin and if the url is relative append it to the url path before adding it to the origin~~
|
||||||
- Finish JS rewriting
|
- Finish JS rewriting
|
||||||
- Only thing rewritten currently are imports and exports
|
- Only thing rewritten currently are imports and exports
|
||||||
- Check imports/exports for values contained in the `importmap` array, don't rewrite the node value if present
|
- Check imports/exports for values contained in the `importmap` array, don't rewrite the node value if present
|
||||||
- Fix CSS rewriting
|
- Fix CSS rewriting
|
||||||
- CSS rewriting only rewrites the `url()` function, but `@import` rules can import urls using just a string: https://developer.mozilla.org/en-US/docs/Web/CSS/@import
|
- CSS rewriting only rewrites the `url()` function, but `@import` rules can import urls using just a string: https://developer.mozilla.org/en-US/docs/Web/CSS/@import
|
||||||
- Rewrite `Link` header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link
|
- ~~Rewrite `Link` header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link~~
|
||||||
- Write client APIs
|
- Write client APIs
|
||||||
- `__scope$()` function for JS rewriting
|
- `__scope$()` function for JS rewriting
|
||||||
- Location object
|
- Location object
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { encodeUrl } from "./url"
|
import { encodeUrl } from "./url"
|
||||||
|
|
||||||
export function rewriteCss(css: string, origin?: string) {
|
export function rewriteCss(css: string, origin?: URL) {
|
||||||
css = css.replace(/(?<=url\("?'?)[^"'][\S]*[^"'](?="?'?\);?)/g, (match) => encodeUrl(match, origin));
|
css = css.replace(/(?<=url\("?'?)[^"'][\S]*[^"'](?="?'?\);?)/g, (match) => encodeUrl(match, origin));
|
||||||
|
|
||||||
return "/* intercepted by scramjet 🐳 */" + css;
|
return "/* intercepted by scramjet 🐳 */" + css;
|
||||||
|
|
|
@ -26,11 +26,13 @@ const urlHeaders = [
|
||||||
"referer"
|
"referer"
|
||||||
];
|
];
|
||||||
|
|
||||||
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: string) {
|
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
|
|
||||||
for (const key in rawHeaders) {
|
for (const key in rawHeaders) {
|
||||||
headers[key.toLowerCase()] = rawHeaders[key];
|
headers[key.toLowerCase()] = rawHeaders[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
cspHeaders.forEach((header) => {
|
cspHeaders.forEach((header) => {
|
||||||
delete headers[header];
|
delete headers[header];
|
||||||
});
|
});
|
||||||
|
@ -38,7 +40,11 @@ export function rewriteHeaders(rawHeaders: BareHeaders, origin?: string) {
|
||||||
urlHeaders.forEach((header) => {
|
urlHeaders.forEach((header) => {
|
||||||
if (headers[header])
|
if (headers[header])
|
||||||
headers[header] = encodeUrl(headers[header] as string, origin);
|
headers[header] = encodeUrl(headers[header] as string, origin);
|
||||||
})
|
});
|
||||||
|
|
||||||
|
if (headers["link"]) {
|
||||||
|
headers["link"] = headers["link"].replace(/<(.*?)>/gi, (match) => encodeUrl(match, origin));
|
||||||
|
}
|
||||||
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ import { rewriteJs } from "./js";
|
||||||
// html nodes to rewrite
|
// html nodes to rewrite
|
||||||
// meta
|
// meta
|
||||||
|
|
||||||
export function rewriteHtml(html: string, origin?: string) {
|
export function rewriteHtml(html: string, origin?: URL) {
|
||||||
const handler = new DomHandler((err, dom) => dom);
|
const handler = new DomHandler((err, dom) => dom);
|
||||||
const parser = new Parser(handler);
|
const parser = new Parser(handler);
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ export function rewriteHtml(html: string, origin?: string) {
|
||||||
return render(traverseParsedHtml(handler.root, origin));
|
return render(traverseParsedHtml(handler.root, origin));
|
||||||
}
|
}
|
||||||
|
|
||||||
function traverseParsedHtml(node, origin?: string) {
|
function traverseParsedHtml(node, origin?: URL) {
|
||||||
/* csp attributes */
|
/* csp attributes */
|
||||||
if (hasAttrib(node, "nonce")) delete node.attribs.nonce;
|
if (hasAttrib(node, "nonce")) delete node.attribs.nonce;
|
||||||
if (hasAttrib(node, "integrity")) delete node.attribs.integrity;
|
if (hasAttrib(node, "integrity")) delete node.attribs.integrity;
|
||||||
|
@ -39,6 +39,13 @@ function traverseParsedHtml(node, origin?: string) {
|
||||||
|
|
||||||
if (node.name === "style" && node.children[0] !== undefined) node.children[0].data = rewriteCss(node.children[0].data, origin);
|
if (node.name === "style" && node.children[0] !== undefined) node.children[0].data = rewriteCss(node.children[0].data, origin);
|
||||||
if (node.name === "script" && /(application|text)\/javascript|importmap|undefined/.test(node.attribs.type) && node.children[0] !== undefined) node.children[0].data = rewriteJs(node.children[0].data, origin);
|
if (node.name === "script" && /(application|text)\/javascript|importmap|undefined/.test(node.attribs.type) && node.children[0] !== undefined) node.children[0].data = rewriteJs(node.children[0].data, origin);
|
||||||
|
if (node.name === "meta" && hasAttrib(node, "http-equiv")) {
|
||||||
|
if (node.attribs["http-equiv"] === "content-security-policy") {
|
||||||
|
return;
|
||||||
|
} else if (node.attribs["http-equiv"] === "refresh") {
|
||||||
|
node.attribs.content = node.attribs.content.split(";url=").map((elem, index) => index === 1 ? encodeUrl(elem) : elem).join(";url=");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (node.childNodes) {
|
if (node.childNodes) {
|
||||||
for (const childNode in node.childNodes) {
|
for (const childNode in node.childNodes) {
|
||||||
|
@ -50,7 +57,7 @@ function traverseParsedHtml(node, origin?: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// stole from osana lmao
|
// stole from osana lmao
|
||||||
export function rewriteSrcset(srcset: string, origin?: string) {
|
export function rewriteSrcset(srcset: string, origin?: URL) {
|
||||||
const urls = srcset.split(/ [0-9]+x,? ?/g);
|
const urls = srcset.split(/ [0-9]+x,? ?/g);
|
||||||
if (!urls) return "";
|
if (!urls) return "";
|
||||||
const sufixes = srcset.match(/ [0-9]+x,? ?/g);
|
const sufixes = srcset.match(/ [0-9]+x,? ?/g);
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { encodeUrl } from "./url";
|
||||||
|
|
||||||
// i am a cat. i like to be petted. i like to be fed. i like to be
|
// i am a cat. i like to be petted. i like to be fed. i like to be
|
||||||
|
|
||||||
export function rewriteJs(js: string, origin?: string) {
|
export function rewriteJs(js: string, origin?: URL) {
|
||||||
const ast = parse(js, {
|
const ast = parse(js, {
|
||||||
module: true
|
module: true
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
import { rewriteJs } from "./js";
|
import { rewriteJs } from "./js";
|
||||||
|
|
||||||
function canParseUrl(url: string, origin?: string) {
|
function canParseUrl(url: string, origin?: URL) {
|
||||||
if (URL.canParse) {
|
|
||||||
console.log(URL.canParse(url, origin) + "\n" + url + "\n" + origin);
|
|
||||||
|
|
||||||
return URL.canParse(url, origin);
|
|
||||||
} else {
|
|
||||||
try {
|
try {
|
||||||
new URL(url, origin);
|
new URL(url, origin);
|
||||||
|
|
||||||
|
@ -13,13 +8,12 @@ function canParseUrl(url: string, origin?: string) {
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// something is broken with this but i didn't debug it
|
// something is broken with this but i didn't debug it
|
||||||
export function encodeUrl(url: string, origin?: string) {
|
export function encodeUrl(url: string, origin?: URL) {
|
||||||
if (!origin) {
|
if (!origin) {
|
||||||
origin = self.__scramjet$config.codec.decode(location.href.slice((location.origin + self.__scramjet$config.prefix).length));
|
origin = new URL(self.__scramjet$config.codec.decode(location.href.slice((location.origin + self.__scramjet$config.prefix).length)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.startsWith("javascript:")) {
|
if (url.startsWith("javascript:")) {
|
||||||
|
|
|
@ -41,13 +41,13 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
|
||||||
switch (request.destination) {
|
switch (request.destination) {
|
||||||
case "iframe":
|
case "iframe":
|
||||||
case "document":
|
case "document":
|
||||||
responseBody = self.__scramjet$bundle.rewriters.rewriteHtml(await response.text(), url.origin);
|
responseBody = self.__scramjet$bundle.rewriters.rewriteHtml(await response.text(), origin);
|
||||||
break;
|
break;
|
||||||
case "script":
|
case "script":
|
||||||
responseBody = self.__scramjet$bundle.rewriters.rewriteJs(await response.text(), url.origin);
|
responseBody = self.__scramjet$bundle.rewriters.rewriteJs(await response.text(), origin);
|
||||||
break;
|
break;
|
||||||
case "style":
|
case "style":
|
||||||
responseBody = self.__scramjet$bundle.rewriters.rewriteCss(await response.text(), url.origin);
|
responseBody = self.__scramjet$bundle.rewriters.rewriteCss(await response.text(), origin);
|
||||||
break;
|
break;
|
||||||
case "sharedworker":
|
case "sharedworker":
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue