WME Fix Map Object

Temporary fix for the changes made in the WME internal data structure that breaks any script that interacts with the map

目前為 2019-12-04 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        WME Fix Map Object
// @namespace   http://www.tomputtemans.com/
// @description Temporary fix for the changes made in the WME internal data structure that breaks any script that interacts with the map
// @include     /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor.*$/
// @version     1.0.0
// @grant       none
// ==/UserScript==

/* global W */

(function() {
    'use strict';

    var recoveredProperties = new Map();

    function init() {
        if (typeof W === 'undefined' ||
            typeof W.map === 'undefined' ||
            typeof W.map.olMap === 'undefined') {
            setTimeout(init, 100);
            return;
        }
        W.map = new Proxy(W.map, {
            'get': function(target, property) {
                checkProperty(property);
                return Reflect.get(...arguments);
            }
        });
        recoverProperties();
        console.log("Fix Map Object: The following properties have been recovered: ", Array.from(recoveredProperties.keys()).sort());
    }
    init();

    function recoverProperties() {
        // Go through all properties, including the prototype chain
        for (var mapProperty in W.map.getOLMap()) {
            if (!W.map[mapProperty]) {
                recoveredProperties.set(mapProperty, 0);
                W.map[mapProperty] = W.map.getOLMap()[mapProperty];
            }
        }
    }

    function checkProperty(propertyName) {
        if (recoveredProperties.has(propertyName)) {
            var timesCalled = recoveredProperties.get(propertyName);
            if (timesCalled >= 10) {
                return;
            }
            console.groupCollapsed("Fix Map Object: Removed property W.map." + propertyName + " accessed. Expand for stack trace.");
            console.trace();
            console.groupEnd();
            if (timesCalled == 9) {
                console.warn("Fix Map Object: No longer reporting on removed property W.map." + propertyName + " (limit reached)");
            }
            recoveredProperties.set(propertyName, timesCalled + 1);
        }
    }
})();