mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
fix rewriting
This commit is contained in:
parent
9be963e977
commit
00dbf22c70
4 changed files with 27 additions and 5 deletions
|
@ -21,3 +21,20 @@ declare global {
|
||||||
__s: any;
|
__s: any;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const scripts = document.querySelectorAll("script:not([data-scramjet])");
|
||||||
|
|
||||||
|
for (const script of scripts) {
|
||||||
|
const clone = document.createElement("script");
|
||||||
|
|
||||||
|
for (const attr of Array.from(script.attributes)) {
|
||||||
|
clone.setAttribute(attr.name, attr.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script.innerHTML !== "") {
|
||||||
|
clone.innerHTML = script.innerHTML;
|
||||||
|
}
|
||||||
|
|
||||||
|
script.insertAdjacentElement("afterend", clone);
|
||||||
|
script.remove();
|
||||||
|
}
|
|
@ -12,4 +12,4 @@ WebSocket = new Proxy(WebSocket, {
|
||||||
ArrayBuffer.prototype
|
ArrayBuffer.prototype
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
});
|
|
@ -4,7 +4,7 @@ import { hasAttrib } from "domutils";
|
||||||
import render from "dom-serializer";
|
import render from "dom-serializer";
|
||||||
import { encodeUrl } from "./url";
|
import { encodeUrl } from "./url";
|
||||||
import { rewriteCss } from "./css";
|
import { rewriteCss } from "./css";
|
||||||
import { rewriteJs } from "./js";
|
// import { rewriteJs } from "./js";
|
||||||
import { isScramjetFile } from "..";
|
import { isScramjetFile } from "..";
|
||||||
|
|
||||||
export function rewriteHtml(html: string, origin?: URL) {
|
export function rewriteHtml(html: string, origin?: URL) {
|
||||||
|
@ -50,7 +50,7 @@ function traverseParsedHtml(node, origin?: URL) {
|
||||||
if (hasAttrib(node, "style")) node.attribs.style = rewriteCss(node.attribs.style, origin);
|
if (hasAttrib(node, "style")) node.attribs.style = rewriteCss(node.attribs.style, origin);
|
||||||
|
|
||||||
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.name === "meta" && hasAttrib(node, "http-equiv")) {
|
||||||
if (node.attribs["http-equiv"] === "content-security-policy") {
|
if (node.attribs["http-equiv"] === "content-security-policy") {
|
||||||
node = {};
|
node = {};
|
||||||
|
@ -66,7 +66,8 @@ function traverseParsedHtml(node, origin?: URL) {
|
||||||
["codecs", "config", "shared", "client"].forEach((script) => {
|
["codecs", "config", "shared", "client"].forEach((script) => {
|
||||||
scramjetScripts.push(new Element("script", {
|
scramjetScripts.push(new Element("script", {
|
||||||
src: self.__scramjet$config[script],
|
src: self.__scramjet$config[script],
|
||||||
type: "module"
|
type: "module",
|
||||||
|
"data-scramjet": ""
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,11 @@ export default class ScramjetServiceWorker {
|
||||||
switch (request.destination) {
|
switch (request.destination) {
|
||||||
case "iframe":
|
case "iframe":
|
||||||
case "document":
|
case "document":
|
||||||
responseBody = rewriteHtml(await response.text(), url);
|
if (responseHeaders["content-type"].startsWith("text/html")) {
|
||||||
|
responseBody = rewriteHtml(await response.text(), url);
|
||||||
|
} else {
|
||||||
|
responseBody = response.body;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "script":
|
case "script":
|
||||||
responseBody = rewriteJs(await response.text(), url);
|
responseBody = rewriteJs(await response.text(), url);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue