Documentation
¶
Index ¶
- func CreateNewScript(dir string) (string, error)
- func DeleteScript(path string) error
- func EnsureScriptDir(dir string) error
- func GlobMatch(pattern, url string) bool
- func GlobMatchAny(patterns []string, url string) bool
- func LoadDir(dir string) (scripts []ScriptFile, errs []ScriptFile)
- func NewScriptTemplate() string
- func ToggleEnabled(path string) error
- type Engine
- func (e *Engine) Errors() []string
- func (e *Engine) LoadFromDir(dir string)
- func (e *Engine) LoadScript(name, source, urlPattern string) error
- func (e *Engine) Reload(dir string)
- func (e *Engine) RunOnRequest(ctx *RequestContext)
- func (e *Engine) RunOnResponse(ctx *ResponseContext, hostPath string)
- func (e *Engine) ScriptInfos() []ScriptInfo
- type Manager
- type RequestContext
- type ResponseContext
- type ScriptFile
- type ScriptInfo
- type ScriptMeta
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateNewScript ¶
CreateNewScript creates dir if needed and writes the default template to a new file with the pattern script-*.js.
func EnsureScriptDir ¶
EnsureScriptDir creates the directory (and parents) if it doesn't exist.
func GlobMatch ¶
GlobMatch checks if a URL matches a glob pattern. Pattern format: scheme://host/path where * is a wildcard. Matching is case-insensitive. In the host part, * matches a single domain label (via path.Match). In the path part, * matches any substring including slashes.
func GlobMatchAny ¶
GlobMatchAny returns true if the URL matches any of the patterns.
func LoadDir ¶
func LoadDir(dir string) (scripts []ScriptFile, errs []ScriptFile)
LoadDir reads all .js files from dir and parses their headers. Missing dir returns empty results (not error). Bad files go into errs with Error set and Meta.Name as filename fallback.
func NewScriptTemplate ¶
func NewScriptTemplate() string
NewScriptTemplate returns the default script template string.
func ToggleEnabled ¶
ToggleEnabled reads a script file, flips its enabled state, and rewrites the file. If no "// enabled:" line exists, one is inserted before the closing "// ---" delimiter.
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine manages loaded JS scripts and runs them via goja.
func (*Engine) LoadFromDir ¶
LoadFromDir loads all scripts from dir via LoadDir. Enabled scripts are added to the engine; disabled scripts are tracked for ScriptInfos.
func (*Engine) LoadScript ¶
LoadScript adds a script with an optional URL pattern filter.
func (*Engine) Reload ¶
Reload clears dir-loaded scripts and re-loads from dir. Manually loaded scripts (via LoadScript) are kept intact.
func (*Engine) RunOnRequest ¶
func (e *Engine) RunOnRequest(ctx *RequestContext)
RunOnRequest executes all scripts' onRequest against the context.
func (*Engine) RunOnResponse ¶
func (e *Engine) RunOnResponse(ctx *ResponseContext, hostPath string)
RunOnResponse executes all scripts' onResponse against the context.
func (*Engine) ScriptInfos ¶
func (e *Engine) ScriptInfos() []ScriptInfo
ScriptInfos returns info about all dir-loaded scripts (including errors).
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager wraps Engine and provides TUI-friendly script management.
func NewManager ¶
NewManager creates a Manager for the given engine and scripts directory.
func (*Manager) CreateNew ¶
CreateNew creates a new script from the default template and returns its path.
func (*Manager) Reload ¶
func (m *Manager) Reload()
Reload re-scans the scripts directory and reloads the engine.
func (*Manager) Scripts ¶
func (m *Manager) Scripts() []ScriptInfo
Scripts returns info about all scripts (enabled, disabled, and errored). Unlike Engine.ScriptInfos(), this scans the directory directly to include disabled and errored scripts that the engine doesn't load.
type RequestContext ¶
type RequestContext struct {
Method string
URL string
Headers http.Header
Body []byte
Blocked bool
}
RequestContext is passed to JS onRequest handlers.
type ResponseContext ¶
ResponseContext is passed to JS onResponse handlers.
type ScriptFile ¶
type ScriptFile struct {
Meta *ScriptMeta
Source string // JS body (after header)
FilePath string
Error string // non-empty if script failed to load
}
ScriptFile represents a loaded script file with metadata.
type ScriptInfo ¶
ScriptInfo describes a loaded script for TUI display.
type ScriptMeta ¶
type ScriptMeta struct {
Name string `yaml:"name"`
Match []string `yaml:"match"`
Enabled *bool `yaml:"enabled,omitempty"`
}
ScriptMeta holds parsed YAML frontmatter from a script file.
func ParseHeader ¶
func ParseHeader(source string) (*ScriptMeta, string, error)
ParseHeader extracts YAML frontmatter from a script source string. The frontmatter is delimited by "// ---" lines, with each YAML line prefixed by "// ". Returns the parsed metadata, the remaining script body, and any error.
func (*ScriptMeta) IsEnabled ¶
func (m *ScriptMeta) IsEnabled() bool
IsEnabled returns the enabled state, defaulting to true when unset.