Skip to content

Rule JSON Import/Export ​

The last section of the settings page displays the current custom rules as JSON. It's useful for backups, migrations, and bulk edits.

Use Cases ​

  • Migrate custom rules across devices or browser profiles
  • Back up rules with version control
  • Bulk editing (search-and-replace in the text field is faster than editing row by row in the form)
  • Programmatically generate a large rule set (write a script to produce the array)

Field Structure ​

Each rule is an object:

json
{
  "name": "MyCMS",
  "category": "CMS / Website Programs",
  "kind": "Custom CMS",
  "confidence": "medium",
  "matchType": "regex",
  "patterns": ["mycms-[0-9.]+\\.js", "X-Generator: MyCMS"],
  "selectors": ["[data-powered-by='mycms']"],
  "globals": ["MyCMS"],
  "matchIn": ["url", "resources", "html", "headers", "dynamic"],
  "url": "https://mycms.example.com"
}
FieldTypeRequiredDescription
namestringYesName shown in the popup
categorystringCategory; defaults to Other Libraries
kindstringKind label; defaults to Custom Rule
confidence"high" / "medium" / "low"Defaults to medium
matchType"keyword" / "regex"Defaults to regex
patternsstring[]One of threeAt least one of patterns / selectors / globals must be non-empty
selectorsstring[]One of threeCSS selector list
globalsstring[]One of threeGlobal variable name list (window.x.y written as x.y)
matchInstring[]Any combination of url / resources / html / headers / dynamic; defaults to all
urlstringMust be http(s)://; may be empty

Buttons ​

Two buttons appear in the section header:

  • Import from JSON: parses the textarea content and, if validation passes, replaces the entire custom rule list (not a merge)
  • Format: re-serializes the parsed rules with JSON.stringify(rules, null, 2) for clean indentation

Backslashes in Strings ​

Regexes in JSON require double-escaping. To match \.js:

json
{
  "matchType": "regex",
  "patterns": ["\\.js$"]
}

The JSON parser reads \\ as a single \, giving the extension \.js$, which new RegExp() then interprets as an escaped dot.

Atomic Validation ​

The entire array is parsed before any rule is committed. If any rule has an invalid field, the whole import is rejected — there is no partial import state. The error message identifies which rule number and which field failed.

Relationship to the Form ​

  • Form edits automatically sync to the JSON textarea
  • JSON textarea edits require clicking "Import from JSON" to sync back to the internal rule list
  • Clicking "Save Settings" without importing first saves the state from the last form edit or import operation

Limits ​

CUSTOM_RULE_LIMITS.rules defaults to 100. Exceeding it causes the import to be rejected. Full limits in src/types/settings.ts:

FieldLimit
Total rules100
name80 chars
category40 chars
kind40 chars
Single pattern / selector / global240 chars
patterns / selectors / globals array length80 each
matchIn array length8
url240 chars
customCss10,000 chars

Released under MIT