ZG_GoogleMapsInit

Waits for the Google Maps scripts to be fully initialized

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/524179/1523058/ZG_GoogleMapsInit.js

// ==UserScript==
// @exclude *
// ==UserLibrary==
// @name        ZG_GoogleMapsInit
// @description Waits for the Google Map script to be fully initialized
// @version     1.0.0
// @license     MIT
// @author      zecageo
// @namespace   https://gf.qytechs.cn/users/1340965
// ==/UserLibrary==
// ==/UserScript==

window.googleMapsInit = new Promise(resolve => {

  // Watch <head> and <body> for the Google Maps script to be added
  let scriptObserver = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      for (const newNode of mutation.addedNodes) {
        if (newNode.tagName === "SCRIPT" && newNode.scr && newNode.src.startsWith('https://maps.googleapis.com/')) {
          // When it’s been added and loaded, load the script below.
          newNode.addEventListener('load', () => resolve()); // jshint ignore:line
          if (scriptObserver) scriptObserver.disconnect();
          scriptObserver = undefined;
        }
      }
    }
  });

  // Wait for the head and body to be actually added to the page, applying the
  // observer above to these elements directly.
  // There are two separate observers because only the direct children of <head>
  // and <body> should be watched, but these elements are not necessarily
  // present at document-start.
  let bodyDone = false;
  let headDone = false;
  new MutationObserver((_, observer) => {
    if (!bodyDone && document.body) {
      bodyDone = true;
      if (scriptObserver) scriptObserver.observe(document.body, {
        childList: true
      });
    }
    if (!headDone && document.head) {
      headDone = true;
      if (scriptObserver) scriptObserver.observe(document.head, {
        childList: true
      });
    }
    if (headDone && bodyDone) observer.disconnect();
  }).observe(document.documentElement, {
    childList: true,
    subtree: true,
  });
});

QingJ © 2025

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