Brazen Framework - Utilities

Utilities and helpers for Brazen user scripts framework

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/375557/1875053/Brazen%20Framework%20-%20Utilities.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Advertisement:

نووسەر
brazenvoid
وەشان
4.2.1
Created
2018-12-15
Updated
2026-07-13
Size
22.3 KB
مۆڵەت
GPL-3.0-only

Brazen Framework — Utilities (developer guide)

Foundation module: small, site-agnostic building blocks used by the Brazen Framework stack. As a script author, you’ll mostly touch these through BrazenFramework helpers and through other modules (Configuration Manager, View Layer), not by treating Utilities as a standalone layer.

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


When to use / when not to use

  • Use this module when you need one of the framework’s shared primitives: DOM mutation watching, storage wrappers, selector/id generation, statistics labels, validation helpers, or tiny async helpers like sleep.
  • Don’t use this module to build UI or settings schemas — that belongs to the View Layer and Configuration Manager.

Quick start

These are the two most common “surface-level” utilities you’ll see in app code that extends BrazenFramework:

// Watch a list for new nodes (useful outside the main compliance pipeline).
ChildObserver.create()
  .onNodesAdded((nodes) => {
    // nodes: Node[]
  })
  .observe(document.querySelector('#results'))

// Throttle or delay between operations.
await Utilities.sleep(250)

Features

DOM observation: ChildObserver

What it does: A fluent wrapper over MutationObserver for childList add/remove events on a single target node.

Typical use cases:

  • Watching a site widget that’s unrelated to item compliance (e.g. a sidebar that updates via AJAX).
  • Running a one-off enhancement when new rows/cards are appended.

How it works (developer-relevant):

  • Observes one node at a time (observe(node)).
  • Provides separate callbacks for added vs removed nodes.
  • You can pause/resume without rebuilding the observer.

APIs:

  • ChildObserver.create() → factory
  • observe(node) → attach to a Node
  • onNodesAdded(handler) / onNodesRemoved(handler)(nodes, previousSibling, nextSibling, target) => void
  • pauseObservation() / resumeObservation()

Notes: The framework itself uses ChildObserver on each itemListSelectors entry so infinite scroll and AJAX lists get compliance automatically. Use it directly only when you’re doing DOM work outside that pipeline.


Persistence helpers: LocalStore (localStorage)

What it does: A localStorage wrapper for JSON blobs with default values, change callbacks, and a nested-object serialization mode used by some parts of the stack.

Typical use cases:

  • App-specific caching where you want “defaults on first run” behavior.
  • Lightweight state where you don’t want to define a full Configuration Manager field.

Quick recipe:

const store = new LocalStore('myapp-cache', { seen: [] })
const data = store.get()
data.seen.push('abc')
store.save(data)

APIs:

  • new LocalStore(key, defaultsObject)
  • get() → object; if missing/empty, writes defaults via restoreDefaults()
  • save(data) / delete()
  • restoreDefaults() / wereDefaultsSet()
  • onChange(handler)(storeObject) => void

Nested-object mode: If stored JSON includes top-level arrays, objects, and properties keys, get() uses Utilities.objectFromJSON to round-trip nested arrays/objects in a stable way. Plain JSON objects are returned as-is.


Pluggable storage drivers: StorageDriver, LocalStorageDriver, GmStorageDriver, DriverStore

What it does: A small driver interface used by the Configuration Manager to persist settings to either localStorage (local) or Tampermonkey GM value storage (gm).

Typical use cases:

  • You usually don’t instantiate these directly; instead you attach fields to storage via Configuration Manager (field.attachStorage('gm')).

Concepts:

  • Driver = read/write primitives for a backend.
  • Store = aggregate JSON blob over a driver (similar to LocalStore, but backend-agnostic).

Constants:

  • STORAGE_DRIVER_LOCAL = 'local'
  • STORAGE_DRIVER_GM = 'gm'

APIs (mostly framework-internal):

  • StorageDriver (abstract): read, write, remove, readStore, writeStore
  • LocalStorageDriver / GmStorageDriver
  • DriverStore (aggregate settings wrapper)

Stable ids/selectors: SelectorGenerator

What it does: Generates prefixed element ids/selectors from human-readable names. The rest of the stack uses this to keep UI ids stable and script-scoped.

Typical use cases:

  • When you need to create your own DOM nodes but still want to align with the stack’s id conventions.

APIs:

  • new SelectorGenerator(selectorPrefix) (typically your scriptPrefix)
  • getSelector(selector)prefix + selector
  • getSettingsInputSelector(settingName){prefix}{kebab-name}-setting
  • getSettingsRangeInputSelector(name, getMin){prefix}{kebab-name}-min-setting / -max-setting
  • getStatLabelSelector(statisticType) → element id used by stat labels

Filter removal counts: StatisticsRecorder

What it does: Tracks how many items were removed (failed validation) per filter and updates matching DOM stat labels.

Typical use cases:

  • You usually get this via the framework, but direct use is straightforward when building custom hide/show logic.

Key behavior: record(type, validationResult) increments counters only when validationResult is falsy (i.e. the item failed a filter).

APIs:

  • new StatisticsRecorder(selectorPrefix)
  • record(statisticType, validationResult, value = 1)
  • getTotal(), reset(), updateUI()

Per-rule hide diagnostics: ComplianceRuleRecorder

What it does: When enabled (via framework trackComplianceRules: true), records “which rule caused hides” so the UI can display a breakdown.

APIs:

  • record(configKey, ruleLabel, count = 1)
  • reset()
  • getReport(resolveFilterLabel?)[{ filterKey, filterLabel, rules: [{ label, count }] }]

Small general helpers: Utilities (static)

What it does: A grab-bag of small utilities the framework stack depends on.

Commonly used APIs:

  • sleep(ms) → Promise-based delay
  • toKebabCase(text) → used for tab ids and config keys
  • trimAndKeepNonEmptyStrings(strings[])
  • buildWholeWordMatchingRegex(words[]) → used by blacklist filters
  • callEventHandler, callEventHandlerOrFail, processEventHandlerQueue
  • generateId(prefix?)
  • objectToJSON(object) / objectFromJSON(json)

Example:

await Utilities.sleep(500)
const pattern = Utilities.buildWholeWordMatchingRegex(['foo', 'bar'])

Validation & sanitization helpers: Validator (static)

What it does: Common validation and sanitization helpers used by range filters, tag/text rules, and text normalization.

Commonly used APIs:

  • isInRange(value, lower, upper) → inclusive bounds; supports “only min” or “only max”
  • sanitize(text, rules) → apply regex replacements then trim()
  • regexMatches(text, rules) / validateTextDoesNotContain(text, rules)
  • DOM helpers: doesChildExist, isChildMissing
  • Title/text helpers: sanitizeTextNode, sanitizeNodeOfSelector
  • iFramesRemover() → injects iframe { display: none !important; } (requires style injection grant)

Public API reference (index)

  • Constants: REGEX_LINE_BREAK, REGEX_PRESERVE_NUMBERS, STORAGE_DRIVER_LOCAL, STORAGE_DRIVER_GM
  • Classes: ChildObserver, LocalStore, StorageDriver, LocalStorageDriver, GmStorageDriver, DriverStore, SelectorGenerator, StatisticsRecorder, ComplianceRuleRecorder
  • Statics: Utilities.*, Validator.*

Integration notes (grants, load order, pitfalls)

  • Grants: this module declares no Tampermonkey grants. Put GM_addStyle, GM_download, GM_getValue, GM_setValue, etc. on the application script as needed.
  • Load order: load after jQuery, before all other Brazen modules.
  • Next in stack: View Layer