Greasy Fork

Brazen Item Attributes Resolver

Item attributes resolution helper class

目前为 2021-07-19 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/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()
			})
		}
	}
}