WME Reload Map Position Fix

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

目前為 2021-09-01 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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         2021.09.01.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, WazeWrap */

/* Changelog:                                                                           *
 *  2020.09.08.01: Initial release.                                                     *
 *  2020.09.09.01: Check for URL parameters.                                            *
 *  2021.09.01.01: Update to latest WME zoom level changes.                             *
 *                                                                                      */

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(),
        urlParams = new URLSearchParams(window.location.search);
    if (!urlParams.get('lon') && !urlParams.get('lat') && !urlParams.get('zoomLevel') && ((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 && $ && WazeWrap.Ready) {
        window.clearTimeout(_bootstrapTimeout);
        _bootstrapTimeout = undefined;
        init();
    }
    else if (tries < 1000) {
        console.log(`RMPF: WME not ready. 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);