Item attributes resolution helper class
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/429587/951949/Brazen%20Item%20Attributes%20Resolver.js
// ==UserScript==
// @name Brazen Item Attributes Resolver
// @namespace brazenvoid
// @version 1.0.0
// @author brazenvoid
// @license GPL-3.0-only
// @description Item attributes resolution helper class
// ==/UserScript==
class BrazenItemAttributesResolver
{
/**
* @callback ItemAttributesResolverCallback
* @param {JQuery} item
* @return {*}
*/
/**
* @param {JQuery.Selector} itemPageLinkSelector
* @param {JQuery.Selector} itemPageDeepAnalysisSelector
*/
constructor (itemPageLinkSelector, itemPageDeepAnalysisSelector)
{
/**
* @type {{}}
* @private
*/
this._attributes = {}
/**
* @type {{}}
* @private
*/
this._deepAttributes = {}
/**
* @type {boolean}
* @private
*/
this._hasDeepAttributes = false
/**
* @type {JQuery.Selector}
* @protected
*/
this._itemPageLinkSelector = itemPageLinkSelector
/**
* @type {JQuery.Selector}
* @protected
*/
this._itemPageDeepAnalysisSelector = itemPageDeepAnalysisSelector
/**
* @type {JQuery<HTMLElement> | jQuery | HTMLElement}
* @private
*/
this._sandbox = $('<div id="brazen-item-attributes-resolver-sandbox" hidden/>').appendTo('body')
}
/**
* @param {string} attributeName
* @returns {string}
* @private
*/
_generateAttributeName (attributeName)
{
return 'scriptItemAttribute' + attributeName[0].toUpperCase() + attributeName.slice(1)
}
/**
* @param {string} name
* @param {ItemAttributesResolverCallback} resolutionCallback
* @returns {this}
*/
addAttribute (name, resolutionCallback)
{
this._attributes[name] = resolutionCallback
return this
}
/**
* @param {string} name
* @param {ItemAttributesResolverCallback} resolutionCallback
* @returns {this}
*/
addDeepAttribute (name, resolutionCallback)
{
this._deepAttributes[name] = resolutionCallback
this._hasDeepAttributes = true
return this
}
/**
* @param {JQuery} item
* @param {string} attributeName
* @returns {*}
*/
getAttribute (item, attributeName)
{
return item[0][this._generateAttributeName(attributeName)]
}
/**
* @param {JQuery} item
* @param {Function|null} afterResolutionCallback
*/
resolveAttributes (item, afterResolutionCallback = null)
{
let element = item[0]
for (const attributeName in this._attributes) {
element[this._generateAttributeName(attributeName)] = this._attributes[attributeName](item)
}
if (this._hasDeepAttributes) {
this._sandbox.load(item.find(this._itemPageLinkSelector).attr('href') + ' ' + this._itemPageDeepAnalysisSelector, () => {
for (const attributeName in this._deepAttributes) {
element[this._generateAttributeName(attributeName)] = this._deepAttributes[attributeName](this._sandbox)
}
this._sandbox.empty()
})
}
}
}