WME Utils - Bootstrap

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

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

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

  1. // ==UserScript==
  2. // @name WME Utils - Bootstrap
  3. // @namespace WazeDev
  4. // @version 2024.09.27.001
  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. /* global SDK_INITIALIZED */
  14.  
  15. // Using var here to allow scripts to override with their own bootstrap, if needed,
  16. // without having to remove the @require line for this code.
  17.  
  18. // eslint-disable-next-line no-unused-vars, func-names, no-var
  19. var bootstrap = (function() {
  20. 'use strict';
  21.  
  22. let wmeSdk;
  23.  
  24. function wmeReady(scriptName, scriptId) {
  25. wmeSdk = getWmeSdk({ scriptName, scriptId });
  26. return new Promise(resolve => {
  27. if (wmeSdk.State.isReady()) resolve();
  28. wmeSdk.Events.once('wme-ready').then(resolve);
  29. });
  30. }
  31.  
  32. function wazeWrapReady(scriptName) {
  33. return new Promise(resolve => {
  34. (function checkWazeWrapReady(tries = 0) {
  35. if (WazeWrap.Ready) {
  36. resolve();
  37. } else if (tries < 1000) {
  38. setTimeout(checkWazeWrapReady, 200, ++tries);
  39. } else {
  40. console.error(`${scriptName}: WazeWrap took too long to load.`);
  41. }
  42. })();
  43. });
  44. }
  45.  
  46. function loadScriptUpdateMonitor(scriptName, scriptVersion, downloadUrl, metaUrl, metaRegExp) {
  47. let updateMonitor;
  48. try {
  49. if (!GM_xmlhttpRequest) {
  50. throw new Error('GM_xmlhttpRequest is required for WazeWrap.Alerts.ScriptUpdateMonitor');
  51. }
  52. updateMonitor = new WazeWrap.Alerts.ScriptUpdateMonitor(scriptName, scriptVersion, downloadUrl, GM_xmlhttpRequest, metaUrl, metaRegExp);
  53. updateMonitor.start();
  54. } catch (ex) {
  55. // Report, but don't stop if ScriptUpdateMonitor fails.
  56. console.error(`${scriptName}:`, ex);
  57. }
  58. }
  59.  
  60. async function bootstrapFunc(args) {
  61. await SDK_INITIALIZED;
  62. await wmeReady(args.scriptName, args.scriptId);
  63. if (args.useWazeWrap || args.scriptUpdateMonitor) await wazeWrapReady(args);
  64. if (args.scriptUpdateMonitor) {
  65. loadScriptUpdateMonitor(
  66. args.scriptName,
  67. args.scriptUpdateMonitor.scriptVersion,
  68. args.scriptUpdateMonitor.downloadUrl,
  69. args.scriptUpdateMonitor.metaUrl,
  70. args.scriptUpdateMonitor.metaRegExp
  71. );
  72. }
  73. return wmeSdk;
  74. }
  75.  
  76. return bootstrapFunc;
  77. })();