{"size": 4211, "ext": "js", "max_stars_repo_path": "src/bootstrap.js", "max_stars_repo_name": "ppixiv/ppixiv", "max_stars_repo_stars_event_min_datetime": "2018-09-23T19:51:40.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T02:47:43.000Z", "max_issues_repo_path": "src/bootstrap.js", "max_issues_repo_name": "ppixiv/ppixiv", "max_issues_repo_issues_event_min_datetime": "2018-10-01T18:51:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-27T02:56:09.000Z", "max_forks_repo_path": "src/bootstrap.js", "max_forks_repo_name": "ppixiv/ppixiv", "max_forks_repo_forks_event_min_datetime": "2019-10-06T11:00:18.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-28T23:10:11.000Z"}
{"max_stars_count": 57, "max_issues_count": 34, "max_forks_count": 4, "avg_line_length": 34.5163934426, "max_line_length": 98, "alphanum_fraction": 0.5252909048}
// Note that this file doesn't use strict, because JS language developers remove
// useful features without a second thought.  "with" may not be used often, but
// it's an important part of the language.

(() => {
    // If we're in a release build, we're inside
    // (function () {
    //     with(this)
    //     {
    //         ...
    //     }
    // }.exec({});
    //
    // The empty {} object is our environment.  It can be assigned to as "this" at the
    // top level of scripts, and it's included in scope using with(this) so it's searched
    // as a global scope.
    //
    // If we're in a debug build, this script runs standalone, and we set up the environment
    // here.
    
    // Our source files are stored as text, so we can attach sourceURL to them to give them
    // useful filenames.  "this" is set to the ppixiv context, and we load them out here so
    // we don't have many locals being exposed as globals during the eval.  We also need to
    // do this out here in order ot use with.
    let _load_source_file = function(__pixiv, __source) {
        const ppixiv = __pixiv;
        with(ppixiv)
        {
            return eval(__source);
        }
    };

    new class
    {
        constructor(env)
        {
            // If this is an iframe, don't do anything.
            if(window.top != window.self)
                return;

            // Don't activate for things like sketch.pixiv.net.
            if(window.location.hostname != "www.pixiv.net")
                return;

            console.log("ppixiv bootstrap");

            // If env is the window, this script was run directly, which means this is a
            // development build and we need to do some extra setup.  If this is a release build,
            // the environment will be set up already.
            if(env === window)
                this.devel_setup();
            else
                this.env = env;

            this.launch();
        }

        devel_setup()
        {
            // In a development build, our source and binary assets are in @resources, and we need
            // to pull them out into an environment manually.
            let env = {};
            env.resources = {};
        
            env.resources["output/setup.js"] = JSON.parse(GM_getResourceText("output/setup.js"));
            let setup = env.resources["output/setup.js"];
            let source_list = setup.source_files;

            // Add the file containing binary resources to the list.
            source_list.unshift("output/resources.js");

            for(let path of source_list)
            {
                // Load the source file.
                let source = GM_getResourceText(path);
                if(source == null)
                {
                    // launch() will show an error for this, so don't do it here too.
                    continue;
                }

                // Add sourceURL to each file, so they show meaningful filenames in logs.
                // Since we're loading the files as-is and line numbers don't change, we
                // don't need a source map.
                //
                // This uses a path that pretends to be on the same URl as the site, which
                // seems to be needed to make VS Code map the paths correctly.
                source += "\n";
                source += `//# sourceURL=${document.location.origin}/ppixiv/${path}\n`;

                env.resources[path] = source;
            }

            this.env = env;
        }

        launch()
        {
            let setup = this.env.resources["output/setup.js"];
            let source_list = setup.source_files;
            unsafeWindow.ppixiv = this.env;

            // Load each source file.
            for(let path of source_list)
            {
                let source = this.env.resources[path];
                if(!source)
                {
                    console.error("Source file missing:", path);
                    continue;
                }

                _load_source_file(this.env, source);
            }

            // Create the main controller.
            this.env.main_controller.launch();
        }
    }(this);
})();

