fix siteflags

This commit is contained in:
velzie 2024-10-13 15:05:55 -04:00
parent fd420cfda7
commit 8fc98e66c8
No known key found for this signature in database
GPG key ID: AA51AEFB0A1F3820
5 changed files with 36 additions and 12 deletions

View file

@ -2,7 +2,10 @@ pub mod rewrite;
use std::{panic, str::FromStr}; use std::{panic, str::FromStr};
use js_sys::{Function, Object, Reflect}; use js_sys::{
global, Array, Function, Object,
Reflect::{self, construct},
};
use rewrite::{rewrite, Config, EncodeFn}; use rewrite::{rewrite, Config, EncodeFn};
use url::Url; use url::Url;
use wasm_bindgen::{prelude::*, throw_str}; use wasm_bindgen::{prelude::*, throw_str};
@ -45,10 +48,24 @@ fn get_str(obj: &JsValue, k: &str) -> String {
Reflect::get(obj, &k.into()).unwrap().as_string().unwrap() Reflect::get(obj, &k.into()).unwrap().as_string().unwrap()
} }
fn get_config(scramjet: &Object) -> Config { fn get_flag(scramjet: &Object, url: &str, flag: &str) -> bool {
let urlconstructor = get_obj(&global(), "URL");
let args = Array::new();
args.push(&JsValue::from_str(url));
let url = construct(&urlconstructor.dyn_into::<Function>().unwrap(), &args).unwrap();
let fenabled = get_obj(scramjet, "flagEnabled")
.dyn_into::<Function>()
.unwrap();
return fenabled
.call2(&JsValue::NULL, &JsValue::from_str(flag), &url)
.unwrap()
.as_bool()
.unwrap();
}
fn get_config(scramjet: &Object, url: &str) -> Config {
let codec = &get_obj(scramjet, "codec"); let codec = &get_obj(scramjet, "codec");
let config = &get_obj(scramjet, "config"); let config = &get_obj(scramjet, "config");
let flags = &get_obj(config, "flags");
let globals = &get_obj(config, "globals"); let globals = &get_obj(config, "globals");
Config { Config {
@ -62,9 +79,9 @@ fn get_config(scramjet: &Object) -> Config {
setrealmfn: get_str(globals, "setrealmfn"), setrealmfn: get_str(globals, "setrealmfn"),
pushsourcemapfn: get_str(globals, "pushsourcemapfn"), pushsourcemapfn: get_str(globals, "pushsourcemapfn"),
do_sourcemaps: get_bool(flags, "sourcemaps"), do_sourcemaps: get_flag(scramjet, url, "sourcemaps"),
capture_errors: get_bool(flags, "captureErrors"), capture_errors: get_flag(scramjet, url, "captureErrors"),
scramitize: get_bool(flags, "scramitize"), scramitize: get_flag(scramjet, url, "scramitize"),
} }
} }
@ -85,7 +102,7 @@ pub fn rewrite_js(js: &str, url: &str, scramjet: &Object) -> Vec<u8> {
return Vec::new(); return Vec::new();
} }
rewrite(js, Url::from_str(url).unwrap(), get_config(scramjet)) rewrite(js, Url::from_str(url).unwrap(), get_config(scramjet, url))
} }
#[wasm_bindgen] #[wasm_bindgen]
@ -98,5 +115,5 @@ pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, scramjet: &Object) -> V
// we know that this is a valid utf-8 string // we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) }; let js = unsafe { std::str::from_utf8_unchecked(js) };
rewrite(js, Url::from_str(url).unwrap(), get_config(scramjet)) rewrite(js, Url::from_str(url).unwrap(), get_config(scramjet, url))
} }

View file

@ -192,7 +192,7 @@ export class ScramjetClient {
}); });
for (const module of modules) { for (const module of modules) {
if (!module.enabled || module.enabled()) if (!module.enabled || module.enabled(this))
module.default(this, this.global); module.default(this, this.global);
else if (module.disabled) module.disabled(this, this.global); else if (module.disabled) module.disabled(this, this.global);
} }

View file

@ -8,6 +8,7 @@ if (!("$scramjet" in self)) {
version: VERSION, version: VERSION,
}, },
codec: {}, codec: {},
flagEnabled,
}; };
} }

View file

@ -7,7 +7,7 @@ import {
rewrite_js, rewrite_js,
rewrite_js_from_arraybuffer, rewrite_js_from_arraybuffer,
} from "../../../rewriter/out/rewriter.js"; } from "../../../rewriter/out/rewriter.js";
import { $scramjet } from "../../scramjet"; import { $scramjet, flagEnabled } from "../../scramjet";
initSync({ initSync({
module: new WebAssembly.Module( module: new WebAssembly.Module(
@ -20,9 +20,10 @@ init();
Error.stackTraceLimit = 50; Error.stackTraceLimit = 50;
export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) { export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
if ($scramjet.config.flags.naiiveRewriter) { if (flagEnabled("naiiveRewriter", meta.origin)) {
const text = typeof js === "string" ? js : new TextDecoder().decode(js); const text = typeof js === "string" ? js : new TextDecoder().decode(js);
console.log("naiive");
return rewriteJsNaiive(text); return rewriteJsNaiive(text);
} }

View file

@ -6,6 +6,11 @@ const scramjet = new ScramjetController({
shared: "/scram/scramjet.shared.js", shared: "/scram/scramjet.shared.js",
sync: "/scram/scramjet.sync.js", sync: "/scram/scramjet.sync.js",
}, },
siteFlags: {
"https://discord.com/.*": {
naiiveRewriter: true,
},
},
}); });
scramjet.init("./sw.js"); scramjet.init("./sw.js");
@ -183,7 +188,7 @@ function App() {
if (!url) return; if (!url) return;
if (url === "about:blank") return; if (url === "about:blank") return;
this.url = $scramjet.codecs.plain.decode( this.url = $scramjet.codec.decode(
url.substring((location.href + "/scramjet").length) url.substring((location.href + "/scramjet").length)
); );
}); });