mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
add script cloning
This commit is contained in:
parent
efa30f6d84
commit
759ebd747e
4 changed files with 42 additions and 2 deletions
|
@ -16,8 +16,6 @@ export function encodeUrl(url: string, origin?: string | URL) {
|
||||||
origin = new URL(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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(url);
|
|
||||||
|
|
||||||
if (url.startsWith("javascript:")) {
|
if (url.startsWith("javascript:")) {
|
||||||
return "javascript:" + rewriteJs(url.slice("javascript:".length));
|
return "javascript:" + rewriteJs(url.slice("javascript:".length));
|
||||||
} else if (/^(#|mailto|about|data)/.test(url) || url.startsWith(location.origin + self.__scramjet$config.prefix)) {
|
} else if (/^(#|mailto|about|data)/.test(url) || url.startsWith(location.origin + self.__scramjet$config.prefix)) {
|
||||||
|
|
|
@ -44,6 +44,10 @@ Object.keys(attribs).forEach((attrib: string) => {
|
||||||
},
|
},
|
||||||
|
|
||||||
set(value) {
|
set(value) {
|
||||||
|
if (this.dataset["scramjet"]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(value);
|
||||||
this.dataset[`${attrib}`] = value;
|
this.dataset[`${attrib}`] = value;
|
||||||
if (/nonce|integrity|csp/.test(attrib)) {
|
if (/nonce|integrity|csp/.test(attrib)) {
|
||||||
this.removeAttribute(attrib);
|
this.removeAttribute(attrib);
|
||||||
|
@ -84,6 +88,10 @@ Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
|
||||||
|
|
||||||
Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
|
Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
|
||||||
apply(target, thisArg, argArray) {
|
apply(target, thisArg, argArray) {
|
||||||
|
if (thisArg.dataset["scramjet"]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(argArray[1])
|
||||||
if (Object.keys(attribs).includes(argArray[0])) {
|
if (Object.keys(attribs).includes(argArray[0])) {
|
||||||
thisArg.dataset[`${argArray[0]}`] = argArray[1];
|
thisArg.dataset[`${argArray[0]}`] = argArray[1];
|
||||||
if (/nonce|integrity|csp/.test(argArray[0])) {
|
if (/nonce|integrity|csp/.test(argArray[0])) {
|
||||||
|
|
25
src/html/cloner.ts
Normal file
25
src/html/cloner.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
export default class Clone {
|
||||||
|
el: HTMLElement;
|
||||||
|
copy: HTMLElement
|
||||||
|
|
||||||
|
constructor(element: HTMLElement) {
|
||||||
|
this.el = element;
|
||||||
|
this.copy = document.createElement(element.tagName);
|
||||||
|
|
||||||
|
for (const attr of element.getAttributeNames()) {
|
||||||
|
this.copy.setAttribute(attr, element.getAttribute(attr));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.innerHTML) {
|
||||||
|
this.copy.innerHTML = element.innerHTML;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
insertCopy() {
|
||||||
|
this.el.insertAdjacentElement("afterend", this.copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeElement() {
|
||||||
|
this.el.remove();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
// this code needs to be bundled as a separate file due to when it is ran
|
// this code needs to be bundled as a separate file due to when it is ran
|
||||||
|
|
||||||
import { encodeUrl, rewriteCss, rewriteJs, rewriteSrcset } from "../bundle";
|
import { encodeUrl, rewriteCss, rewriteJs, rewriteSrcset } from "../bundle";
|
||||||
|
import Clone from "./cloner.ts";
|
||||||
|
|
||||||
const parser = new DOMParser();
|
const parser = new DOMParser();
|
||||||
|
|
||||||
|
@ -79,4 +80,12 @@ navigator.serviceWorker.ready.then(({ active }) => {
|
||||||
|
|
||||||
navigator.serviceWorker.addEventListener("message", (message) => {
|
navigator.serviceWorker.addEventListener("message", (message) => {
|
||||||
document.documentElement.replaceWith(rewriteHtml(message.data).documentElement);
|
document.documentElement.replaceWith(rewriteHtml(message.data).documentElement);
|
||||||
|
|
||||||
|
const scripts = document.querySelectorAll("script:not([data-scramjet])");
|
||||||
|
|
||||||
|
for (const script of scripts) {
|
||||||
|
const clone = new Clone(script);
|
||||||
|
clone.insertCopy();
|
||||||
|
clone.removeElement();
|
||||||
|
}
|
||||||
});
|
});
|
Loading…
Add table
Add a link
Reference in a new issue