mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
html rewriting
This commit is contained in:
parent
9ec2247df7
commit
b8ee2ef53e
5 changed files with 62 additions and 22 deletions
|
@ -8,7 +8,7 @@
|
||||||
"no-unused-vars": "error",
|
"no-unused-vars": "error",
|
||||||
"quotes": ["error", "double"],
|
"quotes": ["error", "double"],
|
||||||
"max-lines-per-function": ["error", {
|
"max-lines-per-function": ["error", {
|
||||||
"max": 60,
|
"max": 200,
|
||||||
"skipComments": true
|
"skipComments": true
|
||||||
}],
|
}],
|
||||||
"getter-return": "error",
|
"getter-return": "error",
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { encodeUrl, decodeUrl } from "./rewriters/url";
|
import { encodeUrl, decodeUrl } from "./rewriters/url";
|
||||||
import { rewriteCss } from "./rewriters/css";
|
import { rewriteCss } from "./rewriters/css";
|
||||||
import { rewriteHtml } from "./rewriters/html";
|
import { rewriteHtml, rewriteSrcset } from "./rewriters/html";
|
||||||
|
import { rewriteJs } from "./rewriters/js";
|
||||||
|
|
||||||
const bundle = {
|
const bundle = {
|
||||||
rewriters: {
|
rewriters: {
|
||||||
|
@ -8,7 +9,9 @@ const bundle = {
|
||||||
encodeUrl, decodeUrl
|
encodeUrl, decodeUrl
|
||||||
},
|
},
|
||||||
rewriteCss,
|
rewriteCss,
|
||||||
rewriteHtml
|
rewriteHtml,
|
||||||
|
rewriteSrcset,
|
||||||
|
rewriteJs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,24 +6,7 @@ import { encodeUrl } from "./url";
|
||||||
import { rewriteCss } from "./css";
|
import { rewriteCss } from "./css";
|
||||||
|
|
||||||
// html nodes to rewrite
|
// html nodes to rewrite
|
||||||
// object
|
|
||||||
// iframe
|
|
||||||
// embed
|
|
||||||
// link
|
|
||||||
// style
|
|
||||||
// script
|
|
||||||
// img
|
|
||||||
// source
|
|
||||||
// form
|
|
||||||
// meta
|
// meta
|
||||||
// area
|
|
||||||
// base
|
|
||||||
// body
|
|
||||||
// input
|
|
||||||
// audio
|
|
||||||
// button
|
|
||||||
// track
|
|
||||||
// video
|
|
||||||
|
|
||||||
export function rewriteHtml(html: string, origin?: string) {
|
export function rewriteHtml(html: string, origin?: string) {
|
||||||
const handler = new DomHandler((err, dom) => dom);
|
const handler = new DomHandler((err, dom) => dom);
|
||||||
|
@ -63,6 +46,40 @@ function traverseParsedHtml(node: any, origin?: string) {
|
||||||
|
|
||||||
// implement js rewriting when done
|
// implement js rewriting when done
|
||||||
} else if (node.name === "img" && hasAttrib(node, "src")) {
|
} else if (node.name === "img" && hasAttrib(node, "src")) {
|
||||||
|
if (hasAttrib(node, "src")) {
|
||||||
|
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasAttrib(node, "srcset")) {
|
||||||
|
node.attribs.srcset = rewriteSrcset(node.attribs.srcset);
|
||||||
|
}
|
||||||
|
} else if (node.name === "object") {
|
||||||
|
node.attribs.data = encodeUrl(node.attribs.data, origin);
|
||||||
|
} else if (node.name === "embed") {
|
||||||
|
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
||||||
|
} else if (node.name === "source") {
|
||||||
|
if (hasAttrib(node, "src")) {
|
||||||
|
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasAttrib(node, "srcset")) {
|
||||||
|
node.attribs.srcset = rewriteSrcset(node.attribs.srcset);
|
||||||
|
}
|
||||||
|
} else if (node.name === "form") {
|
||||||
|
node.attribs.action = encodeUrl(node.attribs.action, origin);
|
||||||
|
} else if (node.name === "area") {
|
||||||
|
node.attribs.href = encodeUrl(node.attribs.href, origin);
|
||||||
|
} else if (node.name === "base" && hasAttrib(node, "href")) {
|
||||||
|
node.attribs.href = encodeUrl(node.attribs.href, origin);
|
||||||
|
} else if (node.name === "input") {
|
||||||
|
node.attribs.formaction = encodeUrl(node.attribs.formaction, origin);
|
||||||
|
} else if (node.name === "audio") {
|
||||||
|
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
||||||
|
} else if (node.name === "button") {
|
||||||
|
node.attribs.formaction = encodeUrl(node.attribs.formaction, origin);
|
||||||
|
} else if (node.name === "track") {
|
||||||
|
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
||||||
|
} else if (node.name === "video") {
|
||||||
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
node.attribs.src = encodeUrl(node.attribs.src, origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,3 +91,18 @@ function traverseParsedHtml(node: any, origin?: string) {
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stole from osana lmao
|
||||||
|
export function rewriteSrcset(srcset: string, origin?: string) {
|
||||||
|
const urls = srcset.split(/ [0-9]+x,? ?/g);
|
||||||
|
if (!urls) return "";
|
||||||
|
const sufixes = srcset.match(/ [0-9]+x,? ?/g);
|
||||||
|
if (!sufixes) return "";
|
||||||
|
const rewrittenUrls = urls.map((url, i) => {
|
||||||
|
if (url && sufixes[i]) {
|
||||||
|
return encodeUrl(url, origin) + sufixes[i];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return rewrittenUrls.join("");
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
// don't do js rewriting yet
|
||||||
|
// i'm making a benchmark on different rewriting methods
|
||||||
|
|
||||||
|
export function rewriteJs(js: string, origin?: string) {
|
||||||
|
return js;
|
||||||
|
}
|
|
@ -33,8 +33,7 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
|
||||||
} else if (event.request.destination === "style") {
|
} else if (event.request.destination === "style") {
|
||||||
responseBody = self.__scramjet$bundle.rewriters.rewriteCss(await response.text(), url.origin);
|
responseBody = self.__scramjet$bundle.rewriters.rewriteCss(await response.text(), url.origin);
|
||||||
} else if (event.request.destination === "script") {
|
} else if (event.request.destination === "script") {
|
||||||
responseBody = await response.text();
|
responseBody = self.__scramjet$bundle.rewriters.rewriteJs(await response.text(), url.origin);
|
||||||
// implement when js is finished
|
|
||||||
} else {
|
} else {
|
||||||
responseBody = response.body;
|
responseBody = response.body;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue