WME Utils - Bootstrap

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

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/509664/1461898/WME%20Utils%20-%20Bootstrap.js

  1. // ==UserScript==
  2. // @name WME Utils - Bootstrap
  3. // @namespace WazeDev
  4. // @version 2024.10.09.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. /* 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({ eventName: '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(args) {
  47. let updateMonitor;
  48. try {
  49. if (typeof GM_xmlhttpRequest === 'undefined') {
  50. throw new Error('GM_xmlhttpRequest is required to use WazeWrap.Alerts.ScriptUpdateMonitor');
  51. }
  52. updateMonitor = new WazeWrap.Alerts.ScriptUpdateMonitor(
  53. args.scriptName,
  54. args.scriptUpdateMonitor.scriptVersion,
  55. args.scriptUpdateMonitor.downloadUrl,
  56. GM_xmlhttpRequest,
  57. args.scriptUpdateMonitor.metaUrl,
  58. args.scriptUpdateMonitor.metaRegExp
  59. );
  60. updateMonitor.start();
  61. } catch (ex) {
  62. // Report, but don't stop if ScriptUpdateMonitor fails.
  63. console.error(`${args.scriptName}:`, ex);
  64. }
  65. }
  66.  
  67. async function bootstrapFunc(args) {
  68. args = { ...args };
  69. args.scriptName ??= GM_info.script.name;
  70. args.scriptId ??= GM_info.script.name.replaceAll(' ', '');
  71. await SDK_INITIALIZED;
  72. await wmeReady(args.scriptName, args.scriptId);
  73. if (args.useWazeWrap || args.scriptUpdateMonitor) await wazeWrapReady(args);
  74. if (args.scriptUpdateMonitor) {
  75. args.scriptUpdateMonitor.scriptVersion ??= GM_info.script.version;
  76. loadScriptUpdateMonitor(args);
  77. }
  78. args.callback?.(wmeSdk);
  79. return wmeSdk;
  80. }
  81.  
  82. return bootstrapFunc;
  83. })();