ProgressBar

Create and manage simple progress bars

目前为 2019-10-16 提交的版本。查看 最新版本

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

  1. // ProgressBar library
  2. //
  3. // Create and manage simple progress bars.
  4. //
  5. // https://greasyfork.org/scripts/**TBD**-progressbar
  6. // Copyright (C) 2019, Guido Villa
  7. // Original version of the script is taken from IMDb 'My Movies' enhancer:
  8. // Copyright (C) 2008-2018, Ricardo Mendonça Ferreira (ric@mpcnet.com.br)
  9. // Released under the GPL license - http://www.gnu.org/copyleft/gpl.html
  10. //
  11. // For instructions on user scripts, see:
  12. // https://greasyfork.org/help/installing-user-scripts
  13. //
  14. // To use this library in a userscript you must add to script header:
  15. // @require https://greasyfork.org/scripts/**TBD**-progressbar/code/ProgressBar.js
  16. // @grant GM_addStyle
  17. //
  18. // --------------------------------------------------------------------
  19. //
  20. // ==UserScript==
  21. // @namespace https://greasyfork.org/users/373199-guido-villa
  22. // @exclude *
  23. //
  24. // ==UserLibrary==
  25. // @name ProgressBar
  26. // @description Create and manage simple progress bars
  27. // @version 1.0
  28. // @author guidovilla
  29. // @date 16.10.2019
  30. // @copyright 2019, Guido Villa (https://greasyfork.org/users/373199-guido-villa)
  31. // @license GPL-3.0-or-later
  32. // @homepageURL https://greasyfork.org/scripts/**TBD**-progressbar
  33. // @supportURL https://gitlab.com/gv-browser/userscripts/issues
  34. // @contributionURL https://tinyurl.com/gv-donate-2e
  35. // ==/UserScript==
  36. //
  37. // ==/UserLibrary==
  38. //
  39. // --------------------------------------------------------------------
  40. //
  41. // To-do (priority: [H]igh, [M]edium, [L]ow):
  42. // - [M] width must be an integer multiple of background-size, otherwise animation will skip => address
  43. // - [M] speed of the animation depends on width => fix?
  44. // - [m] speed of transition is not constant (time is constant, regardless of the "space" to be travelled) => can it be fixed?
  45. // - [H] wrap in order to hide global variables
  46. // - [M] nicer presentation style (maybe small vertical bars), graphical improvements
  47. //
  48. // Changelog:
  49. // ----------
  50. // 2019.10.16 [1.0] First version
  51. // 2019.10.14 [0.1] First test version, private use only
  52. //
  53.  
  54. /* jshint esversion: 6, supernew: true, laxbreak: true */
  55. /* exported EL, Library_Version_PROGRESSBAR */
  56.  
  57. const Library_Version_PROGRESSBAR = '1.0';
  58.  
  59. /* How to use the library
  60.  
  61. - Create a new progress bar:
  62. var pb = new ProgressBar(...)
  63.  
  64. - Change the progress:
  65. pb.update(...)
  66. pb.advance(...)
  67.  
  68. - Remove the progress bar:
  69. pb.close()
  70.  
  71. Details
  72. Progress bars are defined by three main parameters:
  73. - finish: value that defines what is 100%
  74. this is set at creation time and cannot be changed
  75. - progress: value that defines current completion status (must be <= finish)
  76. initial progress is set a creation time, then it can be updated
  77. with update() and advance()
  78. When progress = -1, the bar is in "generic" loading mode, i.e. it
  79. does not show a specific progress but an unspecified loading status
  80. - message: the message printed inside the bar (e.g. "Loading...")
  81. initial message is set a creation time, then it can be changed
  82. with every update() and advance().
  83. The message can contain a few placeholders that are replaced with
  84. actual progress data:
  85. - {#}: replace with current progress number
  86. - {$}: replace with finish value
  87. - {%}: replace with completion percentage (= 100*progress/finish)
  88. E.g.: "Loading {#} of {$}..." => "Loading 7 of 23..."
  89.  
  90. All numbers are integers.
  91.  
  92. Information for changing styles:
  93. The HTML id of the container DIV can be accessed through the 'id' property
  94. of the progress bar object.
  95. All elements that constitute the bar have a generic "pb-progress-bar" class and
  96. a specific "pb-progress-bar-XXX" class different for each element.
  97. Generic loading is enabled by applying a "pb-generic" class to the
  98. container DIV.
  99.  
  100. Parameters (all parameters are optional):
  101.  
  102. - ProgressBar(finish, msg, options)
  103. Create a new progress bar. Parameters:
  104. - finish: maximum value that can be reached (default is 100)
  105. - msg: message written in the bar, see above for substitutions
  106. default is "Loading {#}/{$}..."
  107. - options: an object that may contain:
  108. - start: initial progress status (default is 0, i.e. the beginning)
  109. - container: positioned element where the bar will be centered
  110. null (the default): center bar on the screen
  111. - width: width in pixels of the progress bar (default is 226.3)
  112. - height: height in pixels of the progress bar (default is 30)
  113.  
  114. - update(progress, msg)
  115. Update the progress bar status. Parameters:
  116. - progress: the new progress value (default is 0)
  117. - msg: an optional new message (default is: don't change message)
  118.  
  119. - advance(value, msg)
  120. Increment the progress bar status. Parameters:
  121. - value: the increment value, can be negative (default is 1)
  122. - msg: an optional new message (default is: don't change message)
  123.  
  124. - close()
  125. Close the progress bar and remove it from the DOM.
  126.  
  127. */
  128.  
  129.  
  130. var progress_bar_style_has_been_loaded = false;
  131. var progress_bar_index = 0;
  132. // Create progress bar
  133. // eslint-disable-next-line max-statements
  134. function ProgressBar(finish = 100, msg = 'Loading {#}/{$}...', options) {
  135. // style definition
  136. var STYLE = '.pb-progress-bar.pb-progress-bar-box{border:2px solid black;background-color:white;padding:4px;outline:white solid 6px;}'
  137. + '.pb-progress-bar.pb-progress-bar-bar{background-color:green;height:100%;transition:width 300ms linear;}'
  138. + '.pb-progress-bar.pb-progress-bar-txtcont{position:absolute;top:0;left:0;width:100%;height:100%;display:table;}'
  139. + '.pb-progress-bar.pb-progress-bar-txt{display:table-cell;text-align:center;vertical-align:middle;font:16px verdana,sans-serif;color:black;}'
  140. + '.pb-progress-bar.pb-progress-bar-box.pb-generic{background:repeating-linear-gradient(-45deg,#F0F0F0 0 20px,#ccc 20px 40px);background-size:56.56854px;animation:2s linear infinite loading;}'
  141. + '.pb-progress-bar.pb-progress-bar-box.pb-generic .pb-progress-bar-bar{background-color:transparent;transition:none}'
  142. + '@keyframes loading{from{background-position-x:0%;} to{background-position-x:100%;}}';
  143. if (!progress_bar_style_has_been_loaded) {
  144. GM_addStyle(STYLE);
  145. progress_bar_style_has_been_loaded = true;
  146. }
  147.  
  148. var self = this;
  149.  
  150. // basic configuration
  151. this.id = 'pb-progress-bar-' + ++progress_bar_index; // 'id' is public
  152. var start = 0;
  153. var container = null;
  154. var width = 226.27417;
  155. var height = 30;
  156. var message = msg;
  157.  
  158. var current; // completion status of the progress bar
  159.  
  160. var pbBox, pb, pbTxtCont, pbTxt; // elements of the progress bar
  161.  
  162. // helper function to create the elements
  163. function createElement(father, elementType, className, id) {
  164. var elem = document.createElement(elementType);
  165. if (typeof id !== 'undefined') elem.id = id;
  166. elem.className = 'pb-progress-bar ' + className;
  167. father.appendChild(elem);
  168. return elem;
  169. }
  170.  
  171. // initialization function
  172. function init() {
  173. // check for options in the call
  174. if (options && typeof options === 'object') {
  175. if (typeof options.id !== 'undefined') self.id = options.id;
  176. if (typeof options.start !== 'undefined') start = options.start;
  177. if (typeof options.container !== 'undefined') container = options.container;
  178. if (typeof options.width !== 'undefined') width = options.width;
  179. if (typeof options.height !== 'undefined') height = options.height;
  180. }
  181.  
  182. // calculate positioning
  183. var containerWidth, containerHeight,
  184. cntElem,
  185. positioningStyle;
  186.  
  187. function setPositioningVars(cnt, pos, w, h) {
  188. containerWidth = w;
  189. containerHeight = h;
  190. cntElem = cnt;
  191. positioningStyle = pos;
  192. }
  193.  
  194. if (container) {
  195. var rect = container.getBoundingClientRect();
  196. setPositioningVars(container, 'absolute', rect.width, rect.height);
  197. } else {
  198. setPositioningVars(document.body, 'fixed', window.innerWidth, window.innerHeight);
  199. }
  200. var top = containerHeight / 2 - height / 2;
  201. var left = containerWidth / 2 - width / 2;
  202.  
  203. // create the elements
  204. pbBox = createElement(cntElem, 'div', 'pb-progress-bar-box', self.id);
  205. pbBox.style.cssText = 'position:' + positioningStyle
  206. + '; height:' + height + 'px;width:' + width
  207. + 'px;top:' + top + 'px;left:' + left + 'px;';
  208.  
  209. pb = createElement(pbBox, 'div', 'pb-progress-bar-bar');
  210. pbTxtCont = createElement(pbBox, 'div', 'pb-progress-bar-txtcont');
  211. pbTxt = createElement(pbTxtCont, 'div', 'pb-progress-bar-txt');
  212.  
  213. // set the initial progress
  214. self.update(start);
  215. }
  216.  
  217.  
  218. /* PUBLIC members */
  219.  
  220. // update the progress to "currentVal" and optionally change the message
  221. this.update = function(currentVal = 0, newMsg) {
  222. if (newMsg) message = newMsg;
  223. var newVal = (currentVal > finish ? finish : currentVal);
  224.  
  225. if (newVal < 0) {
  226. // setting the width to zero is not really needed, but ensures a
  227. // more consistent behaviour in cases where the delay (see
  228. // below) is not enough.
  229. pb.style.width = '0';
  230. // try to make the message more appealing in "generic" case
  231. pbTxt.textContent = message
  232. .replace(/ *{#}.*{\$} */g, '')
  233. .replace(/ *{#} */g, '')
  234. .replace(/ *{\$} */g, '')
  235. .replace(/ *{%} *%? */g, '');
  236. pbBox.classList.add('pb-generic');
  237. } else {
  238. pb.style.width = (100*newVal/finish) + '%';
  239. if (current < 0) {
  240. // if exiting from "generic" mode a small delay is needed,
  241. // otherwise the class may be removed when changing the
  242. // width, and the width transition takes place anyway
  243. setTimeout(function() {
  244. pbBox.classList.remove('pb-generic');
  245. }, 33);
  246. } else {
  247. pbBox.classList.remove('pb-generic');
  248. }
  249. // replace placeholders with actual numbers
  250. pbTxt.textContent = message
  251. .replace(/{#}/g, newVal)
  252. .replace(/{\$}/g, finish)
  253. .replace(/{%}/g, Math.round(100*newVal/finish));
  254. }
  255. current = newVal;
  256. };
  257.  
  258.  
  259. // advance the progress by "value" and optionally change the message
  260. this.advance = function(value = 1, newMsg) {
  261. self.update(current + value, newMsg);
  262. };
  263.  
  264.  
  265. // close/remove the progress bar
  266. this.close = function() {
  267. pbBox.parentNode.removeChild(pbBox);
  268. };
  269.  
  270.  
  271. /* INITIALIZATION */
  272. init();
  273. }