Big Aspirations (0.7.4) A weight gain interactive story

I made a save as file functionality button for this game, I originally created this as a userscript for personal use (because I love this game), I thought it would be good to show you.

to use it, just put this inside of output html, inside head tag after title.

<head>
    <title>Big Aspirations WIP</title>
    <!-- here -->
</head>

If you want some modifications, I’ll fix it!
currently it only works well only “a new day starts” screen.
it shows on every screen for now, but I can hide it if you want.

<script>
{

    const realSessionStorage = window.sessionStorage;
    let sessionCache = {};

    Object.defineProperty(window, 'sessionStorage', {
        get: () => {
            return {
                getItem: (key) => {
                    return realSessionStorage.getItem(key);
                },
                setItem: (key, value) => {
                    sessionCache[key] = value;
                    console.log(sessionCache)
                    return realSessionStorage.setItem(key, value);
                },
                removeItem: (key) => {
                    delete sessionCache[key];
                    return realSessionStorage.removeItem(key);
                },
                clear: () => {
                    sessionCache = {};
                    return realSessionStorage.clear();
                },
                key: (index) => {
                    return realSessionStorage.key(index);
                }
            }   
        }

    })


    function styleDom(dom){
        dom.style.position = 'absolute';
        dom.style.right = '5px';
        dom.style.cursor = 'pointer';
        dom.style.zIndex = '99'
        dom.style.color = 'white';
        dom.style.backgroundColor = 'black';
        dom.style.padding = '5px';
        dom.style.borderRadius = '5px';
        dom.style.border = '1px solid white';
        dom.style.fontSize = '12px';
    }

    function loadSaveDom(){
        {
            const saveDom = document.createElement('div');
            saveDom.style.top = '5px';
            styleDom(saveDom);
            saveDom.innerText = 'save';
            document.body.appendChild(saveDom);
            saveDom.addEventListener('click', () => {
                //get all sessionStorage keys
                const keys = Object.keys(sessionCache);

                //create object to store all sessionStorage keys and values
                const storage = {};

                //loop over keys and add to storage object
                keys.forEach(key => {
                    storage[key] = sessionCache[key];
                })

                //save storage object to json file
                const storageString = JSON.stringify(storage);
                const storageBlob = new Blob([storageString], {type: 'application/json'});
                const storageUrl = URL.createObjectURL(storageBlob);
                const storageLink = document.createElement('a');

                //download json file
                storageLink.href = storageUrl;
                storageLink.download = 'save.json';
                storageLink.click();

            })

            const loadDom = document.createElement('div');
            loadDom.style.top = '35px';
            styleDom(loadDom);
            loadDom.innerText = 'load';
            document.body.appendChild(loadDom);
            loadDom.addEventListener('click', () => {
                const fileInput = document.createElement('input');
                fileInput.type = 'file';
                fileInput.accept = 'application/json';
                fileInput.click();
                fileInput.addEventListener('change', () => {
                    const file = fileInput.files[0];
                    const reader = new FileReader();
                    reader.readAsText(file);
                    reader.addEventListener('load', () => {
                        const storageString = reader.result;
                        const storage = JSON.parse(storageString);
                        const keys = Object.keys(storage);
                        keys.forEach(key => {
                            sessionStorage.setItem(key, storage[key]);
                        })
                        window.location.reload();
                    })
                })
            })
        }
    }

    setTimeout(loadSaveDom, 100)
}
</script>
7 Likes