mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
fix config stuff
This commit is contained in:
parent
b180272b17
commit
39af54f893
6 changed files with 126 additions and 97 deletions
|
@ -1,73 +0,0 @@
|
|||
import IDBMap from "idb-map-entries";
|
||||
import { ScramjetConfig } from "../types";
|
||||
import { Codec } from "../codecs";
|
||||
|
||||
export class ScramjetBootstrapper {
|
||||
config: ScramjetConfig;
|
||||
private store: IDBMap;
|
||||
codec: Codec;
|
||||
|
||||
constructor(config: ScramjetConfig) {
|
||||
const defaultConfig = {
|
||||
prefix: "/scramjet/",
|
||||
codec: "plain",
|
||||
wrapfn: "$scramjet$wrap",
|
||||
trysetfn: "$scramjet$tryset",
|
||||
importfn: "$scramjet$import",
|
||||
rewritefn: "$scramjet$rewrite",
|
||||
shared: "/scramjet.shared.js",
|
||||
worker: "/scramjet.worker.js",
|
||||
thread: "/scramjet.thread.js",
|
||||
client: "/scramjet.client.js",
|
||||
codecs: "/scramjet.codecs.js",
|
||||
};
|
||||
|
||||
this.config = Object.assign({}, defaultConfig, config);
|
||||
|
||||
// rspack won't let me use a dynamic import
|
||||
fetch(config.codecs).then(async (response) => {
|
||||
eval(await response.text());
|
||||
|
||||
self.$scramjet.codec = self.$scramjet.codecs[this.config.codec];
|
||||
self.$scramjet.config = this.config;
|
||||
});
|
||||
|
||||
console.log(this.config);
|
||||
this.store = new IDBMap("config", {
|
||||
prefix: "scramjet",
|
||||
});
|
||||
this.saveConfig();
|
||||
}
|
||||
|
||||
registerSw(serviceWorkerPath: string) {
|
||||
if ("serviceWorker" in navigator) {
|
||||
navigator.serviceWorker
|
||||
.register(serviceWorkerPath, {
|
||||
scope: this.config.prefix,
|
||||
})
|
||||
.then((registration) => {
|
||||
console.log(
|
||||
"ServiceWorker registration successful with scope: ",
|
||||
registration.scope
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("ServiceWorker registration failed: ", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
saveConfig() {
|
||||
this.store.set("config", this.config).then(() => {
|
||||
console.log("scramjet config saved");
|
||||
});
|
||||
}
|
||||
|
||||
modifyConfig(config: ScramjetConfig) {
|
||||
this.config = Object.assign({}, this.config, config);
|
||||
|
||||
this.saveConfig();
|
||||
}
|
||||
}
|
||||
|
||||
window.ScramjetBootstrapper = ScramjetBootstrapper;
|
98
src/controller/index.ts
Normal file
98
src/controller/index.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
import IDBMap from "@webreflection/idb-map";
|
||||
import { ScramjetConfig } from "../types";
|
||||
import { Codec } from "../codecs";
|
||||
|
||||
export class ScramjetController {
|
||||
config: ScramjetConfig;
|
||||
private store: IDBMap;
|
||||
codec: Codec;
|
||||
|
||||
constructor(config: ScramjetConfig) {
|
||||
const defaultConfig = {
|
||||
prefix: "/scramjet/",
|
||||
codec: "plain",
|
||||
wrapfn: "$scramjet$wrap",
|
||||
trysetfn: "$scramjet$tryset",
|
||||
importfn: "$scramjet$import",
|
||||
rewritefn: "$scramjet$rewrite",
|
||||
shared: "/scramjet.shared.js",
|
||||
worker: "/scramjet.worker.js",
|
||||
thread: "/scramjet.thread.js",
|
||||
client: "/scramjet.client.js",
|
||||
codecs: "/scramjet.codecs.js",
|
||||
};
|
||||
|
||||
this.config = Object.assign({}, defaultConfig, config);
|
||||
}
|
||||
|
||||
async init(serviceWorkerPath: string): Promise<ServiceWorkerRegistration> {
|
||||
await import(/* webpackIgnore: true */ this.config.codecs);
|
||||
this.codec = self.$scramjet.codecs[this.config.codec];
|
||||
|
||||
this.store = new IDBMap("config", {
|
||||
prefix: "scramjet",
|
||||
});
|
||||
await this.#saveConfig();
|
||||
|
||||
const reg = await navigator.serviceWorker.register(serviceWorkerPath, {
|
||||
scope: this.config.prefix,
|
||||
});
|
||||
dbg.log("service worker registered");
|
||||
|
||||
return reg;
|
||||
}
|
||||
|
||||
createFrame(frame?: HTMLIFrameElement): ScramjetFrame {
|
||||
if (!frame) {
|
||||
frame = document.createElement("iframe");
|
||||
}
|
||||
|
||||
return new ScramjetFrame(this, frame);
|
||||
}
|
||||
|
||||
encodeUrl(url: string | URL): string {
|
||||
if (url instanceof URL) url = url.toString();
|
||||
|
||||
return this.config.prefix + this.codec.encode(url);
|
||||
}
|
||||
|
||||
async #saveConfig() {
|
||||
this.store.set("config", this.config);
|
||||
}
|
||||
|
||||
async modifyConfig(config: ScramjetConfig) {
|
||||
this.config = Object.assign({}, this.config, config);
|
||||
this.codec = self.$scramjet.codecs[this.config.codec];
|
||||
|
||||
await this.#saveConfig();
|
||||
}
|
||||
}
|
||||
|
||||
class ScramjetFrame extends EventTarget {
|
||||
static SCRAMJETFRAME = Symbol.for("scramjet frame handle");
|
||||
constructor(
|
||||
private controller: ScramjetController,
|
||||
public frame: HTMLIFrameElement
|
||||
) {
|
||||
super();
|
||||
frame[ScramjetFrame.SCRAMJETFRAME] = this;
|
||||
}
|
||||
|
||||
go(url: string | URL) {
|
||||
if (url instanceof URL) url = url.toString();
|
||||
|
||||
dbg.log("navigated to", url);
|
||||
|
||||
this.frame.src = this.controller.encodeUrl(url);
|
||||
}
|
||||
|
||||
back() {
|
||||
this.frame.contentWindow?.history.back();
|
||||
}
|
||||
|
||||
forward() {
|
||||
this.frame.contentWindow?.history.forward();
|
||||
}
|
||||
}
|
||||
|
||||
window.ScramjetController = ScramjetController;
|
4
src/types.d.ts
vendored
4
src/types.d.ts
vendored
|
@ -1,4 +1,4 @@
|
|||
import { ScramjetBootstrapper } from "./bootsrapper/index";
|
||||
import { ScramjetController } from "./bootsrapper/index";
|
||||
import { encodeUrl, decodeUrl } from "./shared/rewriters/url";
|
||||
import { rewriteCss } from "./shared/rewriters/css";
|
||||
import { rewriteHtml, rewriteSrcset } from "./shared/rewriters/html";
|
||||
|
@ -56,6 +56,6 @@ declare global {
|
|||
codec: Codec;
|
||||
};
|
||||
WASM: string;
|
||||
ScramjetBootstrapper: typeof ScramjetBootstrapper;
|
||||
ScramjetController: typeof ScramjetController;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,10 +29,11 @@
|
|||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="https://unpkg.com/dreamland"></script>
|
||||
<script src="/baremux/index.js" defer></script>
|
||||
<script src="/scram/scramjet.bootstrapper.js"></script>
|
||||
<script src="/scram/scramjet.controller.js"></script>
|
||||
<script src="ui.js" defer></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -7,6 +7,7 @@ importScripts(
|
|||
const scramjet = new ScramjetServiceWorker();
|
||||
|
||||
async function handleRequest(event) {
|
||||
await scramjet.loadConfig();
|
||||
if (scramjet.route(event)) {
|
||||
return scramjet.fetch(event);
|
||||
}
|
||||
|
|
44
static/ui.js
44
static/ui.js
|
@ -1,4 +1,4 @@
|
|||
const bootstrapper = new ScramjetBootstrapper({
|
||||
const scramjet = new ScramjetController({
|
||||
codecs: "/scram/scramjet.codecs.js",
|
||||
worker: "/scram/scramjet.worker.js",
|
||||
thread: "/scram/scramjet.thread.js",
|
||||
|
@ -6,23 +6,23 @@ const bootstrapper = new ScramjetBootstrapper({
|
|||
shared: "/scram/scramjet.shared.js",
|
||||
});
|
||||
|
||||
bootstrapper.registerSw("./sw.js");
|
||||
scramjet.init("./sw.js");
|
||||
|
||||
navigator.serviceWorker.ready.then((reg) => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const thread = new SharedWorker($scramjet.config.thread, {
|
||||
name: "thread" + i,
|
||||
});
|
||||
|
||||
reg.active.postMessage(
|
||||
{
|
||||
scramjet$type: "add",
|
||||
handle: thread.port,
|
||||
},
|
||||
[thread.port]
|
||||
);
|
||||
}
|
||||
});
|
||||
// navigator.serviceWorker.ready.then((reg) => {
|
||||
// for (let i = 0; i < 20; i++) {
|
||||
// const thread = new SharedWorker($scramjet.config.thread, {
|
||||
// name: "thread" + i,
|
||||
// });
|
||||
//
|
||||
// reg.active.postMessage(
|
||||
// {
|
||||
// scramjet$type: "add",
|
||||
// handle: thread.port,
|
||||
// },
|
||||
// [thread.port]
|
||||
// );
|
||||
// }
|
||||
// });
|
||||
|
||||
navigator.serviceWorker.onmessage = ({ data }) => {
|
||||
if (data.scramjet$type === "getLocalStorage") {
|
||||
|
@ -133,6 +133,8 @@ function App() {
|
|||
}
|
||||
`;
|
||||
|
||||
let frame = scramjet.createFrame();
|
||||
|
||||
return html`
|
||||
<div>
|
||||
<h1>Percury Unblocker</h1>
|
||||
|
@ -165,8 +167,8 @@ function App() {
|
|||
<button on:click=${() => window.open(this.urlencoded)}>open in fullscreen</button>
|
||||
</div>
|
||||
</div>
|
||||
<input class="bar" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && console.log((this.urlencoded = $scramjet.config.prefix + $scramjet.codec.encode(e.target.value)))}></input>
|
||||
<iframe src=${use(this.urlencoded)}></iframe>
|
||||
<input class="bar" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && frame.go(e.target.value)}></input>
|
||||
${frame.frame}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
@ -175,8 +177,8 @@ window.addEventListener("load", async () => {
|
|||
document.body.appendChild(h(App));
|
||||
function b64(buffer) {
|
||||
let binary = "";
|
||||
let bytes = new Uint8Array(buffer);
|
||||
let len = bytes.byteLength;
|
||||
const bytes = new Uint8Array(buffer);
|
||||
const len = bytes.byteLength;
|
||||
for (let i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue