scripting

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMapLocalScript added in v0.1.6

func CreateMapLocalScript(dir, pattern, localPath string) (string, error)

CreateMapLocalScript creates a map-local script in dir with the given URL pattern and local file path. Returns the path to the created file.

func CreateNewScript

func CreateNewScript(dir string) (string, error)

CreateNewScript creates dir if needed and writes the default template to a new file with the pattern script-*.js.

func DeleteScript

func DeleteScript(path string) error

DeleteScript removes the file at path.

func EnsureScriptDir

func EnsureScriptDir(dir string) error

EnsureScriptDir creates the directory (and parents) if it doesn't exist.

func GlobMatch

func GlobMatch(pattern, url string) bool

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

func GlobMatchAny(patterns []string, url string) bool

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

func ToggleEnabled(path string) error

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 New

func New() *Engine

New creates a scripting engine.

func (*Engine) Errors

func (e *Engine) Errors() []string

Errors returns recorded runtime errors.

func (*Engine) LoadFromDir

func (e *Engine) LoadFromDir(dir string)

LoadFromDir loads all scripts from dir via LoadDir. Enabled scripts are added to the engine; disabled scripts are tracked for ScriptInfos.

func (*Engine) LoadScript

func (e *Engine) LoadScript(name, source, urlPattern string) error

LoadScript adds a script with an optional URL pattern filter.

func (*Engine) Reload

func (e *Engine) Reload(dir string)

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).

func (*Engine) SetBreakpointController added in v0.1.6

func (e *Engine) SetBreakpointController(ctrl breakpoint.Controller)

SetBreakpointController injects the breakpoint controller.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager wraps Engine and provides TUI-friendly script management.

func NewManager

func NewManager(engine *Engine, dir string) *Manager

NewManager creates a Manager for the given engine and scripts directory.

func (*Manager) CreateNew

func (m *Manager) CreateNew() (string, error)

CreateNew creates a new script from the default template and returns its path.

func (*Manager) Delete

func (m *Manager) Delete(filePath string) error

Delete removes a script file and reloads the engine.

func (*Manager) QuickAddMapLocal added in v0.1.6

func (m *Manager) QuickAddMapLocal(
	pattern, localPath string,
) (string, error)

QuickAddMapLocal creates a map-local script with respondWith({file}).

func (*Manager) Reload

func (m *Manager) Reload()

Reload re-scans the scripts directory and reloads the engine.

func (*Manager) ScriptDir

func (m *Manager) ScriptDir() string

ScriptDir returns the scripts directory path.

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.

func (*Manager) Toggle

func (m *Manager) Toggle(filePath string) error

Toggle flips the enabled state of a script file and reloads the engine.

type RequestContext

type RequestContext struct {
	Method  string
	URL     string
	Headers http.Header
	Body    []byte
	Blocked bool
	Meta    store.FlowMeta

	Responded       bool
	ResponseStatus  int
	ResponseHeaders map[string]string
	ResponseBody    []byte
}

RequestContext is passed to JS onRequest handlers.

type ResponseContext

type ResponseContext struct {
	Status  int
	Headers http.Header
	Body    []byte
	Meta    store.FlowMeta

	Responded       bool
	ResponseStatus  int
	ResponseHeaders map[string]string
	ResponseBody    []byte
}

ResponseContext is passed to JS onResponse handlers.

type ScriptCategory added in v0.1.6

type ScriptCategory string

ScriptCategory classifies scripts by detected API usage.

const (
	CategoryScript     ScriptCategory = "Script"
	CategoryMapLocal   ScriptCategory = "Map Local"
	CategoryBreakpoint ScriptCategory = "Breakpoint"
)

func DetectCategories added in v0.1.6

func DetectCategories(source string) []ScriptCategory

DetectCategories returns categories based on static source analysis.

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

type ScriptInfo struct {
	Name       string
	Matches    []string
	Enabled    bool
	FilePath   string
	Error      string
	Categories []ScriptCategory
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL