# zotero/zotero-connectors architecture

> Architecture diagram of the GitHub repository zotero/zotero-connectors, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zotero/zotero-connectors
- Repository: https://github.com/zotero/zotero-connectors

## Overview

1. **Repository purpose**
   This repo builds the Zotero browser connector: a cross-browser extension that detects scholarly content on web pages, translates it into Zotero items, and sends those items either to the local Zotero desktop app or to the Zotero web API when the desktop app is unavailable.

2. **Top-level architectural split**
   The codebase is organized around three main boundaries:
   - **Page-injected scripts** that run inside individual webpages
   - **Background/runtime process** that owns extension state, networking, caching, and privileged browser APIs
   - **Shared “common” logic** reused by browser-specific builds and by injected/background contexts

3. **Primary runtime data flow**
   The usual save flow is:
   webpage detection or user action → injected translation code on the page → background process for translator lookup/caching and privileged operations → Zotero desktop connector server on port 23119 or fallback to zotero.org API.  
   The browser extension never waits for Zotero to call back; all communication is connector-initiated.

4. **Injected page layer**
   `src/common/inject` and browser-specific injection entrypoints under `src/browserExt/inject` and `src/safari/inject_safari.js` run in page context. This layer hosts the Zotero Translate web stack, page scraping, item detection, sandboxed page parsing, and any UI or prompts that must live in the page context.

5. **Background layer**
   Browser-specific background entrypoints are in `src/browserExt/background.js`, `background-worker.js`, and Safari’s `src/safari/global.html` / `global.js`. This layer is the middle tier between page scripts and external services. It manages extension state, translator caches, preferences, saving, messaging, offscreen handling, and browser-specific APIs such as webRequest interception and MV3 keep-alive logic.

6. **Translator and save subsystem**
   The shared translation engine lives in `src/common/translators.js`, `translate.js`, `translateWeb.js`, `itemSaver.js`, and related modules. It performs:
   - coarse URL-based translator matching in background
   - page-level `detectWeb()` refinement in injected scripts
   - item creation and save orchestration
   - retrieval of translator code from Zotero repo / zotero.org via `src/common/repo.js`
   The translator system is the core application logic, and most of the repo exists to support it.

7. **Messaging boundary**
   Communication between background and injected contexts happens only through browser messaging. The shared messaging layer in `src/common/messaging.js`, `src/common/messages.js`, `src/common/connector.js`, and browser-specific adapters like `src/browserExt/messaging_inject.js` and `src/safari/messaging_inject.js` exposes “monkey-patched” async methods across the boundary. This is the main architectural seam in the project.

8. **Browser-specific implementation layer**
   `src/browserExt` contains the WebExtension implementation for Chrome/Edge/Firefox, including manifest files, offscreen support, content scripts, webRequest interception, permissions/policy handling, and UI pages such as confirm, item selector, and attachment monitor. `src/safari` mirrors the same conceptual roles using Safari app-extension mechanisms and browser-specific shims.

9. **External integration targets**
   There are two external systems:
   - **Zotero desktop connector server**: local HTTP server on `localhost:23119`, accessed through `Zotero.Connector.callMethod(...)`
   - **zotero.org Web API**: used when saving without the desktop app  
   Supporting code lives in `src/common/connector.js` and `src/common/api.js`.

10. **User-facing UI surfaces**
   The extension includes several small UI subsystems rather than a single app shell: preferences, progress window, modal prompt, item selector, confirm dialogs, and browser attachment monitoring. These are mostly in `src/common/*` with browser-specific wrappers or HTML entrypoints in `src/browserExt/*` and `src/safari/*`.

11. **Shared utilities and domain support**
   `src/common` also contains infrastructure that underpins saving and detection: HTTP helpers, proxy logic, i18n, cached types, schema definitions, Zotero-specific config, updater fixes, and support for Google Docs integration and translator testing. These are shared because they must behave consistently across browsers and contexts.

12. **Build and packaging pipeline**
   The repo is built with Node/Gulp (`gulpfile.js`, `build.sh`, `scripts/replace_browser.js`) into a `build/` output directory, with browser-specific packaging helpers for Chrome, Edge, Firefox, and Safari. The build system assembles the shared code into browser-targeted extension bundles and handles manifest/browser replacement.

13. **Testing and CI**
   Automated tests are in `test/tests` and cover background behavior, messaging, translation, HTTP, preferences, proxying, webRequest interception, and Google Docs integration. GitHub Actions workflows run CI and code spell checks, indicating this repo is maintained as a multi-browser extension with substantial cross-context integration testing.

14. **Important architectural takeaway**
   This is not a monolithic extension UI; it is a distributed system inside the browser. The key diagram should show page-context scripts, a privileged background/runtime layer, shared translator and messaging libraries, and two external save targets (local Zotero server and zotero.org API), with browser-specific wrappers around the same core architecture.

Main entry points: [`src/common/translate.js`](https://github.com/zotero/zotero-connectors/blob/master/src/common/translate.js) (Translate core), [`src/common/messaging.js`](https://github.com/zotero/zotero-connectors/blob/master/src/common/messaging.js) (Messaging core), [`src/browserExt/background.js`](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/background.js) (BG entry), [`src/common/inject/inject.jsx`](https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/inject.jsx) (Page inject)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_page["Page context"]
  node_inject_entry["Inject entry<br/>page entrypoints"]
  node_page_inject["Page inject<br/>page runtime<br/>[inject.jsx]"]
  node_page_sandbox["Sandbox mgr<br/>page support<br/>[sandboxManager.js]"]
  node_page_ui["Page prompts<br/>page UI"]
end

subgraph group_bg["Background/runtime"]
  node_bg_entry["BG entry<br/>background entry<br/>[background.js]"]
  node_bg_worker["BG worker<br/>background worker"]
  node_bg_state["State & prefs<br/>runtime state<br/>[prefs.js]"]
  node_bg_webreq["WebRequest<br/>browser hook"]
  node_bg_offscreen["Offscreen<br/>browser hook<br/>[offscreen.js]"]
end

subgraph group_core["Shared core"]
  node_msg_core["Messaging core<br/>shared boundary<br/>[messaging.js]"]
  node_translate_core["Translate core<br/>translator engine<br/>[translate.js]"]
  node_translators["Translators<br/>translator registry<br/>[translators.js]"]
  node_item_saver["Item saver<br/>save orchestration<br/>[itemSaver.js]"]
  node_repo["Repo fetch<br/>translator source<br/>[repo.js]"]
end

subgraph group_browser["Browser-specific"]
  node_browser_shim["Browser shims<br/>browser-specific layer"]
  node_safari_entry["Safari entry<br/>safari runtime<br/>[global.js]"]
end

subgraph group_external["External systems"]
  node_connector[("Connector API<br/>local connector<br/>[connector.js]")]
  node_web_api[("Web API<br/>cloud api<br/>[api.js]")]
end

subgraph group_build["Build & test"]
  node_build_ci["Build & CI<br/>build pipeline<br/>[gulpfile.js]"]
  node_tests["Tests<br/>test suite<br/>[backgroundTest.mjs]"]
end

node_inject_entry -->|"loads"| node_page_inject
node_page_inject -->|"uses"| node_page_sandbox
node_page_inject -->|"shows"| node_page_ui
node_page_inject -->|"calls"| node_msg_core
node_bg_entry -->|"handles"| node_msg_core
node_bg_worker -->|"handles"| node_msg_core
node_msg_core -->|"adapts"| node_browser_shim
node_msg_core -->|"adapts"| node_safari_entry
node_bg_entry -->|"manages"| node_bg_state
node_bg_entry -->|"intercepts"| node_bg_webreq
node_bg_entry -->|"uses"| node_bg_offscreen
node_translate_core -->|"matches"| node_translators
node_translate_core -->|"saves"| node_item_saver
node_translate_core -->|"coordinates"| node_msg_core
node_translate_core -->|"fetches"| node_repo
node_item_saver -->|"prefers"| node_connector
node_item_saver -->|"falls back"| node_web_api
node_bg_entry -->|"orchestrates"| node_translate_core
node_safari_entry -->|"hosts"| node_translate_core
node_build_ci -->|"packages"| node_bg_entry
node_build_ci -->|"packages"| node_safari_entry
node_tests -->|"verifies"| node_translate_core
node_tests -->|"verifies"| node_msg_core

click node_inject_entry "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/inject/virtualOffscreenTranslate.js"
click node_page_inject "https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/inject.jsx"
click node_page_sandbox "https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/sandboxManager.js"
click node_page_ui "https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/modalPrompt_inject.js"
click node_bg_entry "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/background.js"
click node_bg_worker "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/background-worker.js"
click node_bg_state "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/prefs.js"
click node_bg_webreq "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/webRequestIntercept.js"
click node_bg_offscreen "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/offscreen/offscreen.js"
click node_msg_core "https://github.com/zotero/zotero-connectors/blob/master/src/common/messaging.js"
click node_translate_core "https://github.com/zotero/zotero-connectors/blob/master/src/common/translate.js"
click node_translators "https://github.com/zotero/zotero-connectors/blob/master/src/common/translators.js"
click node_item_saver "https://github.com/zotero/zotero-connectors/blob/master/src/common/itemSaver.js"
click node_repo "https://github.com/zotero/zotero-connectors/blob/master/src/common/repo.js"
click node_connector "https://github.com/zotero/zotero-connectors/blob/master/src/common/connector.js"
click node_web_api "https://github.com/zotero/zotero-connectors/blob/master/src/common/api.js"
click node_browser_shim "https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/messaging_inject.js"
click node_safari_entry "https://github.com/zotero/zotero-connectors/blob/master/src/safari/global.js"
click node_build_ci "https://github.com/zotero/zotero-connectors/blob/master/gulpfile.js"
click node_tests "https://github.com/zotero/zotero-connectors/blob/master/test/tests/backgroundTest.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_inject_entry,node_page_inject,node_page_sandbox,node_page_ui toneBlue
class node_bg_entry,node_bg_worker,node_bg_state,node_bg_webreq,node_bg_offscreen toneAmber
class node_msg_core,node_translate_core,node_translators,node_item_saver,node_repo toneMint
class node_browser_shim,node_safari_entry toneRose
class node_connector,node_web_api toneIndigo
class node_build_ci,node_tests toneTeal
```

## Components

### Page context

Injected scripts and page-local translation

- [Inject entry](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/inject/virtualOffscreenTranslate.js): page entrypoints · Browser and Safari entrypoints that load page-context code · `src/browserExt/inject/virtualOffscreenTranslate.js`
- [Page inject](https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/inject.jsx): page runtime · Shared injected logic for scraping, detection, and sandboxed page parsing · `src/common/inject/inject.jsx`
- [Sandbox mgr](https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/sandboxManager.js): page support · Sandboxed page parsing and helper orchestration · `src/common/inject/sandboxManager.js`
- [Page prompts](https://github.com/zotero/zotero-connectors/blob/master/src/common/inject/modalPrompt_inject.js): page UI · Modal prompts and progress UI that run in page context · `src/common/inject/modalPrompt_inject.js`

### Background/runtime

Privileged extension process and browser APIs

- [BG entry](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/background.js): background entry · Main background/runtime entrypoint for WebExtension builds · `src/browserExt/background.js`
- [BG worker](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/background-worker.js): background worker · Worker-style background runtime for MV3 and privileged operations · `src/browserExt/background-worker.js`
- [State & prefs](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/prefs.js): runtime state · Preferences, keep-alive, offscreen, and browser-state coordination · `src/browserExt/prefs.js`
- [WebRequest](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/webRequestIntercept.js): browser hook · Browser request interception and related browser-specific plumbing · `src/browserExt/webRequestIntercept.js`
- [Offscreen](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/offscreen/offscreen.js): browser hook · Offscreen document support and translate helpers · `src/browserExt/offscreen/offscreen.js`

### Shared core

Cross-browser translation, messaging, save, and UI logic

- [Messaging core](https://github.com/zotero/zotero-connectors/blob/master/src/common/messaging.js): shared boundary · Shared messaging protocol and connector-style async method bridge · `src/common/messaging.js`
- [Translate core](https://github.com/zotero/zotero-connectors/blob/master/src/common/translate.js): translator engine · Translator matching, detection, page translation, and save orchestration · `src/common/translate.js`
- [Translators](https://github.com/zotero/zotero-connectors/blob/master/src/common/translators.js): translator registry · Translator lookup and coarse URL-based matching · `src/common/translators.js`
- [Item saver](https://github.com/zotero/zotero-connectors/blob/master/src/common/itemSaver.js): save orchestration · Creates items and coordinates save flows · `src/common/itemSaver.js`
- [Repo fetch](https://github.com/zotero/zotero-connectors/blob/master/src/common/repo.js): translator source · Retrieves translator code from Zotero repositories and zotero.org · `src/common/repo.js`

### Browser-specific

WebExtension and Safari shims and entrypoints

- [Browser shims](https://github.com/zotero/zotero-connectors/blob/master/src/browserExt/messaging_inject.js): browser-specific layer · Chrome, Firefox, and Safari adapters for messaging, i18n, and UI entrypoints · `src/browserExt/messaging_inject.js`
- [Safari entry](https://github.com/zotero/zotero-connectors/blob/master/src/safari/global.js): safari runtime · Safari global and injected entrypoints mirroring the WebExtension roles · `src/safari/global.js`

### External systems

Local Zotero connector and web API targets

- [Connector API](https://github.com/zotero/zotero-connectors/blob/master/src/common/connector.js): local connector · Local Zotero desktop connector HTTP bridge · `src/common/connector.js`
- [Web API](https://github.com/zotero/zotero-connectors/blob/master/src/common/api.js): cloud api · Zotero.org API fallback when desktop connector is unavailable · `src/common/api.js`

### Build & test

Packaging, CI, and automated tests

- [Build & CI](https://github.com/zotero/zotero-connectors/blob/master/gulpfile.js): build pipeline · Gulp/build scripts, packaging helpers, and GitHub Actions workflows · `gulpfile.js`
- [Tests](https://github.com/zotero/zotero-connectors/blob/master/test/tests/backgroundTest.mjs): test suite · Integration and runtime tests for messaging, translation, requests, and saving · `test/tests/backgroundTest.mjs`

## Connections

- Inject entry → Page inject: loads · Inject entry loads shared page-context logic
- Page inject → Sandbox mgr: uses · Page logic delegates parsing and isolation to sandbox helpers
- Page inject → Page prompts: shows · Page context can display prompts and progress UI
- Page inject → Messaging core: calls · Injected code reaches background through shared messaging bridge
- BG entry → Messaging core: handles · Background runtime serves message endpoints
- BG worker → Messaging core: handles · Worker background also participates in messaging
- Messaging core → Browser shims: adapts · Browser-specific adapters expose the shared messaging API
- Messaging core → Safari entry: adapts · Safari uses its own global and injected messaging shims
- BG entry → State & prefs: manages · Background owns preferences, offscreen, and keep-alive state
- BG entry → WebRequest: intercepts · Background may intercept browser requests for translator and content handling
- BG entry → Offscreen: uses · Background coordinates offscreen documents for translation work
- Translate core → Translators: matches · Translation starts with translator selection and detection
- Translate core → Item saver: saves · Core translation hands resolved items to the save subsystem
- Translate core → Messaging core: coordinates · Translate flow spans page and background through messaging
- Translate core → Repo fetch: fetches · Translator code is retrieved from repository sources
- Item saver → Connector API: prefers · Primary save target is the local Zotero desktop connector
- Item saver → Web API: falls back · Web API is used when desktop connector saving is unavailable
- BG entry → Translate core: orchestrates · Background coordinates the overall save and translation flow
- Safari entry → Translate core: hosts · Safari entrypoints host the same shared translation logic
- Build & CI → BG entry: packages · Build scripts assemble browser-specific background bundles
- Build & CI → Safari entry: packages · Build scripts also produce Safari-targeted artifacts
- Tests → Translate core: verifies · Tests cover translation and item saving behavior
- Tests → Messaging core: verifies · Tests cover cross-context messaging behavior
