From 8f0b2a5ab86bc788ef53517b3a5a2ec6a6c3fed1 Mon Sep 17 00:00:00 2001 From: rektrex Date: Thu, 19 Sep 2019 22:14:51 +0530 Subject: [PATCH] add exmcmds to load RC file from url --- src/background/config_rc.ts | 19 +++++++++++++++++++ src/excmds.ts | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/background/config_rc.ts b/src/background/config_rc.ts index f1696619..95101b20 100644 --- a/src/background/config_rc.ts +++ b/src/background/config_rc.ts @@ -13,6 +13,25 @@ export async function source(filename = "auto") { return true } +async function fetchConfig(url: string) { + const response = await fetch(url) + const reader = response.body.getReader() + let rctext = "" + const decoder = new TextDecoder("utf-8") + while (true) { + const { value: chunk, done: isDone } = await reader.read() + if (isDone) return rctext + rctext += decoder.decode(chunk) + } +} + +export async function sourceFromUrl(url: string) { + const rctext = await fetchConfig(url) + if (!rctext) return false + await runRc(rctext) + return true +} + export async function writeRc(conf: string, force = false, filename = "auto") { let path: string if (filename === "auto") { diff --git a/src/excmds.ts b/src/excmds.ts index c354798a..117b518e 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -751,6 +751,26 @@ export async function source_quiet(...fileArr: string[]) { } } +/** Use tridactylrc located at the given url + * @param url the url where the raw version of tridactylrc can be found + */ +//#background +export async function source_from_url(url: string) { + if (!url) return + await rc.sourceFromUrl(url) +} + +/** + * Same as [[source_from_url]] but suppresses all errors + */ +//#background +export async function source_from_url_quiet(url: string) { + if (!url) return + try { + await rc.sourceFromUrl(url) + } catch (e) {} +} + /** * Updates the native messenger if it is installed, using our GitHub repo. This is run every time Tridactyl is updated. *