// Early setup. If we're running in a user script, this is the entry point for regular // app code that isn't running in the script sandbox, where we interact with the page // normally and don't need to worry about things like unsafeWindow. // // If we're running on Pixiv, this checks if we want to be active, and handles adding the // the "start ppixiv" button. If the app is running, it starts it. This also handles // shutting down Pixiv's scripts before we get started. // // For vview, this is the main entry point. class AppStartup { constructor({env, rootUrl}) { this.initialSetup({env, rootUrl}); } // We can either be given a startup environment, or a server URL where we can fetch one. // If we're running in a user script then the environment is packaged into the script, and // if we're running on vview or a user script development environment we'll have a URL. // We'll always be given one or the other. This lets us skip the extra stuff in bootstrap.js // when we're running natively, and just start directly. async initialSetup({env, rootUrl}) { let native = location.hostname != "pixiv.net" && location.hostname != "www.pixiv.net"; let ios = navigator.platform.indexOf('iPhone') != -1 || navigator.platform.indexOf('iPad') != -1; let android = navigator.userAgent.indexOf('Android') != -1; let mobile = ios || android; // If we weren't given an environment, fetch it from rootUrl. if(env == null) { if(rootUrl == null) { alert("Unexpected error: no environment or root URL"); return; } // init.js gives us the list of source and resource files to load. If we're running // natively, just fetch it normally. If we're running as a user script (this is used // for debugging), use a sync XHR to try to mimic the regular environment as closely // as possible. This avoids going async and letting page scripts run. let url = new URL("/vview/init.js", rootUrl); let request = await fetch(url); env = await request.json(); } if(window.ppixiv) { // Make sure that we're not loaded more than once. This can happen if we're installed in // multiple script managers, or if the release and debug versions are enabled simultaneously. console.error("ppixiv has been loaded twice. Is it loaded in multiple script managers?"); return; } // Set up the global object. window.ppixiv = { resources: env.resources, version: env.version, native, mobile, ios, android, }; console.log(`${native? "vview":"ppixiv"} setup`); console.log("Browser:", navigator.userAgent); // "Stay" for iOS leaves a