Brazen Framework - Utilities

Utilities and helpers for Brazen user scripts framework

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greatest.deepsurf.us/scripts/375557/1847304/Brazen%20Framework%20-%20Utilities.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

作者
brazenvoid
版本
4.1.0
建立日期
2018-12-15
更新日期
2026-06-09
尺寸
13.1 KB
授權條款
GPL-3.0-only

Brazen Framework — Utilities (developer guide)

Foundation module: shared helpers with no site logic. Every other Brazen Framework module depends on this script.

Greasy Fork: Utilities · Loads first among framework modules (after jQuery).


Module constants

Constant Value Use
REGEX_LINE_BREAK /\r?\n/g Split textarea rules (Configuration Manager rulesets)
REGEX_PRESERVE_NUMBERS /\D/g Strip non-digits from strings

ChildObserver

Fluent MutationObserver wrapper for childList add/remove on a single node.

Method Returns Description
ChildObserver.create() ChildObserver Factory
observe(node) this Attach to a DOM Node
onNodesAdded(handler) this (nodes, previousSibling, nextSibling, target) => void
onNodesRemoved(handler) this Same callback signature
pauseObservation() disconnect()
resumeObservation() Re-attach with same options

The framework uses ChildObserver on each itemListSelectors entry so infinite scroll and AJAX lists receive compliance without manual hooks. Use directly only for non-compliance DOM watching.

ChildObserver.create()
  .onNodesAdded((nodes) => { /* ... */ })
  .observe(document.querySelector('#results'))

LocalStore

localStorage wrapper with defaults, change callbacks, and optional nested-object serialization.

Constructor: new LocalStore(key, defaultsObject)

Method Returns Description
get() Object Parse stored JSON; if empty, calls restoreDefaults()
save(data) this JSON.stringify + setItem; fires onChange
delete() this removeItem
restoreDefaults() Object Writes defaults and sets wereDefaultsSet() flag
onChange(handler) this (storeObject) => void after every save
wereDefaultsSet() boolean true when last get() fell back to defaults

Nested objects: If stored JSON has top-level arrays, objects, and properties keys, get() uses Utilities.objectFromJSON for round-trip of nested arrays/objects. Plain JSON objects are returned as parsed.

Configuration Manager wraps two LocalStore instances: {scriptPrefix}settings and {scriptPrefix}settings-id.


SelectorGenerator

Builds prefixed kebab-case selectors for generated UI fragments.

Constructor: new SelectorGenerator(selectorPrefix) — typically scriptPrefix from the app.

Method Returns Example
getSelector(selector) string prefix + selector
getSettingsInputSelector(settingName) string {prefix}{kebab-name}-setting
getSettingsRangeInputSelector(name, getMin) string {prefix}{kebab-name}-min-setting or -max-setting
getStatLabelSelector(statisticType) string Used as element id for stat labels

StatisticsRecorder and View Layer stat widgets use getStatLabelSelector.


StatisticsRecorder

Tracks per-filter removal counts and updates DOM labels.

Constructor: new StatisticsRecorder(selectorPrefix)

Method Description
record(statisticType, validationResult, value = 1) Increments statisticType and Total only when validationResult is falsy (item failed)
getTotal() Sum of all non-compliant hits
reset() Zero all counters
updateUI() Writes counts to elements whose id matches getStatLabelSelector(type)

Pair statisticType with config keys used in createStatisticsFormGroup(key) so bottom-panel labels reflect hide counts.


ComplianceRuleRecorder

Optional per-rule diagnostics when trackComplianceRules: true on the framework.

Method Description
record(configKey, ruleLabel, count = 1) Accumulate hides per filter + rule label
reset() Clear all recorded rules
getReport(resolveFilterLabel?) Sorted report: [{ filterKey, filterLabel, rules: [{ label, count }] }]

resolveFilterLabel maps internal keys to human-readable filter titles (framework passes Configuration Manager field titles).


Utilities (static)

Method Signature Notes
sleep (ms) => Promise Throttles deep attribute loads, paginator fetches, download queue
callEventHandler (handler, params?, defaultValue?) Invokes handler or returns default
callEventHandlerOrFail (name, handler, params?) Throws if handler missing
processEventHandlerQueue (handlers[], params?, defaultValue?) Runs all handlers in order
generateId `(prefix?) => number\ string`
toKebabCase (text) => string Lowercase, spaces → hyphens (tab ids, field keys)
trimAndKeepNonEmptyStrings (strings[]) => string[] Trim + drop empty
buildWholeWordMatchingRegex `(words[]) => RegExp\ null`
objectToJSON (object) => string Nested structure with arrays / objects / properties
objectFromJSON (json) => Object Inverse of objectToJSON
await Utilities.sleep(500)
const pattern = Utilities.buildWholeWordMatchingRegex(['foo', 'bar'])

Validator (static)

Method Signature Behaviour
isInRange (value, lower, upper) => boolean If both bounds > 0: inclusive range. If only lower > 0: value >= lower. If only upper > 0: value <= upper.
doesChildExist (item: JQuery, selector) => boolean item.find(selector).length > 0
isChildMissing (item, selector) => boolean Inverse of above
sanitize (text, rules) => string rules map: regex pattern → replacement string; then trim()
sanitizeTextNode (textNode: JQuery, rules) => Validator Mutates node text
sanitizeNodeOfSelector (selector, rules) => Validator Sanitizes node + document.title
regexMatches (text, rules) => boolean true if no rules or regex matches
validateTextDoesNotContain (text, rules) => boolean true if no rules or regex does not match
iFramesRemover () Injects iframe { display: none !important; } via GM_addStyle

Framework range filters and whitelist/blacklist paths use Validator internally. Custom comply callbacks often call isInRange and regexMatches.


Grants and load order

This module declares no Tampermonkey grants. Grant GM_addStyle, GM_download, GM_getValue, GM_setValue, etc. on the application script as needed.

@run-at document-end

Next in stack: View Layer