Skip to content
  • autosave no longer reads family size/count shows up as ", ? Family" and save time always is in massive drawn out zulu time format instead of basegame "x seconds/minutes/hours/days/weeks/months ago" ie "2024-11-30T15:35:55.977Z" instead of "3 minutes ago"

  • Author Developer

    these aren't really maintained as I don't play much anymore, welcome to make modifications as needed

  • i fixed both issues i complained about nearly a year ago :P

    
    // ==UserScript==
    // @name         Zeke's AutoSave
    // @version      1.5.5
    
    // @description  Autosaves when you sleep
    // @author       @wren:therian.dev
    
    // @match        https://badtraining.quest/story/trainer1*
    // @match        https://badtraining.xyz/story/trainer1*
    // @match        http://twvcssnikbibzwjmadkdzrnof7crtjiu3tqjeidhkepizcf66sograad.onion/story/trainer1*
    // @match        https://bt.lewd.dad/story/trainer1*
    
    // @icon         https://badtraining.quest/favicon.ico
    // @source       https://git.allthefallen.moe/voidsquad/ttuac/-/snippets/36/raw/main/autosave.user.js
    // @supportURL   https://git.allthefallen.moe/voidsquad/ttuac/-/issues
    // @grant        none
    // ==/UserScript==
    /* globals $ after_render_page STORAGE_KEY localforage bookmark esc_html */
    
    let saveFunction = () => {
      console.debug("Injected AutoSave!");
      const handlePageRender = async (page) => {
        if (page?.woke) {
          const currentDay = `Day #${page.woke}`;
          console.debug({ currentPage: page, currentDay });
    
          try {
            const currentLocalSaves = await localforage.getItem(STORAGE_KEY);
    
            if (typeof currentLocalSaves !== 'object') {
              alert('Gave up trying to autosave to protect saves, please check your saves are backed up (to server) before reloading the page!');
              return;
            }
    
            // Pull Family from characters[0].insights if available
            let familyText = 0; // default fallback
    
            if (page.characters && page.characters[0] && page.characters[0].insights) {
                const familyValue = page.characters[0].insights.Family; // e.g., "14 people"
                if (typeof familyValue === 'string') {
                    const match = familyValue.match(/\d+/);
                    familyText = match ? Number(match[0]) : 0;
                } else if (typeof familyValue === 'number') {
                    familyText = familyValue;
                }
            }
    
            const autoSave = {
              date: Date.now(),
              label: `${currentDay} (${page.meta.Funds}, ${familyText} Family)`,
              stored_data: page
            };
    
            currentLocalSaves.push(autoSave);
    
            const bookmarkStorage = await localforage.setItem(STORAGE_KEY, currentLocalSaves);
    
            $('#client_bookmarks').html('');
            for (let i = bookmarkStorage.length - 1; i >= 0; i--) {
              const g = bookmarkStorage[i];
              const d = new Date(g.date);
              $('#client_bookmarks').append(
                `<li class="list-group-item text-bg-dark" data-saved-game-ix="${i}">
                  <div><strong>${esc_html(g.label)}</strong> <small style="font-size:75%"><span class="timeago" title="${d.toISOString()}">${d.toISOString()}</span></small></div>
                  <div>
                    <small><a class="text-primary saved-game-go" href="#">Go here</a></small> &middot;
                    <small><a class="text-success saved-game-save" href="#">Save</a></small> &middot;
                    <small><a class="text-danger saved-game-delete" href="#">Delete</a></small>
                  </div>
                </li>`
              );
              // Immediately update just-added timeago spans
              $('#client_bookmarks li:last .timeago').each(function() {
                $(this).timeago();
              });
            }
            if (typeof $.fn.timeago === 'function') {
              $("span.timeago").timeago();
            }
            return true;
          } catch (error) {
            console.error('Error during autosave:', error);
          }
        }
        return false
      };
    
      const originalAfterRenderPage = after_render_page;
      window.after_render_page = (page) => {
        handlePageRender(page).then(hasSaved => {
          if(hasSaved) {
              let main = document.querySelector("main");
              main.innerHTML = "<p class='text-success' style='font-weight:bold;'>- - Saved the game! - -</p>\n" + main?.innerHTML
          }
        }).finally(()=>{
            return originalAfterRenderPage(page);
        })
      };
    };
    
    if(document.readyState == "complete") saveFunction();
    else {
    document.addEventListener("readystatechange", () => {
        if(document.readyState == "complete") saveFunction();
    });
    }
    
    Edited by Uzr 4994
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment