WME Reload Map Position Fix

Keeps track of the current map center and zoom and restores it upon reloading.

当前为 2020-09-09 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            WME Reload Map Position Fix
// @namespace       https://greasyfork.org/users/166843
// @description     Keeps track of the current map center and zoom and restores it upon reloading.
// @version         2020.09.08.01
// @author          dBsooner
// @grant           none
// @require         https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @license         GPLv3
// @include         /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @contributionURL https://github.com/WazeDev/Thank-The-Authors
// ==/UserScript==

/* global window, localStorage, sessionStorage, W, $, OpenLayers */

const SETTINGS_STORE_NAME = 'WME_RMPF';

let _bootstrapTimeout;

function updatedSavedMapPosition() {
    const storage = { savedCenter: W.map.getCenter(), savedZoom: W.map.getZoom() };
    localStorage.setItem(SETTINGS_STORE_NAME, JSON.stringify(storage));
    sessionStorage.setItem(SETTINGS_STORE_NAME, JSON.stringify(storage));
}

function init() {
    const loadedSettings = $.parseJSON(sessionStorage.getItem(SETTINGS_STORE_NAME)) || $.parseJSON(localStorage.getItem(SETTINGS_STORE_NAME)),
        savedCenter = (loadedSettings) ? loadedSettings.savedCenter : W.map.getCenter(),
        savedZoom = (loadedSettings) ? loadedSettings.savedZoom : W.map.getZoom(),
        currCenter = W.map.getCenter(),
        currZoom = W.map.getZoom();
    if ((currCenter.lon !== savedCenter.lon) || (currCenter.lat !== savedCenter.lat) || (savedZoom !== currZoom))
        W.map.getOLMap().moveTo(new OpenLayers.LonLat([savedCenter.lon, savedCenter.lat], savedZoom));
    W.map.events.register('zoomend', null, updatedSavedMapPosition);
    W.map.events.register('moveend', null, updatedSavedMapPosition);
    window.addEventListener('beforeunload', updatedSavedMapPosition, false);
}

function bootstrap(tries) {
    if (W && W.map && $) {
        window.clearTimeout(_bootstrapTimeout);
        _bootstrapTimeout = undefined;
        init();
    }
    else if (tries < 1000) {
        console.log(`RMPF: Bootstrap failed. Retrying ${tries} of 1000`);
        _bootstrapTimeout = window.setTimeout(bootstrap, 200, ++tries);
    }
    else {
        console.error('RMPF: Bootstrap timed out waiting for WME to become ready.');
    }
}

bootstrap(1);