WME Sandbox Fallback

Whenever the Waze features can't be loaded due to a server error, attempt to load the same data from the sandbox.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        WME Sandbox Fallback
// @author      Tom 'Glodenox' Puttemans
// @namespace   http://www.tomputtemans.com/
// @description Whenever the Waze features can't be loaded due to a server error, attempt to load the same data from the sandbox.
// @include     /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor.*$/
// @version     0.2
// @grant       none
// ==/UserScript==

(function() {
  var notificationMessage = document.createElement('div');
  notificationMessage.style.cssText = 'left:400px; bottom:32px; display:none; position:fixed; z-index:1000; background-color:#ffeb9c; padding:6px; border-radius:5px';
  notificationMessage.textContent = 'Had to fall back to sandbox mode to retrieve data';
  document.body.appendChild(notificationMessage);

  // Replace the send method with a function that adds a listener to the load event
  // This way we can monitor the results
  var originalFetch = window.fetch;
  window.fetch = (resource, init) => {
    return originalFetch(resource, init)
    .then(response => {
      var resourceUrl = typeof(resource) == 'string' ? resource : resource.url;
      if (response.status == 500 && resourceUrl.indexOf('/app/Features') != -1) {
        return originalFetch(resourceUrl + '&sandbox=true', init)
          .then(response => {
            if (response.ok) {
              notificationMessage.style.display = 'block';
              setTimeout(() => notificationMessage.style.display = 'none', 5000);
            }
            return response;
          });
      } else {
        return response;
      }
    });
  };
  log('WME Sandbox Fallback initiated');

  function log(message) {
    if (typeof message === 'string') {
      console.log('%c' + GM_info.script.name + ' (v' + GM_info.script.version + '): %c' + message, 'color:black', 'color:#d97e00');
    } else {
      console.log('%c' + GM_info.script.name + ' (v' + GM_info.script.version + ')', 'color:black', message);
    }
  }
})();