Skip to content

API Reference

crx-manifest-parser exposes two types — Manifest and ContentScript — plus the Json value model and ParseError it is built on. The crate is #![forbid(unsafe_code)] and pulls in no external dependencies.

Manifest::from_json

impl Manifest {
    pub fn from_json(json: &str) -> Result<Manifest, ParseError>
}

Parse a Chrome / MV3 manifest.json string into a typed Manifest. Unknown fields are ignored (Chrome adds many); known fields are decoded into Option / Vec members. Returns a ParseError carrying the byte offset and reason on malformed JSON.

use crx_manifest_parser::Manifest;

let m = Manifest::from_json(r#"{ "manifest_version": 3, "name": "X" }"#)?;
assert_eq!(m.name.as_deref(), Some("X"));
# Ok::<(), crx_manifest_parser::ParseError>(())

Manifest

pub struct Manifest {
    pub manifest_version: Option<i64>,
    pub name: Option<String>,
    pub version: Option<String>,
    pub description: Option<String>,
    pub permissions: Vec<String>,
    pub optional_permissions: Vec<String>,
    pub host_permissions: Vec<String>,
    pub content_scripts: Vec<ContentScript>,
}
field source key meaning
manifest_version manifest_version the MV integer (3 for MV3)
name name display name
version version version string (e.g. "1.4.2")
description description short description
permissions permissions named API tokens granted on install
optional_permissions optional_permissions tokens requested later at runtime
host_permissions host_permissions match-patterns granting site access
content_scripts content_scripts scripts + match list injected into pages

ContentScript

pub struct ContentScript {
    pub matches: Vec<String>,
    pub js: Vec<String>,
}

The script + match list for one content_scripts entry — the biggest cross-origin exposure surface, since it runs code inside matched pages.

field meaning
matches match-patterns deciding which pages the script runs
js the script files injected into those pages

Json

The minimal JSON value model the parser uses internally. Re-exported so downstream callers can inspect a raw field without re-parsing.

pub enum Json {
    Null,
    Bool(bool),
    Number(String),  // stored as string to preserve fidelity losslessly
    String(String),
    Array(Vec<Json>),
    Object(Vec<(String, Json)>),  // insertion-ordered
}

Convenience accessors on Json:

impl Json {
    pub fn as_str(&self) -> Option<&str>;
    pub fn as_i64(&self) -> Option<i64>;
    pub fn as_bool(&self) -> Option<bool>;
    pub fn as_array(&self) -> Option<&[Json]>;
    pub fn as_object(&self) -> Option<&[(String, Json)]>;
    pub fn get(&self, key: &str) -> Option<&Json>;
}

ParseError

pub struct ParseError {
    pub offset: usize,     // 0-based byte offset where parsing failed
    pub message: String,   // short human-readable reason
}

A parse error: the byte offset into the source plus a short reason. The offset makes errors easy to point at in a manifest. Implements Display; implements std::error::Error when the std feature is on.

use crx_manifest_parser::{Manifest, ParseError};

let err = Manifest::from_json("{ not json }").unwrap_err();
println!("{} (offset {})", err.message, err.offset);