Main class of the Brazen user scripts framework
Tính đến
Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta
// @require https://update.sleazyfork.org/scripts/416105/1858208/Brazen%20Framework%20-%20Framework.js
// ==UserScript==
// @name Brazen Framework - Framework
// @namespace brazenvoid
// @version 7.4.1
// @author brazenvoid
// @license GPL-3.0-only
// @description Main class of the Brazen user scripts framework
// ==/UserScript==
const ICON_RECYCLE = '♻'
// Identification Classes
const CLASS_COMPLIANT_ITEM = 'brazen-compliant-item'
const CLASS_NON_COMPLIANT_ITEM = 'brazen-noncompliant-item'
// Preset filter configuration keys
const CONFIG_PAGINATOR_LIMIT = 'Pagination Limit'
const CONFIG_PAGINATOR_THRESHOLD = 'Pagination Threshold'
const FILTER_DURATION_RANGE = 'Duration'
const FILTER_PERCENTAGE_RATING_RANGE = 'Rating'
const FILTER_SUBSCRIBED_VIDEOS = 'Hide Subscribed Videos'
const FILTER_TAG_BLACKLIST = 'Tag Blacklist'
const FILTER_TEXT_BLACKLIST = 'Blacklist'
const FILTER_TEXT_SEARCH = 'Search'
const FILTER_TEXT_SANITIZATION = 'Text Sanitization Rules'
const FILTER_TEXT_WHITELIST = 'Whitelist'
const FILTER_UNRATED = 'Unrated'
const STORE_SUBSCRIPTIONS = 'Account Subscriptions'
// Item preset attributes
const ITEM_NAME = 'name'
const ITEM_PROCESSED_ONCE = 'processedOnce'
// Configuration
const OPTION_ENABLE_TEXT_BLACKLIST = 'Enable Text Blacklist'
const OPTION_ENABLE_TAG_BLACKLIST = 'Enable Tag Blacklist'
const OPTION_ALWAYS_SHOW_SETTINGS_PANE = 'Always Show Settings Pane'
const OPTION_DISABLE_COMPLIANCE_VALIDATION = 'Disable All Filters'
const OPTION_ENABLE_DOWNLOAD_DUPLICATE_LEDGER = 'Skip Duplicate Downloads'
/** Default tooltip for {@link OPTION_ENABLE_DOWNLOAD_DUPLICATE_LEDGER}. */
const OPTION_ENABLE_DOWNLOAD_DUPLICATE_LEDGER_HELP =
'Remember prior saves in script storage and skip re-downloads. Off if you removed files and want them again.'
const OPTION_HIDE_DOWNLOADED_MEDIA = 'Hide Downloaded Media'
/** Default tooltip for {@link OPTION_HIDE_DOWNLOADED_MEDIA}. */
const OPTION_HIDE_DOWNLOADED_MEDIA_HELP =
'Hide search tiles whose media is already in the download ledger. Only works while Skip Duplicate Downloads is enabled.'
/** Default cap for optional download duplicate ledger entries in `GM_setValue`. */
const DEFAULT_DOWNLOAD_DUPLICATE_LEDGER_MAX_ENTRIES = 25000
/**
* `GM_download` conflict policy when `downloadDuplicateLedger` is enabled on a site script.
* Never `'uniquify'` — ledger-enabled scripts dictate filenames.
*
* @type {'overwrite'}
*/
const DOWNLOAD_DUPLICATE_LEDGER_CONFLICT_ACTION = 'overwrite'
class BrazenFramework
{
/**
* @typedef {{configKey: string, validate: SearchEnhancerFilterValidationCallback, comply: SearchEnhancerFilterComplianceCallback}} ComplianceFilter
*/
/**
* @typedef {{storageKey?: string, maxEntries?: number, primaryField?: string, legacyIdFields?: string[],
* enableConfigKey?: string, enableHelpText?: string, enableDefault?: boolean,
* isValidId?: (value: *) => boolean,
* getDownloadId: SearchEnhancerDownloadLedgerIdCallback}} DownloadDuplicateLedgerConfiguration
*/
/**
* @typedef {{configKey: string, otherTagSectionsSelector?: JQuery, styleClass: string, rows?: int, helpText: string, removeClasses?: string,
* formatter?: Function}} ItemTagHighlightsConfiguration
*/
/**
* @typedef {{primaryKey: string, primaryOptionKey: string, secondaryKey: string, secondaryOptionKey: string,
* normalizeTagLine: SearchEnhancerTagSelectorGeneratorCallback,
* normalizeRuleLine: SearchEnhancerTagSelectorGeneratorCallback}} TagBlacklistRoutingConfiguration
*/
/**
* @typedef {{doItemCompliance?: Function, downloadDuplicateLedger?: DownloadDuplicateLedgerConfiguration, downloadsDelay?: int,
* isUserLoggedIn?: boolean, itemDeepAnalysisSelector?: JQuery.Selector,
* itemListSelectors: JQuery.Selector, itemLinkSelector?: JQuery.Selector, itemNameSelector: JQuery.Selector, itemSelectors: JQuery.Selector,
* itemSelectionMethod?: string, itemWrapperResolver?: SearchEnhancerItemWrapperResolver|null,
* requestDelay?: number, scriptPrefix: string, tagSelectorGenerator?: SearchEnhancerTagSelectorGeneratorCallback|null,
* trackComplianceRules?: boolean}} Configuration
*/
/**
* @callback SearchEnhancerFilterValidationCallback
* @param {*} configValues
* @return boolean
*/
/**
* @callback SearchEnhancerFilterComplianceCallback
* @param {JQuery} item
* @param {*} configValues
* @return {*}
*/
/**
* @callback SubscriptionsFilterExclusionsCallback
* @return {boolean}
*/
/**
* @callback SubscriptionsFilterUsernameCallback
* @param {JQuery} item
* @return {boolean|string}
*/
/**
* @callback SearchEnhancerItemWorkerCallback
* @param {JQuery} item
*/
/**
* @callback SearchEnhancerItemWrapperResolver
* @param {JQuery} item
* @return {JQuery}
*/
/**
* @callback SearchEnhancerTagsExtractionCallback
* @param {JQuery} item
* @return {string[]}
*/
/**
* @callback SearchEnhancerTagSelectorGeneratorCallback
* @param {string} tag
* @return {string}
*/
/**
* @callback SearchEnhancerDownloadLedgerIdCallback
* @param {*} item - DOM node, jQuery tile, or other site-specific download context
* @return {string|null|undefined}
*/
/**
* @type {Configuration}
* @protected
*/
_config
/**
* Array of item compliance filters
* @type {ComplianceFilter[]}
* @private
*/
_complianceFilters = []
/**
* Routing config for the active-list tag-blacklist APIs (see {@link _setTagBlacklistRouting}).
* @type {TagBlacklistRoutingConfiguration|null}
* @protected
*/
_tagBlacklistRouting = null
/**
* Local storage store with defaults
* @type {BrazenConfigurationManager}
* @protected
*/
_configurationManager
/**
* @type {boolean}
* @protected
*/
_disableUI = false
/**
* @type {boolean}
* @private
*/
_downloadsProcessing = false
/**
* @type {Promise<{name: string|null, url: string}>[]}
* @private
*/
_downloadsQueue = []
/**
* @type {DownloadDuplicateLedgerConfiguration|null}
* @protected
*/
_downloadDuplicateLedgerConfig = null
/**
* @type {Set<string>}
* @protected
*/
_downloadedIds = new Set()
/**
* @type {Set<string>}
* @protected
*/
_enqueuedDownloadIdsThisPage = new Set()
/**
* @type {boolean}
* @private
*/
_downloadDuplicateLedgerEntryKeysBackfilled = false
/**
* @type {string}
* @private
*/
_highlightClasses = ''
/**
* @type {BrazenItemAttributesResolver}
* @protected
*/
_itemAttributesResolver
/**
* Operations to perform after script initialization
* @type {function[]}
* @protected
*/
_onAfterInitialization = []
/**
* Operations to perform after a complete compliance run
* @type {function[]}
* @protected
*/
_onAfterComplianceRun = []
/**
* Operations to perform after UI generation
* @type {function[]}
* @protected
*/
_onAfterUIBuild = []
/**
* Operations to perform before compliance validation.
* @type {function[]}
* @protected
*/
_onBeforeCompliance = []
/**
* Operations to perform before UI generation
* @type {function[]}
* @protected
*/
_onBeforeUIBuild = []
/**
* Operations to perform after compliance rule checks, the first time a search item is retrieved
* @type {function(JQuery)[]}
* @protected
*/
_onFirstHitAfterCompliance = []
/**
* Operations to perform before compliance checks, the first time a search item is retrieved
* @type {function(JQuery)[]}
* @protected
*/
_onFirstHitBeforeCompliance = []
/**
* Logic to hide a non-compliant item
* @type {SearchEnhancerItemWorkerCallback}
* @param {JQuery} item
* @protected
*/
_onItemHide = null
/**
* Logic to show the compliant search item
* @type {Function[]}
* @param {JQuery} item
* @protected
*/
_onItemShow = []
/**
* Validate initiating initialization.
* Can be used to stop script initialization on specific pages or vice versa
* @type {Function}
* @protected
*/
_onValidateInit = () => true
/**
* Pagination manager
* @type BrazenPaginator|null
* @protected
*/
_paginator = null
/**
* @type {boolean}
* @private
*/
_sanitizationEnabled = false
/**
* @type {StatisticsRecorder}
* @protected
*/
_statistics
/**
* @type {ComplianceRuleRecorder|null}
* @protected
*/
_complianceRules = null
/**
* @type {BrazenSubscriptionsLoader|null}
* @protected
*/
_subscriptionsLoader = null
/**
* @type {JQuery}
* @private
*/
_subscriptionsLoaderButton = null
/**
* @type {BrazenViewLayer}
* @protected
*/
_uiGen
/**
* Must return the generated settings section node
* @type {JQuery[]}
* @protected
*/
_userInterface = []
/**
* @param {Configuration} configuration
*/
constructor(configuration)
{
this._config = configuration
if (configuration.isUserLoggedIn === undefined) {
this._config.isUserLoggedIn = false
}
if (configuration.itemSelectionMethod === undefined) {
this._config.itemSelectionMethod = 'find'
}
if (configuration.itemWrapperResolver === undefined) {
this._config.itemWrapperResolver = (item) => item
}
this._itemAttributesResolver = new BrazenItemAttributesResolver({
itemDeepAnalysisSelector: this._config.itemDeepAnalysisSelector ?? '',
itemLinkSelector: this._config.itemLinkSelector ?? '',
requestDelay: this._config.requestDelay ?? 0,
onDeepAttributesResolution: (item) => {
this._complyItem(item)
Utilities.processEventHandlerQueue(this._onAfterComplianceRun)
},
})
this._statistics = new StatisticsRecorder(this._config.scriptPrefix)
if (configuration.trackComplianceRules) {
this._complianceRules = new ComplianceRuleRecorder()
}
this._uiGen = new BrazenViewLayer(this._config.scriptPrefix)
this._configurationManager = new BrazenConfigurationManager(this._config.scriptPrefix, this._uiGen, this._config.tagSelectorGenerator).
onExternalConfigurationChange(() => this._validateCompliance()).
addFlagField(OPTION_DISABLE_COMPLIANCE_VALIDATION, 'Disables all search filters.').
addFlagField(OPTION_ALWAYS_SHOW_SETTINGS_PANE, 'Always show configuration interface.')
this._onItemHide = (item) => this._config.itemWrapperResolver(item).addClass(CLASS_NON_COMPLIANT_ITEM).removeClass(CLASS_COMPLIANT_ITEM).hide()
this._onItemShow.push((item) => this._config.itemWrapperResolver(item).addClass(CLASS_COMPLIANT_ITEM).removeClass(CLASS_NON_COMPLIANT_ITEM).show())
if (configuration.downloadDuplicateLedger?.getDownloadId) {
this._initDownloadDuplicateLedger(configuration.downloadDuplicateLedger)
}
}
/**
* Removes a picture from the page and clears its src so an in-flight fetch can abort.
*
* @param {HTMLImageElement|HTMLVideoElement} mediaElement
* @protected
*/
_removeDownloadMediaElement(mediaElement)
{
if (!(mediaElement instanceof HTMLImageElement)) {
return
}
mediaElement.removeAttribute('srcset')
mediaElement.removeAttribute('sizes')
mediaElement.src = ''
mediaElement.remove()
}
/**
* Decodes HTML entities (e.g. `&` → `&`) from DOM-derived strings.
*
* @param {string} text
* @return {string}
* @protected
*/
_decodeHtmlEntities(text)
{
if (typeof text !== 'string' || text.length === 0) {
return ''
}
if (!text.includes('&')) {
return text
}
let textarea = document.createElement('textarea')
textarea.innerHTML = text
return textarea.value
}
/**
* Sanitizes a single path segment, dropping characters illegal in file names
* (including `/`, so a segment cannot introduce extra nesting).
*
* @param {string} segment
* @return {string}
* @protected
*/
_sanitizePathSegment(segment)
{
return this._decodeHtmlEntities(String(segment)).
replace(/[<>:"/\\|?*]/g, '-').
replace(/[.\s]+$/, '').
trim()
}
/**
* Builds a sanitized, possibly nested, relative download path.
*
* @param {string} folder
* @param {string} name
* @return {string}
* @protected
*/
_buildDownloadPath(folder, name)
{
let folderPath = String(folder).split('/').
map((segment) => this._sanitizePathSegment(segment)).
filter((segment) => segment.length).
join('/').
substring(0, 120).
replace(/[.\s/]+$/, '')
let fileName = this._sanitizePathSegment(name) || 'media'
return folderPath ? folderPath + '/' + fileName : fileName
}
/**
* Polls and observes the DOM until `predicate(element)` is true for the first
* match of `selector`, then invokes `callback(element)` once.
*
* @param {string} selector
* @param {function(Element): boolean} predicate
* @param {function(Element): void} callback
* @param {{pollMs?: number, timeoutMs?: number, observeRoot?: Element}} [options]
* @protected
*/
_waitForDomElement(selector, predicate, callback, options = {})
{
let pollMs = options.pollMs ?? 150
let timeoutMs = options.timeoutMs ?? 30000
let observeRoot = options.observeRoot ?? document.documentElement
let resolved = false
let observer = null
let poll = null
let timeout = null
let cleanup = () => {
observer?.disconnect()
if (poll) {
clearInterval(poll)
}
if (timeout) {
clearTimeout(timeout)
}
}
let attempt = () => {
if (resolved) {
return
}
let element = document.querySelector(selector)
if (!element || !predicate(element)) {
return
}
resolved = true
cleanup()
callback(element)
}
attempt()
if (resolved) {
return
}
observer = new MutationObserver(attempt)
observer.observe(observeRoot, {subtree: true, childList: true, attributes: true, attributeFilter: ['src']})
poll = setInterval(attempt, pollMs)
timeout = setTimeout(cleanup, timeoutMs)
}
/**
* @param {string} tag
* @param {function(string): string} normalizeToken
* @return {string}
* @protected
*/
_normalizeDownloadTagRuleToken(tag, normalizeToken)
{
return normalizeToken(tag)
}
/**
* @param {string[]} lines
* @param {function(string): string} normalizeToken
* @return {{subject: string, replacement: string}[]}
* @protected
*/
_parseDownloadTagSubstitutionLines(lines, normalizeToken)
{
let rules = []
for (let line of lines) {
let match = line.match(/^(.+?)\s+-\s+(.+)$/)
if (!match) {
continue
}
let subject = this._normalizeDownloadTagRuleToken(match[1], normalizeToken)
let replacement = this._normalizeDownloadTagRuleToken(match[2], normalizeToken)
if (subject.length && replacement.length) {
rules.push({subject, replacement})
}
}
return rules
}
/**
* @param {{subject: string, replacement: string}[]} rules
* @return {Map<string, string>}
* @protected
*/
_buildDownloadTagSubstitutionMap(rules)
{
let map = new Map()
for (let rule of rules) {
map.set(rule.subject, rule.replacement)
}
return map
}
/**
* @param {string} tag
* @param {string|null} type
* @param {Map<string, string>} substitutions
* @param {boolean} stripCharacterSeries
* @return {string}
* @protected
*/
_resolveDownloadTagSubstitution(tag, type, substitutions, stripCharacterSeries)
{
if (substitutions.has(tag)) {
return substitutions.get(tag)
}
if (type === 'character' && stripCharacterSeries) {
let stripped = tag.replace(/_\([^)]*\)$/, '')
if (stripped !== tag && substitutions.has(stripped)) {
return substitutions.get(stripped)
}
}
return tag
}
/**
* @param {string[]} tags
* @param {string|null} type
* @param {Map<string, string>} substitutions
* @param {boolean} stripCharacterSeries
* @return {string[]}
* @protected
*/
_applyDownloadTagSubstitutions(tags, type, substitutions, stripCharacterSeries)
{
if (!substitutions.size) {
return tags
}
return tags.map((tag) => this._resolveDownloadTagSubstitution(tag, type, substitutions, stripCharacterSeries))
}
/**
* @param {string} tag
* @param {string|null} type
* @param {boolean} stripCharacterSeries
* @return {string}
* @protected
*/
_formatTagForDownloadPath(tag, type, stripCharacterSeries)
{
let formatted = this._decodeHtmlEntities(tag)
if (type === 'character' && stripCharacterSeries) {
formatted = formatted.replace(/_\([^)]*\)$/, '')
}
return formatted.replaceAll('_', ' ')
}
/**
* @param {string[]} tags
* @param {string|null} type
* @param {Set<string>|null} ignore
* @param {{substitutions: Map<string, string>, stripCharacterSeries: boolean, multiTagSeparator: string}} options
* @return {string}
* @protected
*/
_joinTagsForDownloadPath(tags, type, ignore, options)
{
tags = this._applyDownloadTagSubstitutions(tags, type, options.substitutions, options.stripCharacterSeries)
if (ignore?.size) {
tags = tags.filter((tag) => !ignore.has(tag))
}
let seen = new Set()
let unique = []
for (let tag of tags) {
let formatted = this._formatTagForDownloadPath(tag, type, options.stripCharacterSeries)
if (!formatted || seen.has(formatted)) {
continue
}
seen.add(formatted)
unique.push(formatted)
}
return unique.
sort((left, right) => left.localeCompare(right, undefined, {sensitivity: 'base'})).
join(options.multiTagSeparator)
}
/**
* @param {string} pattern
* @param {string} type
* @param {{token: string, label: string}[]} chips
* @param {Set<string>} unknownTypes
* @return {boolean}
* @protected
*/
_patternEmploysDownloadTagTypeToken(pattern, type, chips, unknownTypes)
{
if (!unknownTypes.has(type)) {
return false
}
for (let chip of chips) {
if (chip.token === type && pattern.includes(chip.label)) {
return true
}
}
return false
}
/**
* @param {string} pattern
* @param {string[]} tags
* @param {string} type
* @param {Set<string>} ignore
* @param {{chips: {token: string, label: string}[], unknownTypes: Set<string>, unknownDefault: string, substitutions: Map<string, string>, stripCharacterSeries: boolean, multiTagSeparator: string}} resolver
* @return {string}
* @protected
*/
_resolveDownloadTagTypeTokenForPath(pattern, tags, type, ignore, resolver)
{
let value = this._joinTagsForDownloadPath(tags, type, ignore, resolver)
if (!value && this._patternEmploysDownloadTagTypeToken(pattern, type, resolver.chips, resolver.unknownTypes)) {
return resolver.unknownDefault
}
return value
}
/**
* @param {string} pattern
* @param {{}} data
* @param {{}} tagGroups
* @param {{chips: {token: string, label: string}[], tagTypes: string[], unknownTypes: Set<string>, unknownDefault: string, ignore: Set<string>, substitutions: Map<string, string>, stripCharacterSeries: boolean, multiTagSeparator: string}} resolver
* @return {string}
* @protected
*/
_resolveDownloadPattern(pattern, data, tagGroups, resolver)
{
let resolved = pattern
let chips = [...resolver.chips].sort((a, b) => b.label.length - a.label.length)
for (let chip of chips) {
let value
if (resolver.tagTypes.includes(chip.token)) {
value = this._resolveDownloadTagTypeTokenForPath(pattern, tagGroups[chip.token] ?? [], chip.token, resolver.ignore, resolver)
} else {
value = data[chip.token] ?? ''
}
resolved = resolved.replaceAll(chip.label, value)
}
return resolved.replace(/\s+/g, ' ').trim()
}
/**
* @param {HTMLImageElement|HTMLVideoElement} mediaElement
* @param {string} folder
* @param {string} name
* @param {boolean} removeMediaOnSuccess
* @protected
*/
_addDownload(mediaElement, folder, name, removeMediaOnSuccess = false)
{
const path = this._removeIllegalCharactersFromPath(folder).substring(0, 120).trim().replace(/[.\s]+$/, '') + '/' + this._removeIllegalCharactersFromPath(name)
let url = mediaElement.src ?? ''
let downloadElement = null
let removedPicture = false
if (removeMediaOnSuccess && mediaElement instanceof HTMLImageElement) {
this._removeDownloadMediaElement(mediaElement)
removedPicture = true
} else if (removeMediaOnSuccess) {
downloadElement = $(mediaElement)
}
let downloadId = this._getDownloadDuplicateLedgerId(mediaElement)
if (this._isDownloadDuplicateLedgerActive() && !this._claimDownloadDuplicateLedgerSlot(downloadId)) {
this._handleDuplicateDownloadSkipped({name: path})
return
}
let task = this._wrapDownloadTask({
name: path,
element: downloadElement,
url,
downloadId,
})
if (mediaElement instanceof HTMLImageElement && !mediaElement.complete && !removedPicture) {
mediaElement.addEventListener('load', () => this._downloadsQueue.push(task))
} else {
this._downloadsQueue.push(task)
}
// noinspection JSIgnoredPromiseFromCall
this._startProcessingDownloadQueue()
}
/**
* @param {string} helpText
* @param rows
* @protected
*/
_addItemBlacklistFilter(helpText, rows = 5)
{
this._configurationManager.addFlagField(OPTION_ENABLE_TEXT_BLACKLIST, 'Applies the blacklist.').addRulesetField(
FILTER_TEXT_BLACKLIST,
rows,
helpText,
null,
null,
(rules) => Utilities.buildWholeWordMatchingRegex(rules) ?? '',
true
)
this._addItemComplexComplianceFilter(
FILTER_TEXT_BLACKLIST,
(rules) => this._getConfig(OPTION_ENABLE_TEXT_BLACKLIST) && rules !== '',
(item, value) => this._get(item, ITEM_NAME)?.match(value) === null,
)
}
/**
* @param {string} configKey
* @param {SearchEnhancerFilterValidationCallback|null} validationCallback
* @param {SearchEnhancerFilterComplianceCallback|null|string} complianceCallback
* @protected
*/
_addItemComplexComplianceFilter(configKey, validationCallback, complianceCallback)
{
this._addItemComplianceFilter(configKey, complianceCallback, validationCallback)
}
/**
* @param {string} configKey
* @param {SearchEnhancerFilterComplianceCallback|null|string} action
* @param {SearchEnhancerFilterValidationCallback|null} validationCallback
* @protected
*/
_addItemComplianceFilter(configKey, action = null, validationCallback = null)
{
let configType = this._configurationManager.getField(configKey).type
if (action === null) {
action = configKey
}
if (typeof action === 'string') {
let attributeName = action
switch (configType) {
case CONFIG_TYPE_CHECKBOXES_GROUP:
action = (item, values) => {
let attribute = this._get(item, attributeName)
return attribute && values.length ? values.includes(attribute) : true
}
break
case CONFIG_TYPE_FLAG:
action = (item) => {
let attribute = this._get(item, attributeName)
return attribute === null ? true : attribute
}
break
case CONFIG_TYPE_RADIOS_GROUP:
action = (item, value) => {
let attribute = this._get(item, attributeName)
return attribute ? value === attribute : true
}
break
case CONFIG_TYPE_RANGE:
action = (item, range) => {
let attribute = this._get(item, attributeName)
return attribute ? Validator.isInRange(this._get(item, attributeName), range.minimum, range.maximum) : true
}
break
default:
throw new Error('Associated config type requires explicit action callback definition.')
}
}
if (validationCallback === null) {
validationCallback = this._configurationManager.generateValidationCallback(configKey)
}
this._complianceFilters.push({
configKey: configKey,
validate: validationCallback,
comply: action,
})
}
/**
* @param {JQuery.Selector|Function} durationNodeSelector
* @param {string|null} helpText
* @param {string} separator
* @protected
*/
_addItemDurationRangeFilter(durationNodeSelector, helpText = null, separator = ':')
{
this._configurationManager.addRangeField(FILTER_DURATION_RANGE, 0, 100000, helpText ?? 'Filter items by duration.')
this._itemAttributesResolver.addAttribute(FILTER_DURATION_RANGE, (item) => {
let duration
if (typeof durationNodeSelector === 'function') {
duration = durationNodeSelector(item)
} else {
let durationNode = item.find(durationNodeSelector)
if (durationNode.length) {
duration = durationNode.text().trim()
} else {
return null
}
}
duration = duration.split(separator)
duration = (Number.parseInt(duration[0]) * 60) + Number.parseInt(duration[1])
return duration === 0 ? null : duration
})
this._addItemComplianceFilter(FILTER_DURATION_RANGE)
}
/**
* @param {JQuery.Selector} ratingNodeSelector
* @param {string|null} helpText
* @param {string|null} unratedHelpText
* @protected
*/
_addItemPercentageRatingRangeFilter(ratingNodeSelector, helpText = null, unratedHelpText = null)
{
this._configurationManager.addRangeField(FILTER_PERCENTAGE_RATING_RANGE, 0, 100000,
helpText ?? 'Filter items by percentage rating.').addFlagField(
FILTER_UNRATED, unratedHelpText ?? 'Hide items with zero or no rating.')
this._itemAttributesResolver.addAttribute(FILTER_PERCENTAGE_RATING_RANGE, (item) => {
let rating = item.find(ratingNodeSelector)
return rating.length === 0 ? null : Number.parseInt(rating.text().trim().replace('%', ''))
})
this._addItemComplianceFilter(FILTER_PERCENTAGE_RATING_RANGE, (item, range) => {
let rating = this._get(item, FILTER_PERCENTAGE_RATING_RANGE)
return rating ? Validator.isInRange(rating, range.minimum, range.maximum) : !this._getConfig(FILTER_UNRATED)
})
}
/**
* @param {string} key
* @param {boolean} deepAttribute
* @param {boolean} saveSelectors
* @param {SearchEnhancerTagsExtractionCallback} extractTags
* @protected
*/
_addItemTagAttribute(key, deepAttribute, saveSelectors, extractTags)
{
let tagsToSelectorsMapper = (item) => {
if (saveSelectors) {
let tagSelectors = ''
for (let tag of extractTags(item)) {
tagSelectors += this._config.tagSelectorGenerator(tag)
}
return tagSelectors
}
let tags = []
for (let tag of extractTags(item)) {
tags.push(tag)
}
return tags
}
if (deepAttribute) {
this._itemAttributesResolver.addDeepAttribute(key, tagsToSelectorsMapper)
} else {
this._itemAttributesResolver.addAttribute(key, tagsToSelectorsMapper)
}
}
/**
* @param {string|null} attribute
* @param {boolean} useSelectors
* @param {int} rows
* @param {string|null} helpText
* @param {string|null} key
* @param {string|null} optionKey
* @protected
*/
_addItemTagBlacklistFilter(attribute, useSelectors, rows = 5, helpText = null, key = null, optionKey = null)
{
if (helpText === null) {
helpText = 'Specify the tags blacklist with one rule on each line. <br> Conditional operators; "&" "|" can be used to make complex rules.'
}
if (key === null) {
key = FILTER_TAG_BLACKLIST
}
if (optionKey === null) {
optionKey = OPTION_ENABLE_TAG_BLACKLIST
}
this._configurationManager.
addFlagField(optionKey, 'Applies the blacklist.').
addTagRulesetField(key, useSelectors, rows, helpText, null, true)
this._addItemComplexComplianceFilter(
key,
(rules) => this._getConfig(optionKey) && rules.length,
(item, blacklistRuleset) => this._handleComplianceForBlacklistFilter(item, attribute, blacklistRuleset),
)
}
/**
* Hides items whose media is already recorded in the download duplicate ledger. Gated on the
* ledger being active, so the filter is inert (no stat hit) while {@link OPTION_ENABLE_DOWNLOAD_DUPLICATE_LEDGER}
* is off.
*
* @param {SearchEnhancerDownloadLedgerIdCallback} getItemDownloadId Resolves a per-item ledger id from a tile
* @param {string|null} helpText
* @param {string} optionKey
* @protected
*/
_addItemHideDownloadedMediaFilter(getItemDownloadId, helpText = null, optionKey = OPTION_HIDE_DOWNLOADED_MEDIA)
{
this._configurationManager.addFlagField(optionKey, helpText ?? OPTION_HIDE_DOWNLOADED_MEDIA_HELP)
this._addItemComplexComplianceFilter(
optionKey,
(enabled) => enabled && this._isDownloadDuplicateLedgerActive(),
(item) => {
let id = getItemDownloadId(item)
return id !== null && id !== undefined && this._isDownloadDuplicate(id)
? {complies: false, rule: 'Already downloaded'}
: true
},
)
}
/**
* @param {ItemTagHighlightsConfiguration} config
* @protected
*/
_addItemTagHighlights(config)
{
this.registerHighlightStyleClass(config.styleClass)
this._configurationManager.addTagRulesetField(
config.configKey, true, config.rows ?? 5, config.helpText ?? '', config.formatter ?? null, true)
let highlightsHandler = (section) => {
let optimizedRuleset = this._configurationManager.getField(config.configKey).optimized
if (optimizedRuleset) {
let ruleApplies, subjectTags
for (let rule of optimizedRuleset) {
ruleApplies = true
subjectTags = section.find(rule.join(', '))
for (let tagSelector of rule) {
if (section.find(tagSelector).length === 0) {
ruleApplies = false
break
}
}
if (ruleApplies) {
subjectTags.addClass(config.styleClass)
if (config.removeClasses !== undefined) {
subjectTags.removeClass(config.removeClasses)
}
} else {
subjectTags.removeClass(config.styleClass)
}
}
}
}
if (config.otherTagSectionsSelector && config.otherTagSectionsSelector.length > 0) {
this._onBeforeUIBuild.push(() => highlightsHandler(config.otherTagSectionsSelector))
}
this._onItemShow.push((item) => highlightsHandler(item))
}
/**
* @param {string} helpText
* @protected
*/
_addItemTextSanitizationFilter(helpText)
{
this._sanitizationEnabled = true
this._configurationManager.addRulesetField(FILTER_TEXT_SANITIZATION, 2, helpText, (rules) => {
let sanitizationRules = {}, fragments, validatedTargetWords
for (let sanitizationRule of rules) {
if (sanitizationRule.includes('=')) {
fragments = sanitizationRule.split('=')
if (fragments[0] === '') {
fragments[0] = ' '
}
validatedTargetWords = Utilities.trimAndKeepNonEmptyStrings(fragments[1].split(','))
if (validatedTargetWords.length) {
sanitizationRules[fragments[0]] = validatedTargetWords
}
}
}
return sanitizationRules
}, (rules) => {
let sanitizationRulesText = []
for (let substitute in rules) {
sanitizationRulesText.push(substitute + '=' + rules[substitute].join(','))
}
return sanitizationRulesText
}, (rules) => {
let optimizedRules = {}
for (const substitute in rules) {
optimizedRules[substitute] = Utilities.buildWholeWordMatchingRegex(rules[substitute])
}
return optimizedRules
}, true)
}
/**
* @param {string|null} helpText
* @protected
*/
_addItemTextSearchFilter(helpText = null)
{
this._configurationManager.addTextField(FILTER_TEXT_SEARCH,
helpText ?? 'Show videos with these comma separated words in their names.')
this._addItemComplianceFilter(FILTER_TEXT_SEARCH, (item, value) => this._get(item, ITEM_NAME).includes(value))
}
/**
* @param {string} helpText
* @protected
*/
_addItemWhitelistFilter(helpText)
{
this._configurationManager.addRulesetField(
FILTER_TEXT_WHITELIST,
5,
helpText,
null,
null,
(rules) => Utilities.buildWholeWordMatchingRegex(rules),
true)
}
/**
* @param {SubscriptionsFilterExclusionsCallback} exclusionsCallback Add page exclusions here
* @param {SubscriptionsFilterUsernameCallback} getItemUsername Return username of the item or return false to skip
* @protected
*/
_addSubscriptionsFilter(exclusionsCallback, getItemUsername)
{
this._configurationManager.addFlagField(FILTER_SUBSCRIBED_VIDEOS, 'Hide videos from subscribed channels.').
addTextField(STORE_SUBSCRIPTIONS, 'Recorded subscription accounts.')
this._addItemComplexComplianceFilter(
FILTER_SUBSCRIBED_VIDEOS,
(value) => value && this._config.isUserLoggedIn && exclusionsCallback(),
(item) => {
let username = getItemUsername(item)
if (username === false) {
return true
}
return !(new RegExp('"([^"]*' + username + '[^"]*)"')).test(this._getConfig(STORE_SUBSCRIPTIONS))
})
}
/**
* @param {JQuery} item
* @protected
*/
_complyItem(item)
{
let itemComplies = true
let doItemCompliance = true
if (this._config.doItemCompliance) {
doItemCompliance = Utilities.callEventHandler(this._config.doItemCompliance)
}
if (doItemCompliance && !this._getConfig(OPTION_DISABLE_COMPLIANCE_VALIDATION) && this._validateItemWhiteList(item)) {
let configField
Utilities.processEventHandlerQueue(this._onBeforeCompliance, [item])
for (let complianceFilter of this._complianceFilters) {
configField = this._configurationManager.getFieldOrFail(complianceFilter.configKey)
if (complianceFilter.validate(configField.optimized ?? configField.value)) {
let complyResult = complianceFilter.comply(item, configField.optimized ?? configField.value)
let {complies, rule} = this._unwrapComplianceResult(complyResult)
itemComplies = complies
this._statistics.record(complianceFilter.configKey, itemComplies)
if (!itemComplies) {
if (this._complianceRules) {
let ruleLabel = rule ?? this._configurationManager.getField(complianceFilter.configKey)?.title ?? complianceFilter.configKey
this._complianceRules.record(complianceFilter.configKey, ruleLabel)
}
break
}
}
}
}
if (itemComplies) {
Utilities.processEventHandlerQueue(this._onItemShow, [item])
} else {
Utilities.callEventHandler(this._onItemHide, [item])
}
item.css('opacity', 'unset')
}
/**
* Filters items as per settings
* @param {JQuery} itemsList
* @param {boolean} fromObserver
* @protected
*/
_complyItemsList(itemsList, fromObserver = false)
{
let items
if (fromObserver) {
items = itemsList.filter(this._config.itemSelectors).add(itemsList.find(this._config.itemSelectors))
} else if (this._config.itemSelectionMethod === 'find') {
items = itemsList.find(this._config.itemSelectors)
} else {
items = itemsList.children(this._config.itemSelectors)
}
items.css('opacity', 0.75).each((index, element) => {
let item = $(element)
// First run processing
if (this._get(item, ITEM_PROCESSED_ONCE) === null) {
if (this._sanitizationEnabled) {
Validator.sanitizeTextNode(
item.find(this._config.itemNameSelector),
this._configurationManager.getFieldOrFail(FILTER_TEXT_SANITIZATION).optimized)
}
this._itemAttributesResolver.resolveAttributes(item)
Utilities.processEventHandlerQueue(this._onFirstHitBeforeCompliance, [item])
}
// Compliance filtering
this._complyItem(item)
// Processing of search items on later runs
if (!this._get(item, ITEM_PROCESSED_ONCE)) {
Utilities.processEventHandlerQueue(this._onFirstHitAfterCompliance, [item])
this._itemAttributesResolver.set(item, ITEM_PROCESSED_ONCE, true)
}
})
this._statistics.updateUI()
Utilities.processEventHandlerQueue(this._onAfterComplianceRun)
}
/**
* @protected
* @return {JQuery[]}
*/
_createPaginationControls()
{
return [
this._configurationManager.createElement(CONFIG_PAGINATOR_THRESHOLD),
this._configurationManager.createElement(CONFIG_PAGINATOR_LIMIT)]
}
/**
* @protected
* @return {JQuery}
*/
_createSettingsBackupRestoreFormActions()
{
return this._uiGen.createFormActions([
this._uiGen.createFormButton('Backup Configuration', 'Download configuration file.', () => this._onBackupSettings()),
this._uiGen.createSeparator(),
this._uiGen.createFormGroupInput('file').attr('id', 'restore-settings').attr('placeholder', 'Browse for settings file...'),
this._uiGen.createFormButton('Restore Configuration', 'Restore configuration from the selected file.', () => this._onRestoreSettings()),
], 'bv-flex-column')
}
/**
* @protected
* @return {JQuery}
*/
_createSettingsFormActions()
{
return this._uiGen.createFormActions([
this._uiGen.createFormButton('Apply', 'Apply settings.', () => this._onApplyNewSettings()),
this._uiGen.createFormButton('Save', 'Apply and update saved configuration.', () => this._onSaveSettings()),
this._uiGen.createFormButton('Reset', 'Revert to saved configuration.', () => this._onResetSettings()),
])
}
/**
* @protected
* @return {JQuery}
*/
_createSubscriptionLoaderControls()
{
return this._subscriptionsLoaderButton
}
/**
* @param {JQuery} UISection
* @private
*/
_embedUI(UISection)
{
UISection.on('mouseleave', (event) => {
if (!this._uiGen.isSettingsPaneBeingResized() && !this._getConfig(OPTION_ALWAYS_SHOW_SETTINGS_PANE)) {
$(event.currentTarget).slideUp(300)
}
})
if (this._getConfig(OPTION_ALWAYS_SHOW_SETTINGS_PANE)) {
UISection.show()
}
this._uiGen.constructor.appendToBody(UISection)
this._uiGen.constructor.appendToBody(this._uiGen.createSettingsShowButton('', UISection))
}
/**
* @param {JQuery} item
* @param {string} attributeName
* @returns {*}
* @protected
*/
_get(item, attributeName)
{
return this._itemAttributesResolver.get(item, attributeName)
}
/**
* @param {string} config
* @returns {*}
* @protected
*/
_getConfig(config)
{
return this._configurationManager.getValue(config)
}
/**
* @param {*} result
* @return {{complies: boolean, rule: string|null}}
* @protected
*/
_unwrapComplianceResult(result)
{
if (result !== null && typeof result === 'object' && 'complies' in result) {
return {complies: !!result.complies, rule: result.rule ?? null}
}
return {complies: !!result, rule: null}
}
/**
* @return {{filterKey: string, filterLabel: string, rules: {label: string, count: number}[]}[]}
* @protected
*/
_getComplianceRuleReport()
{
if (!this._complianceRules) {
return []
}
return this._complianceRules.getReport((filterKey) => this._configurationManager.getField(filterKey)?.title ?? filterKey)
}
/**
* @protected
*/
_showComplianceRulesModal()
{
let report = this._getComplianceRuleReport()
let bodyNodes = []
let modal
if (!report.length) {
bodyNodes.push($('<p>').text('No items hidden on this page.'))
} else {
for (let section of report) {
bodyNodes.push($('<h3>').text(section.filterLabel))
let list = $('<ul class="bv-compliance-rule-list">')
for (let rule of section.rules) {
let row = $('<li class="bv-compliance-rule-row">')
row.append($('<span class="bv-compliance-rule-label">').text(rule.label + ' — ' + rule.count + ' hidden'))
if (this._canRemoveComplianceRule(section.filterKey, rule.label)) {
row.append(
this._uiGen.createFormButton('Remove', 'Remove this rule from the filter list.', () => {
this._removeComplianceRule(section.filterKey, rule.label)
this._uiGen.hideModal(modal)
this._showComplianceRulesModal()
}).addClass('bv-compliance-rule-remove'),
)
}
list.append(row)
}
bodyNodes.push(list)
}
}
modal = this._uiGen.createModal('Active Hide Rules', bodyNodes)
this._uiGen.showModal(modal)
}
/**
* @param {string} filterKey
* @param {string} ruleLabel
* @return {boolean}
* @protected
*/
_canRemoveComplianceRule(filterKey, ruleLabel)
{
return false
}
/**
* @param {string} filterKey
* @param {string} ruleLabel
* @protected
*/
_removeComplianceRule(filterKey, ruleLabel)
{
}
_handleComplianceForBlacklistFilter(item, attribute, blacklistRuleset)
{
let isBlacklisted
let itemTags = this._get(item, attribute)
if (itemTags !== null && itemTags.length) {
for (let rule of blacklistRuleset) {
isBlacklisted = true
for (let tag of rule) {
if (!itemTags.includes(tag)) {
isBlacklisted = false
break
}
}
if (isBlacklisted) {
return {complies: false, rule: rule.join(' & ')}
}
}
}
return true
}
/**
* Registers routing for the active-list tag-blacklist APIs. The two normalizers stay
* site-supplied (tag canonicalization is site specific); all visual rendering remains in the app.
*
* @param {TagBlacklistRoutingConfiguration} routing
* @protected
*/
_setTagBlacklistRouting(routing)
{
this._tagBlacklistRouting = routing
}
/**
* @return {string} The tag-blacklist field key sidebar toggles read from and write to.
* @protected
*/
_getActiveTagBlacklistFieldKey()
{
let routing = this._tagBlacklistRouting
return this._getConfig(routing.secondaryOptionKey) ? routing.secondaryKey : routing.primaryKey
}
/**
* @param {string} line
* @param {string} normalizedName
* @return {boolean}
* @protected
*/
_tagBlacklistLineMatchesTag(line, normalizedName)
{
let rulePart = line
let commentIdx = line.indexOf(' // ')
if (commentIdx >= 0) {
rulePart = line.slice(0, commentIdx)
}
rulePart = rulePart.trim()
if (!rulePart || rulePart.includes('&') || rulePart.includes('|')) {
return false
}
return this._tagBlacklistRouting.normalizeRuleLine(rulePart) === normalizedName
}
/**
* @param {string} fieldKey
* @param {string} name
* @return {boolean}
* @protected
*/
_isTagInTagBlacklist(fieldKey, name)
{
let normalized = this._tagBlacklistRouting.normalizeTagLine(name)
return this._getConfig(fieldKey).some((line) => this._tagBlacklistLineMatchesTag(line, normalized))
}
/**
* @param {string} name
* @return {boolean}
* @protected
*/
_isTagInActiveTagBlacklist(name)
{
return this._isTagInTagBlacklist(this._getActiveTagBlacklistFieldKey(), name)
}
/**
* Adds or removes a single tag token from the active tag-blacklist list, enabling the active
* option first if it is off. Does not refresh sidebar visuals — callers do that.
*
* @param {string} name
* @protected
*/
_toggleTagInActiveTagBlacklist(name)
{
let routing = this._tagBlacklistRouting
let fieldKey = this._getActiveTagBlacklistFieldKey()
let optionKey = fieldKey === routing.secondaryKey ? routing.secondaryOptionKey : routing.primaryOptionKey
let enableField = this._configurationManager.getField(optionKey)
if (enableField && !enableField.value) {
enableField.value = true
if (enableField.element) {
enableField.updateUserInterface()
}
}
let field = this._configurationManager.getField(fieldKey)
if (!field) {
return
}
let rule = routing.normalizeTagLine(name)
let lines = [...field.value]
let index = lines.findIndex((line) => this._tagBlacklistLineMatchesTag(line, rule))
if (index >= 0) {
lines.splice(index, 1)
} else {
lines.push(rule)
}
this._updateRulesetField(field, lines)
this._configurationManager.save()
this._validateCompliance()
}
/**
* Empties a tag-blacklist list. Does not refresh sidebar visuals — callers do that.
*
* @param {string} fieldKey
* @protected
*/
_clearTagBlacklistField(fieldKey)
{
let field = this._configurationManager.getField(fieldKey)
if (!field) {
return
}
this._updateRulesetField(field, [])
this._configurationManager.save()
this._validateCompliance()
}
/**
* Applies new lines to a ruleset field through its own translate/sort/optimize hooks and
* re-renders its UI. Generic — works for any ruleset field, not just tag blacklists.
*
* @param {ConfigurationField} field
* @param {string[]} lines
* @protected
*/
_updateRulesetField(field, lines)
{
if (field.onTranslateFromUI) {
lines = Utilities.callEventHandler(field.onTranslateFromUI, [lines], lines)
}
if (field.sortRules) {
lines = lines.sort((left, right) => left.localeCompare(right, undefined, {sensitivity: 'base'}))
}
field.value = lines
field.optimized = Utilities.callEventHandler(field.onOptimize, [field.value])
if (field.element) {
field.updateUserInterface()
}
}
/**
* @private
*/
_onApplyNewSettings()
{
this._configurationManager.update()
this._validateCompliance()
}
/**
* @private
*/
_onBackupSettings()
{
this._configurationManager.backup()
}
/**
* @private
*/
_onResetSettings()
{
this._configurationManager.revertChanges()
this._validateCompliance()
}
/**
* @private
*/
_onRestoreSettings()
{
this._configurationManager.restore(new Response($('#restore-settings').prop('files')[0])).then(() => this._validateCompliance())
}
/**
* @private
*/
_onSaveSettings()
{
this._onApplyNewSettings()
this._configurationManager.save()
}
/**
* @param {string} configKey
* @param {function(*)} actionCallback
* @param {function(*, function?): boolean} validationCallback
* @returns {BrazenFramework}
* @protected
*/
_performComplexOperation(configKey, validationCallback, actionCallback)
{
return this._performOperation(configKey, actionCallback, validationCallback)
}
/**
* @param {string} configKey
* @param {function(*)} actionCallback
* @param {function(*, function?): boolean|null} validationCallback
* @returns {BrazenFramework}
* @protected
*/
_performOperation(configKey, actionCallback, validationCallback = null)
{
let configField = this._configurationManager.getField(configKey)
let defaultValidationCallback = this._configurationManager.generateValidationCallback(configKey)
let validationCallbackParams
let values = configField.optimized ?? configField.value
if (validationCallback) {
validationCallbackParams = [values, defaultValidationCallback]
} else {
validationCallbackParams = [values]
validationCallback = defaultValidationCallback
}
if (Utilities.callEventHandler(validationCallback, validationCallbackParams, true)) {
actionCallback(values)
}
return this
}
/**
* @param {string} flagConfigKey
* @param {string|null} configKey
* @param {function(*)} actionCallback
* @param {function(*, function?): boolean} validationCallback
* @returns {BrazenFramework}
* @protected
*/
_performTogglableComplexOperation(flagConfigKey, configKey, validationCallback, actionCallback)
{
if (this._getConfig(flagConfigKey)) {
this._performComplexOperation(configKey ?? flagConfigKey, validationCallback, actionCallback)
}
return this
}
/**
* @param {string} flagConfigKey
* @param {string} configKey
* @param {function(*)} actionCallback
* @param {function(*, function?): boolean|null} validationCallback
* @returns {BrazenFramework}
* @protected
*/
_performTogglableOperation(flagConfigKey, configKey, actionCallback, validationCallback = null)
{
if (this._configurationManager.getValue(flagConfigKey)) {
this._performOperation(configKey, actionCallback, validationCallback)
}
return this
}
/**
* @param {string} path
* @private
*/
_removeIllegalCharactersFromPath(path)
{
return path.replace(/[<>:"/\\|?*]/g, '-').replace(/[.\s]+$/, '').trim()
}
/**
* @param {boolean} enableCondition
* @param {PaginatorConfiguration} configuration
* @protected
*/
_setupPaginator(enableCondition, configuration)
{
if (enableCondition) {
configuration.itemSelectors = this._config.itemSelectors
this._paginator = new BrazenPaginator(configuration)
}
this._configurationManager.addNumberField(CONFIG_PAGINATOR_LIMIT, 1, 50,
'Limit paginator to concatenate the specified number of maximum pages.').
addNumberField(CONFIG_PAGINATOR_THRESHOLD, 1, 1000,
'Make paginator ensure the specified number of minimum results.')
}
/**
* @return {BrazenSubscriptionsLoader}
* @protected
*/
_setupSubscriptionLoader()
{
this._subscriptionsLoader = new BrazenSubscriptionsLoader(
(status) => this._subscriptionsLoaderButton.text(status),
(subscriptions) => {
this._configurationManager.getField(STORE_SUBSCRIPTIONS).value = subscriptions.length ? '"' +
subscriptions.join('""') + '"' : ''
this._configurationManager.save()
$('#subscriptions-loader').prop('disabled', false)
})
this._subscriptionsLoaderButton = this._uiGen.createFormButton(
'Load Subscriptions',
'Makes a copy of your subscriptions in cache for related filters.',
(event) => {
if (this._config.isUserLoggedIn) {
$(event.currentTarget).prop('disabled', true)
this._subscriptionsLoader.run()
} else {
this._showNotLoggedInAlert()
}
}).attr('id', 'subscriptions-loader')
return this._subscriptionsLoader
}
/**
* @protected
*/
_showNotLoggedInAlert()
{
alert('You need to be logged in to use this functionality')
}
/**
* @return {Promise<void>}
* @private
*/
async _startProcessingDownloadQueue()
{
if (this._downloadsProcessing) {
return
}
this._downloadsProcessing = true
while (this._downloadsQueue.length > 0) {
await this._downloadsQueue.shift()
await Utilities.sleep(this._config.downloadsDelay)
}
this._downloadsProcessing = false
}
/**
* @param {boolean} firstRun
* @protected
*/
_validateCompliance(firstRun = false)
{
let itemLists = $(this._config.itemListSelectors)
if (firstRun) {
itemLists.each((index, itemList) => {
let itemListObject = $(itemList)
if (this._paginator && itemListObject.is(this._paginator.getItemListSelector())) {
ChildObserver.create().onNodesAdded((itemsAdded) => {
this._complyItemsList($(itemsAdded), true)
this._paginator.run(this._getConfig(CONFIG_PAGINATOR_THRESHOLD), this._getConfig(CONFIG_PAGINATOR_LIMIT))
}).observe(itemList)
} else {
ChildObserver.create().onNodesAdded((itemsAdded) => {
this._complyItemsList($(itemsAdded), true)
}).observe(itemList)
}
this._complyItemsList(itemListObject)
})
} else {
this._statistics.reset()
if (this._complianceRules) {
this._complianceRules.reset()
}
itemLists.each((index, itemsList) => {
this._complyItemsList($(itemsList))
})
}
if (this._paginator) {
this._paginator.run(this._getConfig(CONFIG_PAGINATOR_THRESHOLD), this._getConfig(CONFIG_PAGINATOR_LIMIT))
}
this._itemAttributesResolver.completeResolutionRun()
}
/**
* @param {JQuery} item
* @return {boolean}
* @protected
*/
_validateItemWhiteList(item)
{
let field = this._configurationManager.getField(FILTER_TEXT_WHITELIST)
if (field) {
let validationResult = field.value.length
? Validator.regexMatches(this._get(item, ITEM_NAME), field.optimized)
: true
this._statistics.record(FILTER_TEXT_WHITELIST, validationResult)
return validationResult
}
return true
}
/**
* @param {DownloadDuplicateLedgerConfiguration} ledgerConfig
* @private
*/
_initDownloadDuplicateLedger(ledgerConfig)
{
let enableConfigKey = ledgerConfig.enableConfigKey ?? OPTION_ENABLE_DOWNLOAD_DUPLICATE_LEDGER
let enableHelpText = ledgerConfig.enableHelpText ?? OPTION_ENABLE_DOWNLOAD_DUPLICATE_LEDGER_HELP
this._configurationManager.addFlagField(enableConfigKey, enableHelpText)
if (ledgerConfig.enableDefault !== false) {
this._configurationManager.getField(enableConfigKey).value = true
}
this._downloadDuplicateLedgerConfig = {
enableConfigKey,
storageKey: ledgerConfig.storageKey ?? this._config.scriptPrefix + 'download-ledger',
maxEntries: ledgerConfig.maxEntries ?? DEFAULT_DOWNLOAD_DUPLICATE_LEDGER_MAX_ENTRIES,
primaryField: ledgerConfig.primaryField ?? 'ids',
legacyIdFields: ledgerConfig.legacyIdFields ?? [],
isValidId: ledgerConfig.isValidId ?? ((value) => typeof value === 'string' && value.trim().length > 0),
getDownloadId: ledgerConfig.getDownloadId,
}
this._downloadedIds = new Set()
this._enqueuedDownloadIdsThisPage = new Set()
this._loadDownloadDuplicateLedger()
window.addEventListener('pagehide', () => this._flushDownloadDuplicateLedgerOnPageHide(), {capture: true})
}
/**
* @return {boolean}
* @protected
*/
_isDownloadDuplicateLedgerActive()
{
if (!this._downloadDuplicateLedgerConfig) {
return false
}
return !!this._getConfig(this._downloadDuplicateLedgerConfig.enableConfigKey)
}
/**
* @param {*} item
* @return {string|null}
* @protected
*/
_getDownloadDuplicateLedgerId(item)
{
if (!this._isDownloadDuplicateLedgerActive()) {
return null
}
let id = Utilities.callEventHandler(this._downloadDuplicateLedgerConfig.getDownloadId, [item], null)
if (id === null || id === undefined || !String(id).length) {
return null
}
return String(id)
}
/**
* @param {*} value
* @return {boolean}
* @private
*/
_isValidDownloadDuplicateLedgerId(value)
{
return this._downloadDuplicateLedgerConfig?.isValidId(value) === true
}
/**
* Per-id storage key — atomic `GM_setValue` avoids read-modify-write races on the index array.
*
* @param {string} id
* @return {string}
* @private
*/
_getDownloadDuplicateLedgerEntryKey(id)
{
return this._downloadDuplicateLedgerConfig.storageKey + '/e/' + encodeURIComponent(String(id).trim())
}
/**
* @param {string} id
* @return {boolean}
* @private
*/
_isDownloadDuplicateLedgerEntryStored(id)
{
if (!this._downloadDuplicateLedgerConfig) {
return false
}
try {
return GM_getValue(this._getDownloadDuplicateLedgerEntryKey(id), null) !== null
} catch (error) {
console.log('Failed to read download duplicate ledger entry:', error)
return false
}
}
/**
* @param {string} id
* @private
*/
_setDownloadDuplicateLedgerEntry(id)
{
GM_setValue(this._getDownloadDuplicateLedgerEntryKey(id), Date.now())
}
/**
* @param {string} id
* @private
*/
_deleteDownloadDuplicateLedgerEntry(id)
{
if (typeof GM_deleteValue === 'function') {
GM_deleteValue(this._getDownloadDuplicateLedgerEntryKey(id))
}
}
/**
* One-time backfill of per-id keys for registries written before atomic entries existed.
*
* @private
*/
_backfillDownloadDuplicateLedgerEntryKeys()
{
if (!this._downloadDuplicateLedgerConfig || this._downloadDuplicateLedgerEntryKeysBackfilled) {
return
}
this._downloadDuplicateLedgerEntryKeysBackfilled = true
for (let id of this._downloadedIds) {
if (!this._isDownloadDuplicateLedgerEntryStored(id)) {
try {
this._setDownloadDuplicateLedgerEntry(id)
} catch (error) {
console.log('Failed to backfill download duplicate ledger entry:', id, error)
}
}
}
}
/**
* Appends one id to the rolling index array (FIFO cap). Duplicate skip uses the per-id key above.
*
* @param {string} id
* @private
*/
_appendDownloadDuplicateLedgerIndexId(id)
{
if (!this._downloadDuplicateLedgerConfig || !this._isValidDownloadDuplicateLedgerId(id)) {
return
}
id = String(id).trim()
this._downloadedIds.add(id)
try {
let storageKey = this._downloadDuplicateLedgerConfig.storageKey
let primaryField = this._downloadDuplicateLedgerConfig.primaryField
let maxEntries = this._downloadDuplicateLedgerConfig.maxEntries ?? DEFAULT_DOWNLOAD_DUPLICATE_LEDGER_MAX_ENTRIES
let registry = GM_getValue(storageKey, null)
if (!registry || typeof registry !== 'object') {
registry = {}
}
let ids = Array.isArray(registry[primaryField])
? registry[primaryField].map((value) => String(value).trim()).filter(Boolean)
: []
if (ids.includes(id)) {
return
}
ids.push(id)
if (ids.length > maxEntries) {
for (let droppedId of ids.slice(0, ids.length - maxEntries)) {
this._deleteDownloadDuplicateLedgerEntry(droppedId)
}
ids = ids.slice(-maxEntries)
}
registry[primaryField] = ids
GM_setValue(storageKey, registry)
} catch (error) {
console.log('Failed to append download duplicate ledger index:', error)
}
}
/**
* @param {Object} registry
* @return {string[]}
* @private
*/
_readDownloadDuplicateLedgerIds(registry)
{
let ids = new Set()
let primaryField = this._downloadDuplicateLedgerConfig.primaryField
if (Array.isArray(registry[primaryField])) {
for (let id of registry[primaryField]) {
if (this._isValidDownloadDuplicateLedgerId(id)) {
ids.add(String(id).trim())
}
}
}
for (let field of this._downloadDuplicateLedgerConfig.legacyIdFields) {
if (!Array.isArray(registry[field])) {
continue
}
for (let id of registry[field]) {
if (this._isValidDownloadDuplicateLedgerId(id)) {
ids.add(String(id).trim())
}
}
}
return [...ids]
}
/**
* @param {Object} registry
* @private
*/
_applyDownloadDuplicateLedger(registry)
{
this._downloadedIds = new Set(this._readDownloadDuplicateLedgerIds(registry))
}
/**
* @private
*/
_loadDownloadDuplicateLedger()
{
this._reloadDownloadDuplicateLedgerFromStorage()
}
/**
* Replaces the in-memory ledger with the current storage snapshot.
*
* @protected
*/
_reloadDownloadDuplicateLedgerFromStorage()
{
if (!this._downloadDuplicateLedgerConfig) {
return
}
try {
let registry = GM_getValue(this._downloadDuplicateLedgerConfig.storageKey, null)
if (registry && typeof registry === 'object') {
this._applyDownloadDuplicateLedger(registry)
this._backfillDownloadDuplicateLedgerEntryKeys()
return
}
} catch (error) {
console.log('Failed to load download duplicate ledger:', error)
}
this._downloadedIds = new Set()
}
/**
* @protected
*/
_mergeDownloadDuplicateLedgerFromStorage()
{
if (!this._downloadDuplicateLedgerConfig) {
return
}
try {
let registry = GM_getValue(this._downloadDuplicateLedgerConfig.storageKey, null)
if (!registry || typeof registry !== 'object') {
return
}
for (let id of this._readDownloadDuplicateLedgerIds(registry)) {
this._downloadedIds.add(id)
}
} catch (error) {
console.log('Failed to merge download duplicate ledger:', error)
}
}
/**
* @private
*/
_flushDownloadDuplicateLedgerOnPageHide()
{
if (!this._downloadDuplicateLedgerConfig) {
return
}
for (let id of this._enqueuedDownloadIdsThisPage) {
this._appendDownloadDuplicateLedgerIndexId(id)
}
}
/**
* @param {string|null|undefined} downloadId
* @return {boolean}
* @protected
*/
_isDownloadDuplicate(downloadId)
{
if (!this._isDownloadDuplicateLedgerActive()) {
return false
}
if (downloadId === null || downloadId === undefined) {
return false
}
let id = String(downloadId).trim()
if (this._isDownloadDuplicateLedgerEntryStored(id)) {
return true
}
return this._downloadedIds.has(id)
}
/**
* Reserves a download before `GM_download` runs. Tampermonkey often does not fire
* `onload` in default download mode, so waiting for success would allow repeat attempts.
*
* @param {string|null|undefined|Array<string|null|undefined>} downloadId
* @return {boolean}
* @protected
*/
_claimDownloadDuplicateLedgerSlot(downloadId)
{
if (Array.isArray(downloadId)) {
return this._claimDownloadDuplicateLedgerSlots(downloadId)
}
return this._claimDownloadDuplicateLedgerSlots(downloadId === null || downloadId === undefined ? [] : [downloadId])
}
/**
* @param {Array<string|null|undefined>} downloadIds
* @return {boolean}
* @protected
*/
_claimDownloadDuplicateLedgerSlots(downloadIds)
{
if (!this._isDownloadDuplicateLedgerActive()) {
return true
}
let ids = [...new Set(downloadIds
.filter((id) => id !== null && id !== undefined && String(id).length)
.map((id) => String(id).trim()))]
if (!ids.length) {
return true
}
this._reloadDownloadDuplicateLedgerFromStorage()
for (let id of ids) {
if (!this._isValidDownloadDuplicateLedgerId(id)) {
continue
}
if (this._isDownloadDuplicate(id)) {
return false
}
}
for (let id of ids) {
if (!this._isValidDownloadDuplicateLedgerId(id)) {
continue
}
try {
this._setDownloadDuplicateLedgerEntry(id)
} catch (error) {
console.log('Failed to set download duplicate ledger entry:', id, error)
}
this._appendDownloadDuplicateLedgerIndexId(id)
this._enqueuedDownloadIdsThisPage.add(id)
}
return true
}
/**
* @param {{name?: string|null}} download
* @protected
*/
_handleDuplicateDownloadSkipped(download)
{
}
/**
* @param {{name?: string|null}} download
* @param {*} error
* @protected
*/
_handleDownloadFailed(download, error)
{
alert('Failed to download:\n\n' + (download.name ?? ''))
console.log('Download error:', error?.error, error?.details)
}
/**
* @param {{name: string|null, element: JQuery|null, url: string, downloadId?: string|null}} download
* @return {Promise<void>}
* @private
*/
_wrapDownloadTask(download)
{
let path = download.name
if (!path) {
alert('Download failed: missing file path.')
return Promise.resolve()
}
let conflictAction = this._isDownloadDuplicateLedgerActive() ? DOWNLOAD_DUPLICATE_LEDGER_CONFLICT_ACTION : 'uniquify'
return new Promise((resolve) => {
download.element?.remove()
GM_download({
url: download.url,
name: path,
conflictAction,
onload: () => resolve(),
onerror: (error) => {
this._handleDownloadFailed(download, error)
resolve()
},
})
})
}
/**
* Initialize the script and do basic UI removals
*/
init()
{
if (Utilities.callEventHandler(this._onValidateInit)) {
this._configurationManager.initialize()
this._itemAttributesResolver.addAttribute(ITEM_PROCESSED_ONCE, () => false)
if (this._config.itemNameSelector !== '') {
this._itemAttributesResolver.addAttribute(ITEM_NAME, (item) => item.find(this._config.itemNameSelector).text())
}
if (this._paginator) {
this._paginator.initialize()
}
Utilities.processEventHandlerQueue(this._onBeforeUIBuild)
if (!this._disableUI) {
this._embedUI(this._uiGen.createSettingsSection().append(this._userInterface))
Utilities.processEventHandlerQueue(this._onAfterUIBuild)
this._configurationManager.updateInterface()
}
this._validateCompliance(true)
Utilities.processEventHandlerQueue(this._onAfterInitialization)
}
}
/**
* @returns {boolean}
*/
isUserLoggedIn()
{
return this._config.isUserLoggedIn
}
registerHighlightStyleClass(styleClass)
{
this._highlightClasses += ' ' + styleClass
return this
}
}