Skip to content

Instantly share code, notes, and snippets.

@MartinBspheroid
Created September 2, 2020 12:02
Show Gist options
  • Save MartinBspheroid/f50ba761b491244fedbda6d2e6af19e6 to your computer and use it in GitHub Desktop.
Save MartinBspheroid/f50ba761b491244fedbda6d2e6af19e6 to your computer and use it in GitHub Desktop.

Revisions

  1. MartinBspheroid created this gist Sep 2, 2020.
    43 changes: 43 additions & 0 deletions fetcher.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@


    class Fetcher {
    constructor(scenesObj, startScene = 0) {
    this.scenes = scenesObj;
    this.currentScene = startScene
    this.init()

    }
    init() {
    this.target = document.createElement("div")
    this.target.id = "stage"
    document.body.appendChild(this.target)
    this.loadScene(this.scenes[this.currentScene].page, this.scenes[this.currentScene].script);

    }
    loadScene(path, jsPath, ) {

    fetch(path /*, options */)
    .then((response) => response.text())
    .then((html) => {
    this.target.innerHTML = html;
    let newScript = document.createElement("script");
    newScript.src = jsPath;
    this.target.appendChild(newScript);
    })
    .catch((error) => {
    console.warn(error);
    });


    }
    loadSceneNumber(num){
    this.loadNextScene(this.scenes[num].page, this.scenes[num].script)
    }
    loadNextScene() {
    this.currentScene += 1
    this.loadScene(this.scenes[this.currentScene].page, this.scenes[this.currentScene].script, this.scenes[this.currentScene]?.req);
    console.log(this.scenes[this.currentScene])
    }

    }