Google Maps Authuser Modifier

Remembers your Google Maps user

目前为 2024-02-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Google Maps Authuser Modifier
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Remembers your Google Maps user
// @author       JKamsker
// @match        *://www.google.at/maps*
// @grant        none
// @run-at       document-start
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  function getAuthUserId() {
    // Check if the authuserId is stored in localStorage
    let authuserId = localStorage.getItem("authuserId");
    // If not present or user wants to change it (by adding ?changeAuthUser=true in the URL)
    let urlParams = new URLSearchParams(window.location.search);
    if (!authuserId || urlParams.get("changeAuthUser") === "true") {
      // Prompt the user to enter their authuserId
      authuserId = prompt(
        "Please enter your authuserId for Google Maps:",
        authuserId || ""
      );
      if (authuserId) {
        // If provided, save it
        localStorage.setItem("authuserId", authuserId);
      } else {
        // If not provided, use a default or exit
        alert("authuserId not set. Using default or existing value.");
        return null;
      }
    }
    return authuserId;
  }

  function ensureAuthUser() {
    let url = new URL(window.location.href);
    // if (url.searchParams.get('authuser') !== authuserId) {
    //     url.searchParams.set('authuser', authuserId);
    //     window.location.href = url.toString();
    // }
    let authUserIdIsSet = url.search.indexOf("authuser=") !== -1;

    // Only set if not already set
    if (!authUserIdIsSet) {
      let authuserId = getAuthUserId();

      if (!authuserId) {
        return;
      }

      url.search += (url.search ? "&" : "?") + "authuser=" + authuserId;
      window.location.href = url.toString();
    }

    // If the URL has a different authuser than the one stored in localStorage, update the localStorage value
    if (authUserIdIsSet) {
      let authuserId = url.searchParams.get("authuser");
      let storedAuthuserId = localStorage.getItem("authuserId");
      if (authuserId !== storedAuthuserId) {
        localStorage.setItem("authuserId", authuserId);
      }
    }
  }

  // Run the function on script load
  ensureAuthUser();

  window.addEventListener("popstate", function (event) {
    ensureAuthUser();
  });

  const originalPushState = history.pushState;
  const originalReplaceState = history.replaceState;

  history.pushState = function () {
    originalPushState.apply(this, arguments);
    ensureAuthUser();
  };

  history.replaceState = function () {
    originalReplaceState.apply(this, arguments);
    ensureAuthUser();
  };
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址