MediaWiki:Common.js: Difference between revisions

cleaned up some dead code from the mobile fix
Tag: Reverted
fixed the redirect not firing on its own on real phones
Tag: Reverted
Line 3: Line 3:
// The site auto-switches to MediaWiki's generic Minerva mobile skin based
// The site auto-switches to MediaWiki's generic Minerva mobile skin based
// on user-agent, which has none of the custom theme/nav and is a much
// on user-agent, which has none of the custom theme/nav and is a much
// worse experience than the (now mobile-responsive) desktop skin. This
// worse experience than the (now mobile-responsive) desktop skin.
// runs as early as possible to minimize the flash of the wrong skin before
// redirecting.
//
//
// The server does set a loop-guard cookie on the toggle ("stopMobileRedirect"),
// The server does set a loop-guard cookie on the toggle ("stopMobileRedirect"),
Line 11: Line 9:
// actually read it - that check would always be dead code. sessionStorage
// actually read it - that check would always be dead code. sessionStorage
// is the only guard we can use here, wrapped in its own try/catch so a
// is the only guard we can use here, wrapped in its own try/catch so a
// browser that blocks storage access (some in-app/embedded browsers do)
// browser that blocks storage access doesn't cancel the whole redirect.
// doesn't cancel the whole redirect and strand the user on Minerva.
//
// This used to bail out permanently if document.body wasn't available yet
// ("if (!document.body) return"), with no retry. Real-device testing (real
// Safari/Chrome on iOS - NOT reproducible by spoofing the user-agent string
// in a desktop Electron/Chromium test, since that never runs WebKit at all)
// showed the redirect worked fine when triggered manually via URL, but never
// fired on its own: this script runs before <body> exists in some real
// mobile browsers, so it needs to wait for DOMContentLoaded instead of
// giving up on the first check.
(function () {
(function () {
   try {
   function tryForceDesktop() {
    if (!document.body || !document.body.classList.contains('skin-minerva')) return;
    try {
    var alreadyTried = false;
      if (!document.body || !document.body.classList.contains('skin-minerva')) return;
    try { alreadyTried = !!sessionStorage.getItem('wovForcedDesktop'); } catch (e) {}
      var alreadyTried = false;
    if (alreadyTried) return;
      try { alreadyTried = !!sessionStorage.getItem('wovForcedDesktop'); } catch (e) {}
    try { sessionStorage.setItem('wovForcedDesktop', '1'); } catch (e) {}
      if (alreadyTried) return;
    var sep = window.location.search ? '&' : '?';
      try { sessionStorage.setItem('wovForcedDesktop', '1'); } catch (e) {}
    window.location.href = window.location.pathname + window.location.search + sep + 'mobileaction=toggle_view_desktop';
      var sep = window.location.search ? '&' : '?';
  } catch (e) {
      window.location.href = window.location.pathname + window.location.search + sep + 'mobileaction=toggle_view_desktop';
    // if even the basic checks are blocked, just leave the user on Minerva
    } catch (e) {
    // rather than risk breaking the rest of the site's scripts.
      // if even the basic checks are blocked, just leave the user on Minerva
      // rather than risk breaking the rest of the site's scripts.
    }
  }
  if (document.body) {
    tryForceDesktop();
  } else {
    document.addEventListener('DOMContentLoaded', tryForceDesktop);
   }
   }
})();
})();