Brazen Item Attributes Resolver

Item attributes resolution helper class

Verzia zo dňa 19.07.2021. Pozri najnovšiu verziu.

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greasyforks.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()
			})
		}
	}
}
长期地址
遇到问题?请前往 GitHub 提 Issues。