Brazen Subscriptions Loader

Helper class for loading account subscriptions

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

  1. // ==UserScript==
  2. // @name Brazen Subscriptions Loader
  3. // @namespace brazenvoid
  4. // @version 1.0.1
  5. // @author brazenvoid
  6. // @license GPL-3.0-only
  7. // @description Helper class for loading account subscriptions
  8. // ==/UserScript==
  9.  
  10. class BrazenSubscriptionsLoader
  11. {
  12. /**
  13. * @callback SubscriptionLoaderGetPageCountCallback
  14. * @param {JQuery} page
  15. * @return {number}
  16. */
  17.  
  18. /**
  19. * @callback SubscriptionLoaderGetPageUrlCallback
  20. * @param {string} baseUrl
  21. * @param {number} pageNo
  22. * @return {string}
  23. */
  24.  
  25. /**
  26. * @callback SubscriptionLoaderProgressReportingCallback
  27. * @param {string} status
  28. */
  29.  
  30. /**
  31. * @callback SubscriptionLoaderSubscriptionsGatheredCallback
  32. * @param {string[]} subscriptions
  33. */
  34.  
  35. /**
  36. * @typedef {{url: string, getPageCount: SubscriptionLoaderGetPageCountCallback, getPageUrl: SubscriptionLoaderGetPageUrlCallback,
  37. * subscriptionNameSelector: string, subscriptionsCountSelector: string}} SubscriptionLoaderConfig
  38. */
  39.  
  40. /**
  41. * @param {SubscriptionLoaderProgressReportingCallback} onProgressUpdate
  42. * @param {SubscriptionLoaderSubscriptionsGatheredCallback} onSubscriptionsGathered
  43. */
  44. constructor (onProgressUpdate, onSubscriptionsGathered)
  45. {
  46. /**
  47. * @type {number}
  48. * @private
  49. */
  50. this._configIterator = 0
  51.  
  52. /**
  53. * @type {SubscriptionLoaderConfig[]}
  54. * @private
  55. */
  56. this._configs = []
  57.  
  58. /**
  59. * @type {SubscriptionLoaderProgressReportingCallback}
  60. * @private
  61. */
  62. this._onProgressUpdate = onProgressUpdate
  63.  
  64. /**
  65. * @type {SubscriptionLoaderSubscriptionsGatheredCallback}
  66. * @private
  67. */
  68. this._onSubscriptionsGathered = onSubscriptionsGathered
  69.  
  70. /**
  71. * @type {JQuery}
  72. * @private
  73. */
  74. this._sandbox = null
  75.  
  76. /**
  77. * @type {SubscriptionLoaderConfig}
  78. * @private
  79. */
  80. this._selectedConfig = {}
  81.  
  82. /**
  83. * @type {*[]}
  84. * @private
  85. */
  86. this._subscriptions = []
  87. }
  88.  
  89. /**
  90. * Loads and records subscriptions by page
  91. * @param {number} maxPage
  92. * @param {number} pageNo
  93. * @private
  94. */
  95. _loadSubscriptionsPage (pageNo, maxPage)
  96. {
  97. this._sandbox.empty().load(this._selectedConfig.getPageUrl(this._selectedConfig.url, pageNo), () => {
  98. this._sandbox.find(this._selectedConfig.subscriptionNameSelector).each((index, element) => {
  99. this._subscriptions.push($(element).text().trim())
  100. })
  101. this._onProgressUpdate('Gathering subscriptions - ' + Math.ceil(((pageNo / maxPage) * 100)) + '% Complete')
  102. pageNo++
  103. if (pageNo <= maxPage) {
  104. this._loadSubscriptionsPage(pageNo, maxPage)
  105. } else {
  106. this._configIterator++
  107. if (this._configIterator < this._configs.length) {
  108. this._processSubscriptions()
  109. } else {
  110. this._sandbox.remove()
  111. this._onProgressUpdate('Loaded ' + this._subscriptions.length + ' subscriptions.')
  112. this._onSubscriptionsGathered(this._subscriptions)
  113. this._configIterator = 0
  114. this._subscriptions = []
  115. }
  116. }
  117. })
  118. }
  119.  
  120. /**
  121. * @private
  122. */
  123. _processSubscriptions ()
  124. {
  125. this._selectedConfig = this._configs[this._configIterator]
  126. this._sandbox.empty().load(this._selectedConfig.url + ' ' + this._selectedConfig.subscriptionsCountSelector, () => {
  127. this._loadSubscriptionsPage(1, Math.ceil(this._selectedConfig.getPageCount(this._sandbox)))
  128. })
  129. }
  130.  
  131. /**
  132. * @param {SubscriptionLoaderConfig} config
  133. * @returns {BrazenSubscriptionsLoader}
  134. */
  135. addConfig (config)
  136. {
  137. this._configs.push(config)
  138. return this
  139. }
  140.  
  141. run ()
  142. {
  143. this._onProgressUpdate('Gathering subscriptions - 0% Complete')
  144. this._sandbox = $('<div id="brazen-subscriptions-loader-sandbox" hidden/>').appendTo('body')
  145. this._processSubscriptions()
  146. }
  147. }