mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
handle unicode characters when encoding
This commit is contained in:
parent
33bcfaedab
commit
7aaf3b7ceb
1 changed files with 83 additions and 68 deletions
|
@ -10,7 +10,7 @@ export function rewriteHtml(
|
||||||
html: string,
|
html: string,
|
||||||
cookieStore: CookieStore,
|
cookieStore: CookieStore,
|
||||||
meta: URLMeta,
|
meta: URLMeta,
|
||||||
fromTop: boolean = false,
|
fromTop: boolean = false
|
||||||
) {
|
) {
|
||||||
const handler = new DomHandler((err, dom) => dom);
|
const handler = new DomHandler((err, dom) => dom);
|
||||||
const parser = new Parser(handler);
|
const parser = new Parser(handler);
|
||||||
|
@ -56,7 +56,7 @@ export function rewriteHtml(
|
||||||
script(self.$scramjet.config["codecs"]),
|
script(self.$scramjet.config["codecs"]),
|
||||||
script("data:application/javascript;base64," + btoa(injected)),
|
script("data:application/javascript;base64," + btoa(injected)),
|
||||||
script(self.$scramjet.config["shared"]),
|
script(self.$scramjet.config["shared"]),
|
||||||
script(self.$scramjet.config["client"]),
|
script(self.$scramjet.config["client"])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ export const htmlRules: {
|
||||||
origin: new URL(meta.origin.origin),
|
origin: new URL(meta.origin.origin),
|
||||||
base: new URL(meta.origin.origin),
|
base: new URL(meta.origin.origin),
|
||||||
},
|
},
|
||||||
true,
|
true
|
||||||
),
|
),
|
||||||
|
|
||||||
// srcdoc
|
// srcdoc
|
||||||
|
@ -180,7 +180,7 @@ export const htmlRules: {
|
||||||
function traverseParsedHtml(
|
function traverseParsedHtml(
|
||||||
node: any,
|
node: any,
|
||||||
cookieStore: CookieStore,
|
cookieStore: CookieStore,
|
||||||
meta: URLMeta,
|
meta: URLMeta
|
||||||
) {
|
) {
|
||||||
if (node.name === "base" && node.attribs.href !== undefined) {
|
if (node.name === "base" && node.attribs.href !== undefined) {
|
||||||
meta.base = new URL(node.attribs.href, meta.origin);
|
meta.base = new URL(node.attribs.href, meta.origin);
|
||||||
|
@ -213,12 +213,15 @@ function traverseParsedHtml(
|
||||||
if (
|
if (
|
||||||
node.name === "script" &&
|
node.name === "script" &&
|
||||||
/(application|text)\/javascript|module|importmap|undefined/.test(
|
/(application|text)\/javascript|module|importmap|undefined/.test(
|
||||||
node.attribs.type,
|
node.attribs.type
|
||||||
) &&
|
) &&
|
||||||
node.children[0] !== undefined
|
node.children[0] !== undefined
|
||||||
) {
|
) {
|
||||||
let js = node.children[0].data;
|
let js = node.children[0].data;
|
||||||
node.attribs[`data-scramjet-script-source-src`] = btoa(js);
|
// node.attribs[`data-scramjet-script-source-src`] = btoa(js);
|
||||||
|
node.attribs[`data-scramjet-script-source-src`] = bytesToBase64(
|
||||||
|
new TextEncoder().encode(js)
|
||||||
|
);
|
||||||
const htmlcomment = /<!--[\s\S]*?-->/g;
|
const htmlcomment = /<!--[\s\S]*?-->/g;
|
||||||
js = js.replace(htmlcomment, "");
|
js = js.replace(htmlcomment, "");
|
||||||
node.children[0].data = rewriteJs(js, meta);
|
node.children[0].data = rewriteJs(js, meta);
|
||||||
|
@ -245,7 +248,7 @@ function traverseParsedHtml(
|
||||||
node.childNodes[childNode] = traverseParsedHtml(
|
node.childNodes[childNode] = traverseParsedHtml(
|
||||||
node.childNodes[childNode],
|
node.childNodes[childNode],
|
||||||
cookieStore,
|
cookieStore,
|
||||||
meta,
|
meta
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -266,3 +269,15 @@ export function rewriteSrcset(srcset: string, meta: URLMeta) {
|
||||||
|
|
||||||
return rewrittenUrls.join("");
|
return rewrittenUrls.join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function base64ToBytes(base64) {
|
||||||
|
const binString = atob(base64);
|
||||||
|
return Uint8Array.from(binString, (m) => m.codePointAt(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
function bytesToBase64(bytes: Uint8Array) {
|
||||||
|
const binString = Array.from(bytes, (byte) =>
|
||||||
|
String.fromCodePoint(byte)
|
||||||
|
).join("");
|
||||||
|
return btoa(binString);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue