Brazen Item Attributes Resolver

Item attributes resolution helper class

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/429587/1244644/Brazen%20Item%20Attributes%20Resolver.js

  1. // ==UserScript==
  2. // @name Brazen Item Attributes Resolver
  3. // @namespace brazenvoid
  4. // @version 2.2.2
  5. // @author brazenvoid
  6. // @license GPL-3.0-only
  7. // @description Item attributes resolution helper class
  8. // ==/UserScript==
  9.  
  10. class BrazenItemAttributesResolver
  11. {
  12. /**
  13. * @typedef {{itemLinkSelector: JQuery.Selector, itemDeepAnalysisSelector: JQuery.Selector, requestDelay: number,
  14. * onDeepAttributesResolution: Function}} ItemAttributesResolverConfiguration
  15. */
  16. /**
  17. * @callback ItemAttributesResolverCallback
  18. * @param {JQuery} item
  19. * @return {*}
  20. */
  21. /**
  22. * @param {ItemAttributesResolverConfiguration} configuration
  23. */
  24. constructor(configuration)
  25. {
  26. /**
  27. * @type {{}}
  28. * @private
  29. */
  30. this._attributes = {}
  31. /**
  32. * @type {{}}
  33. * @private
  34. */
  35. this._deepAttributes = {}
  36. /**
  37. * @type {boolean}
  38. * @private
  39. */
  40. this._hasDeepAttributes = false
  41. /**
  42. * @type {JQuery.Selector}
  43. * @protected
  44. */
  45. this._itemLinkSelector = configuration.itemLinkSelector
  46. /**
  47. * @type {JQuery.Selector}
  48. * @protected
  49. */
  50. this._itemDeepAnalysisSelector = configuration.itemDeepAnalysisSelector
  51. /**
  52. * @type {Function}
  53. * @private
  54. */
  55. this._onDeepAttributesResolution = configuration.onDeepAttributesResolution
  56. /**
  57. * @type {number}
  58. * @private
  59. */
  60. this._requestDelay = configuration.requestDelay
  61. /**
  62. * @type {number}
  63. * @private
  64. */
  65. this._requestIteration = 1
  66. /**
  67. * @type {JQuery<HTMLElement> | jQuery | HTMLElement}
  68. * @private
  69. */
  70. this._sandbox = $('<div id="brazen-item-attributes-resolver-sandbox" hidden/>').appendTo('body')
  71. }
  72. /**
  73. * @param {string} attribute
  74. * @returns {string}
  75. * @private
  76. */
  77. _formatAttributeName(attribute)
  78. {
  79. return attribute.toLowerCase().replaceAll(' ', '_')
  80. }
  81. /**
  82. * @param {JQuery} item
  83. * @param {Object} attributesBag
  84. * @private
  85. */
  86. _loadDeepAttributes(item, attributesBag)
  87. {
  88. let url = item.find(this._itemLinkSelector).first().attr('href')
  89. if (url) {
  90. Utilities.sleep(this._requestIteration * this._requestDelay).then(() => {
  91. try {
  92. this._sandbox.load(url + ' ' + this._itemDeepAnalysisSelector, () => {
  93. for (const attributeName in this._deepAttributes) {
  94. attributesBag[attributeName] = this._deepAttributes[attributeName](this._sandbox)
  95. }
  96. this._onDeepAttributesResolution(item)
  97. this._sandbox.empty()
  98. })
  99. } catch (error) {
  100. console.error('Deep attributes loading failed.')
  101. }
  102. })
  103. this._requestIteration++
  104. }
  105. }
  106. /**
  107. * @param {string} attribute
  108. * @param {ItemAttributesResolverCallback} resolutionCallback
  109. * @returns {this}
  110. */
  111. addAttribute(attribute, resolutionCallback)
  112. {
  113. this._attributes[this._formatAttributeName(attribute)] = resolutionCallback
  114. return this
  115. }
  116. /**
  117. * @param {string} attribute
  118. * @param {ItemAttributesResolverCallback} resolutionCallback
  119. * @returns {this}
  120. */
  121. addDeepAttribute(attribute, resolutionCallback)
  122. {
  123. this._deepAttributes[this._formatAttributeName(attribute)] = resolutionCallback
  124. this._hasDeepAttributes = true
  125. return this
  126. }
  127. /**
  128. * @returns {BrazenItemAttributesResolver}
  129. */
  130. completeResolutionRun()
  131. {
  132. this._requestIteration = 1
  133. return this
  134. }
  135. /**
  136. * @param {JQuery} item
  137. * @param {string} attribute
  138. * @returns {*}
  139. */
  140. get(item, attribute)
  141. {
  142. let attributesBag = item[0].scriptAttributes
  143. if (typeof attributesBag !== 'undefined') {
  144. let attributeName = this._formatAttributeName(attribute)
  145. let attributeValue = attributesBag[attributeName]
  146. if (typeof attributeValue !== 'undefined') {
  147. return attributeValue
  148. } else if (this._hasDeepAttributes) {
  149. this._loadDeepAttributes(item, attributesBag)
  150. }
  151. }
  152. return null
  153. }
  154. /**
  155. * @param {JQuery} item
  156. * @param {Function|null} afterResolutionCallback
  157. */
  158. resolveAttributes(item, afterResolutionCallback = null)
  159. {
  160. let attributesBag = {}
  161. item[0].scriptAttributes = attributesBag
  162. for (const attributeName in this._attributes) {
  163. attributesBag[attributeName] = this._attributes[attributeName](item)
  164. }
  165. }
  166. /**
  167. * @param {JQuery} item
  168. * @param {string} attribute
  169. * @param {*} value
  170. * @returns {BrazenItemAttributesResolver}
  171. */
  172. set(item, attribute, value)
  173. {
  174. item[0].scriptAttributes[this._formatAttributeName(attribute)] = value
  175. return this
  176. }
  177. }

QingJ © 2025

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