Greasy Fork is available in English.

Ajax Async Lib

Async lib for the west

Detta skript bör inte installeras direkt. Det är ett bibliotek för andra skript att inkludera med meta-direktivet // @require https://update.greatest.deepsurf.us/scripts/490628/1468386/Ajax%20Async%20Lib.js

  1. // ==UserScript==
  2. // @name Ajax Async Lib
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Async lib for the west
  6. // @include https://*.the-west.*/game.php*
  7. // @include https://*.the-west.*.*/game.php*
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11.  
  12. (function () {
  13. AjaxAsync = function() {
  14. jQuery.ajaxSetup({
  15. type: 'POST',
  16. dataType: 'json'
  17. });
  18. var makeUrl = function(options) {
  19. var url = 'game.php'
  20. , params = [];
  21. if (options.window)
  22. params.push('window=' + options.window);
  23. if (options.action)
  24. params.push('action=' + options.action, 'h=' + Player.h);
  25. if (options.ajax)
  26. params.push('ajax=' + options.ajax);
  27. if (options.mode)
  28. params.push('mode=' + options.mode);
  29. return url + params.length ? '?' + params.join('&') : '';
  30. };
  31. var onFinish = function(window) {
  32. return function() {
  33. if (window && window.hideLoader)
  34. window.hideLoader();
  35. else if (window && window.hasOwnProperty('window'))
  36. window.window.hideLoader();
  37. };
  38. };
  39. var request = async function(options) {
  40. var url = options.url || makeUrl(options);
  41. return await jQuery.ajax(url, options);
  42. };
  43. var defaultRequest = async function(options, window) {
  44. if (window && window.showLoader)
  45. window.showLoader();
  46. else if (window && window.hasOwnProperty('window'))
  47. window.window.showLoader();
  48. var result = await request(options);
  49. onFinish(window)();
  50. return result;
  51. };
  52. return {
  53. remoteCall: async function(window, action, param, view) {
  54. return await defaultRequest({
  55. window: window,
  56. action: action,
  57. data: param
  58. }, view);
  59. },
  60. remoteCallMode: async function(window, mode, param, view) {
  61. return await defaultRequest({
  62. window: window,
  63. mode: mode,
  64. data: param
  65. }, view);
  66. },
  67. get: async function(window, ajax, param, view) {
  68. return await defaultRequest({
  69. window: window,
  70. ajax: ajax,
  71. data: param
  72. }, view);
  73. },
  74. gameServiceRequest: async function(method, urlparam, post) {
  75. return await defaultRequest({
  76. url: Game.serviceURL + '/' + method + '/' + urlparam,
  77. data: post
  78. });
  79. },
  80. request: request,
  81. wait: function (ms) {
  82. return new Promise(resolve => setTimeout(resolve, ms));
  83. },
  84. WaitJobsAsync: async function () {
  85. do {
  86. await AjaxAsync.wait(400);
  87. } while (TaskQueue.queue.length > 0);
  88. },
  89. }
  90. }();
  91.  
  92. })();