Wanikani Open Framework - Progress module

Progress module for Wanikani Open Framework

当前为 2018-02-17 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/38577/252061/Wanikani%20Open%20Framework%20-%20Progress%20module.js

  1. // ==UserScript==
  2. // @name Wanikani Open Framework - Progress module
  3. // @namespace rfindley
  4. // @description Progress module for Wanikani Open Framework
  5. // @version 1.0.0
  6. // @copyright 2018+, Robin Findley
  7. // @license MIT; http://opensource.org/licenses/MIT
  8. // ==/UserScript==
  9.  
  10. (function(global) {
  11.  
  12. //########################################################################
  13. //------------------------------
  14. // Published interface
  15. //------------------------------
  16. global.wkof.Progress = {
  17. update: update_progress
  18. }
  19. //########################################################################
  20.  
  21. var popup_delay = 1000; // Delay before popup will open (in milliseconds).
  22. var popup_delay_started = false, popup_delay_expired = false;
  23. var externals_requested = false, externals_loaded = false;
  24. var progress_bars = {};
  25. var user_closed = false;
  26. var dialog_visible = false, dialog;
  27.  
  28. //------------------------------
  29. // Update the progress bar.
  30. //------------------------------
  31. function update_progress(data) {
  32. if (data) update_data(data);
  33.  
  34. if (!dialog_visible && !have_pending()) shutdown();
  35.  
  36. // We have something pending, but don't show dialog until popup_delay has passed.
  37. if (!popup_delay_started) return start_popup_delay();
  38.  
  39. // Popup delay has passed. Show progress.
  40. if (!popup_delay_expired) return;
  41. update_dialog();
  42. }
  43.  
  44. //------------------------------
  45. // Update our stored progress bar status
  46. //------------------------------
  47. function update_data(data) {
  48. var bar = progress_bars[data.name];
  49. if (!bar) progress_bars[data.name] = bar = {label: data.label};
  50. bar.is_updated = true;
  51. bar.value = data.value;
  52. bar.max = data.max;
  53. if (bar.max === 0) {
  54. bar.value = 1;
  55. bar.max = 1;
  56. }
  57. // Don't retain items that complete before the dialog pops up.
  58. if (!popup_delay_expired && (bar.value >= bar.max))
  59. delete progress_bars[data.name];
  60. }
  61.  
  62. //------------------------------
  63. // Check if some progress is still pending.
  64. //------------------------------
  65. function have_pending() {
  66. var all_done = true;
  67. for (name in progress_bars) {
  68. var progress_bar = progress_bars[name];
  69. if (progress_bar.value < progress_bar.max) all_done = false;
  70. }
  71. return !all_done;
  72. }
  73.  
  74. //------------------------------
  75. // Delay the dialog from popping up until progress takes at least N milliseconds.
  76. //------------------------------
  77. function start_popup_delay() {
  78. popup_delay_started = true;
  79. popup_timer = setTimeout(function(){
  80. popup_delay_expired = true;
  81. update_progress();
  82. }, popup_delay);
  83. }
  84.  
  85. //------------------------------
  86. // Update the contents of the progress dialog (if it's currently visible)
  87. //------------------------------
  88. function update_dialog() {
  89. if (!externals_requested) {
  90. externals_requested = true;
  91. load_externals()
  92. .then(function(){
  93. externals_loaded = true;
  94. update_progress();
  95. });
  96. return;
  97. }
  98. if (!externals_loaded) return;
  99.  
  100. if (!dialog_visible) {
  101. dialog_visible = true;
  102. if ($('#wkof_ds').length === 0) $('body').prepend('<div id="wkof_ds"></div>');
  103.  
  104. dialog = $('<div id="wkof_progbar_dlg" class="wkofs_progress_dlg" style="display:none;"></div>');
  105.  
  106. dialog.dialog({
  107. title: 'Loading Data...',
  108. minHeight: 20,
  109. maxHeight: window.innerHeight,
  110. height: 'auto',
  111. modal: false,
  112. resizable: false,
  113. autoOpen: false,
  114. appendTo: '#wkof_ds',
  115. close: dialog_close
  116. });
  117. dialog.dialog('open');
  118. }
  119.  
  120. var all_done = true;
  121. for (name in progress_bars) {
  122. var progress_bar = progress_bars[name];
  123. if (progress_bar.value < progress_bar.max) all_done = false;
  124. var bar = $('#wkof_progbar_dlg .wkof_progbar_wrap[name="'+name+'"]');
  125. if (bar.length === 0) {
  126. bar = $('<div class="wkof_progbar_wrap" name="'+name+'"><label>'+progress_bar.label+'</label><div class="wkof_progbar"></div></div>');
  127. var bars = $('#wkof_progbar_dlg .wkof_progbar_wrap');
  128. bars.push(bar[0]);
  129. $('#wkof_progbar_dlg').append(bars.sort(bar_label_compare));
  130. }
  131. if (progress_bar.is_updated) {
  132. progress_bar.is_updated = false;
  133. bar.find('.wkof_progbar').progressbar({value: progress_bar.value, max: progress_bar.max});
  134. }
  135. }
  136.  
  137. if (all_done) shutdown();
  138. }
  139.  
  140. function dialog_close() {
  141. dialog.dialog('destroy');
  142. }
  143.  
  144. //------------------------------
  145. // Load external support files (jquery UI and stylesheet)
  146. //------------------------------
  147. function load_externals() {
  148. if (location.hostname.match(/^(www\.)?wanikani\.com$/) !== null)
  149. css_url = 'https://raw.githubusercontent.com/rfindley/wanikani-open-framework/master/jqui-wkmain.css';
  150.  
  151. return Promise.all([
  152. wkof.load_script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js', true /* cache */),
  153. wkof.load_css(css_url, true /* cache */)
  154. ]);
  155. }
  156.  
  157. //------------------------------
  158. // Comparison function for sorting progress bars.
  159. //------------------------------
  160. function bar_label_compare(a, b){
  161. var a = $(a).find('label').text();
  162. var b = $(b).find('label').text();
  163. return a.localeCompare(b);
  164. }
  165.  
  166. //------------------------------
  167. // Shut down the dialog box and cancel the popup delay timer.
  168. //------------------------------
  169. function shutdown() {
  170. // Reset all flags
  171. user_closed = false;
  172.  
  173. // If popup timer was pending, cancel it.
  174. if (popup_delay_started && !popup_delay_expired) clearTimeout(popup_timer);
  175. popup_delay_started = false;
  176. popup_delay_expired = false;
  177.  
  178. // If progress dialog is open, close it.
  179. if (dialog_visible) {
  180. dialog.dialog('close');
  181. dialog_visible = false;
  182. }
  183. progress_bars = {};
  184. }
  185.  
  186. function set_ready_state(){
  187. // Delay guarantees include() callbacks are called before ready() callbacks.
  188. setTimeout(function(){wkof.set_state('wkof.Progress', 'ready');},0);
  189. }
  190. set_ready_state();
  191.  
  192. })(window);
  193.  

QingJ © 2025

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