WME Utils - Bootstrap

Adds a bootstrap function for easier startup of wmeSdk, WazeWrap, and ScriptUpdateMonitor.

目前为 2024-09-22 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/509664/1452091/WME%20Utils%20-%20Bootstrap.js

  1. // ==UserScript==
  2. // @name WME Utils - Bootstrap
  3. // @namespace WazeDev
  4. // @version 2024.09.22.000
  5. // @description Adds a bootstrap function for easier startup of wmeSdk, WazeWrap, and ScriptUpdateMonitor.
  6. // @author MapOMatic, WazeDev group
  7. // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
  8. // @license GNU GPLv3
  9. // ==/UserScript==
  10.  
  11. /* global WazeWrap */
  12. /* global getWmeSdk */
  13.  
  14. // Using var here to allow scripts to override with their own bootstrap, if needed,
  15. // without having to remove the @require line for this code.
  16.  
  17. // eslint-disable-next-line no-unused-vars, func-names, no-var
  18. var bootstrap = (function() {
  19. 'use strict';
  20.  
  21. let wmeSdk;
  22.  
  23. function wmeReady(scriptName, scriptId) {
  24. wmeSdk = getWmeSdk({ scriptName, scriptId });
  25. return new Promise(resolve => {
  26. if (wmeSdk.State.isReady()) resolve();
  27. wmeSdk.Events.once('wme-ready').then(resolve);
  28. });
  29. }
  30.  
  31. function wazeWrapReady(scriptName) {
  32. return new Promise(resolve => {
  33. (function checkWazeWrapReady(tries = 0) {
  34. if (WazeWrap.Ready) {
  35. resolve();
  36. } else if (tries < 1000) {
  37. setTimeout(checkWazeWrapReady, 200, ++tries);
  38. } else {
  39. console.error(`${scriptName}: WazeWrap took too long to load.`);
  40. }
  41. })();
  42. });
  43. }
  44.  
  45. function loadScriptUpdateMonitor(scriptName, scriptVersion, downloadUrl, metaUrl, metaRegExp) {
  46. let updateMonitor;
  47. try {
  48. if (!GM_xmlhttpRequest) {
  49. throw new Error('GM_xmlhttpRequest is required for WazeWrap.Alerts.ScriptUpdateMonitor');
  50. }
  51. updateMonitor = new WazeWrap.Alerts.ScriptUpdateMonitor(scriptName, scriptVersion, downloadUrl, GM_xmlhttpRequest, metaUrl, metaRegExp);
  52. updateMonitor.start();
  53. } catch (ex) {
  54. // Report, but don't stop if ScriptUpdateMonitor fails.
  55. console.error(`${scriptName}:`, ex);
  56. }
  57. }
  58.  
  59. async function bootstrapFunc(args) {
  60. // SDK: Remove this when fixed
  61. if (!window.SDK_INITIALIZED) {
  62. window.SDK_INITIALIZED = new Promise(resolve => {
  63. document.body.addEventListener('sdk-initialized', () => resolve());
  64. });
  65. }
  66. // --------
  67.  
  68. await window.SDK_INITIALIZED;
  69. await wmeReady(args.scriptName, args.scriptId);
  70. if (args.useWazeWrap) await wazeWrapReady(args);
  71. if (args.scriptUpdateMonitor) {
  72. loadScriptUpdateMonitor(
  73. args.scriptName,
  74. args.scriptUpdateMonitor.scriptVersion,
  75. args.scriptUpdateMonitor.downloadUrl,
  76. args.scriptUpdateMonitor.metaUrl,
  77. args.scriptUpdateMonitor.metaRegExp
  78. );
  79. }
  80. args.callback(wmeSdk);
  81. }
  82.  
  83. return bootstrapFunc;
  84. })();