How zotero/zotero-connectors works
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: translate.js Translate core · messaging.js Messaging core · background.js BG entry · inject.jsx Page inject
Components 20
Page context
Injected scripts and page-local translation
- Inject entry · Browser and Safari entrypoints that load page-context code
src/browserExt/inject/virtualOffscreenTranslate.js - Page inject · Shared injected logic for scraping, detection, and sandboxed page parsing
src/common/inject/inject.jsx - Sandbox mgr · Sandboxed page parsing and helper orchestration
src/common/inject/sandboxManager.js - Page prompts · 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 · Main background/runtime entrypoint for WebExtension builds
src/browserExt/background.js - BG worker · Worker-style background runtime for MV3 and privileged operations
src/browserExt/background-worker.js - State & prefs · Preferences, keep-alive, offscreen, and browser-state coordination
src/browserExt/prefs.js - WebRequest · Browser request interception and related browser-specific plumbing
src/browserExt/webRequestIntercept.js - Offscreen · Offscreen document support and translate helpers
src/browserExt/offscreen/offscreen.js
Shared core
Cross-browser translation, messaging, save, and UI logic
- Messaging core · Shared messaging protocol and connector-style async method bridge
src/common/messaging.js - Translate core · Translator matching, detection, page translation, and save orchestration
src/common/translate.js - Translators · Translator lookup and coarse URL-based matching
src/common/translators.js - Item saver · Creates items and coordinates save flows
src/common/itemSaver.js - Repo fetch · Retrieves translator code from Zotero repositories and zotero.org
src/common/repo.js
Browser-specific
WebExtension and Safari shims and entrypoints
- Browser shims · Chrome, Firefox, and Safari adapters for messaging, i18n, and UI entrypoints
src/browserExt/messaging_inject.js - Safari entry · Safari global and injected entrypoints mirroring the WebExtension roles
src/safari/global.js
External systems
Local Zotero connector and web API targets
- Connector API · Local Zotero desktop connector HTTP bridge
src/common/connector.js - Web API · Zotero.org API fallback when desktop connector is unavailable
src/common/api.js
Build & test
Packaging, CI, and automated tests
- Build & CI · Gulp/build scripts, packaging helpers, and GitHub Actions workflows
gulpfile.js - Tests · Integration and runtime tests for messaging, translation, requests, and saving
test/tests/backgroundTest.mjs
Connections 23
- 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