Nitrome Auto Flash Load

Automatically bypass the "FLASH IS DEAD" propaganda message that Nitrome added to all their Flash games in 2021. The game will automatically load instead (faster and without the google tracking stub too!)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Nitrome Auto Flash Load
// @namespace   TiberiumFusion
// @include     http*://www.nitrome.com/*
// @include     http*://www.nitrome.com/games/preloader_frame_*.php
// @run-at      document-end
// @grant       none
// @version     1.0.0
// @author      TiberiumFusion
// @description Automatically bypass the "FLASH IS DEAD" propaganda message that Nitrome added to all their Flash games in 2021. The game will automatically load instead (faster and without the google tracking stub too!)
// @license     MIT
// @icon        https://cdn.nitrome.com/styles/images/flash_sprite.png
// ==/UserScript==


// This script has been tested to work in the following environments:
// - Firefox 52 with ViolentMonkey 2.14.0
// - Pale Moon 34 with GreaseMonkey 3.34.1
// - Chromium 109 with Tampermonkey 5.1.1


(function() {
  
  
  var ShowTraces = false;
  function Trace()
  {
    if (ShowTraces) {
     console.log.apply(null, arguments); }
  }
  
  
  function Main()
  {
    // Simple way to check if we're the inner preloader frame (not the outer nitrome.com frame)
    var isTargetFrame = (
         (window.writeFlashGame instanceof Function)    // true in Firefox 52 (ViolentMonkey 2.14.0), but false in Pale Moon 34 (GreaseMonkey 3.34.1) and Chromium 109 (Tampermonkey 5.1.1)
      || (typeof window.writeFlashGame === "function")  // true in all browsers but not a proper check, though javascript's motto has always been "idgaf at all about being proper" anyways
    );
    
    Trace("### Frame match check: ",
      window.location.href,
      isTargetFrame,
      (window.writeFlashGame instanceof Function),
      (typeof window.writeFlashGame)
    );
    
    if (isTargetFrame)
    {
      Trace("### Starting game load with writeFlashGame()");
      
      window.writeFlashGame(false);
      // That's all there is to it.
      // At this point, the game will load for normal browsers (Firefox, Pale Moon, etc).
      //
      // The preloader iframe document has a bunch of ugly yet perfectly readable and exposed code, which sets up the 3 button choices of "html5" (ew), "supernova" (ick), and the very deliberately hidden "use flash anyways".
      // Each button's onclick action is a window bound function, and the one to replace the propaganda message with the actual swfobject frame is writeFlashGame().
      //
      // Interestingly, writeFlashGame() takes one parameter, named "use_google". When true, the swf that gets loaded is not the actual game (or game preloader). Instead, it seems to be some google tracking cancer that adds a nice 15 seconds to the game's load time to do absolutely nothing of value.
      // The preloader frame always calls writeFlashGame() with use_google=true, so we take this opportunity to call writeFlashGame(false) instead and make things better.
      //
      // If it wasn't for browsers and javascript being the absolute worst thing on the planet, this entire userscript could just be a single line of code:
      //   if (window.writeFlashGame instanceof Function) { window.writeFlashGame(); }
      
      
      // In chromium (ew) however, the game will NOT load.
      // - For whatever reason, when we call writeFlashGame(), nothing happens. Clicking on the button which itself simply calls writeFlashGame() *does* work. It is not clear why chromium fails so hard at this. Maybe swfobject is choking or bailing early in chromium exclusively.
      //
      // Thankfully, there is another code path in the preloader frame we can leverage: loadGameFlashHandler(), which simply changes window.location from preloader_frame_x.x.x.php to preloader_flash.php, with all parameters kept. And chromium doesn't utterly fail at this one, so it works. It's not clear why.
      // - Note that this is NOT what the "use flash anyways" button does, though, so there is no guarantee this will work as intended.
      //
      // But first we need to detect if the intended path (writeFlashGame()) failed.
      // - writeFlashGame() calls swfobject.embedSWF(), and the target element is the one and only element in the <body>, which is a <div> named "game_object"
      // - If game_object is still a <div> (and not an <object>, <embed>, etc), then writeFlashGame() did not succeed.
      
      // swfobject.embedSWF() is not synchronous, so we'll sin with a delayed check of reasonable length
      setTimeout(function() {
        var gameObject = document.getElementById("game_object");
        Trace("### Post writeFlashGame() check on game_object:", gameObject);
        
        if (gameObject != null && gameObject.tagName.toLowerCase() == "div")
        {
          Trace("### game_object is not the expected swf embed; running backup game load with loadGameFlashHandler()");
          window.loadGameFlashHandler();
        }
      }, 200);
      
      
      // Side note: clicking the supernova button in the preloader frame causes major issues.
      // - When the user clicks the "supernova" button, the preloader frame writes a dangerous value into LocalStorage.
      // - On subsequent page loads, the preloader will see this value and immediately skip the propaganda message and go straight to the supernova loader every single time (and never give the user a way to unset this value), which we obviously don't want.
      // - Calling writeFlashGame() is sufficient to overwrite the supernova loader, but unfortunately the user retains this poisoined value in their LocalStorage for nitrome.com, which they must manually clear through the browser devtools. Obviously few will be comfortable doing that.
    }
  }
  
  
  if (document.readyState != "loading") // readyState becomes "complete" in Firefox and Pale Moon, but always stays "interactive" in chromium
  {
    Trace("### Document already ready at userscript entry");
    Main();
  }
  else
  {
    Trace("### Document not ready at userscript entry; state: ", document.readyState);
    document.addEventListener("DOMContentLoaded", function() {
      Trace("### @DOMContentLoaded: ", document.readyState);
      if (document.readyState != "loading")
      {
        Trace("### Document finally ready");
        Main();
      }
    });
  }
  
  
})();