From d5319922204c35606fb5763b76584ee8e7ce1d6a Mon Sep 17 00:00:00 2001 From: ebolam Date: Sun, 30 Oct 2022 19:10:31 -0400 Subject: [PATCH] Scroll Fix and offline story downloader --- static/koboldai.js | 63 ++++++++++++++++++++++++++++++++-- templates/settings flyout.html | 2 +- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/static/koboldai.js b/static/koboldai.js index 02fe9e59..3f92dc1d 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -2376,6 +2376,63 @@ function process_log_message(full_data) { } //--------------------------------------------UI to Server Functions---------------------------------- +async function download_story_to_json() { + //document.getElementById('download_iframe').src = 'json'; + try { + let r = await fetch("json"); + let j = await r.json(); + downloadString(JSON.stringify(j), j['story_name']+".json") + console.log("Got JSON"); + } + catch(err) { + //first we're going to find all the var_sync_story_ classes used in the document. + let allClasses = []; + const allElements = document.querySelectorAll('*'); + + for (let i = 0; i < allElements.length; i++) { + let classes = allElements[i].classList; + for (let j = 0; j < classes.length; j++) { + if (!(allClasses.includes(classes[j].replace("var_sync_story_", ""))) && (classes[j].includes("var_sync_story_"))) { + allClasses.push(classes[j].replace("var_sync_story_", "")); + } + } + } + + //OK, now we're going to go through each of those classes and get the values from the elements + let j = {} + for (class_name of allClasses) { + for (item of document.getElementsByClassName("var_sync_story_"+class_name)) { + if (['INPUT', 'TEXTAREA', 'SELECT'].includes(item.tagName)) { + if ((item.tagName == 'INPUT') && (item.type == "checkbox")) { + j[class_name] = item.checked; + } else { + j[class_name] = item.value; + } + } else { + j[class_name] = item.textContent; + } + break; + } + } + + //We'll add actions and world info data next + let temp = JSON.parse(JSON.stringify(actions_data)); + delete temp[-1]; + j['actions'] = {'action_count': document.getElementById('action_count').textContent, 'actions': temp}; + j['worldinfo_v2'] = {'entries': world_info_data, 'folders': world_info_folder_data}; + + //Biases + + //substitutions + + j['file_version'] = 2; + j['gamestarted'] = true; + + downloadString(JSON.stringify(j), j['story_name']+".json") + console.log("used alt json"); + } +} + function unload_userscripts() { files_to_unload = document.getElementById('loaded_userscripts'); for (var i=0; i= 0){ - let item_in_view = true; + if ((scroll_trigger_element != undefined) && (scroll_trigger_element)) { + if(scroll_trigger_element.getBoundingClientRect().bottom >= 0){ + let item_in_view = true; + } } for (id of Object.keys(actions_data).map(Number).filter(function(x){return x>0}).sort(function(a, b) {return a - b;})) { //console.log("Checking for "+id); diff --git a/templates/settings flyout.html b/templates/settings flyout.html index dbb162dd..78d82939 100644 --- a/templates/settings flyout.html +++ b/templates/settings flyout.html @@ -60,7 +60,7 @@ - file_download + file_download