How rob--w/crxviewer works
This project is a Chrome (and multi‑browser) extension called “CRX Viewer” that lets users view the source code of any Chrome extension without installing it. The extension achieves this by downloading a CRX (or similar extension package) from various extension stores, converting that package (using helper libraries to extract or “transpile” it into a zip file format), and then rendering the source files in a browser tab with features such as syntax highlighting, beautification, file search, and even hash calculation.
Main Components
• Extension UI Components:
• Viewer UI: Represented by files such as crxviewer.html, crxviewer.js, and associated LESS/CSS (e.g., crxviewer.less).
• Popup UI: popup.html and popup.js that provide a popup interface.
• Options page: options.html and options.js where users can configure behaviors.
• Background/Service Components:
• Background scripts like background.js, service_worker.js, and bg-contextmenu.js handle event listening, context menu creation, and coordinate tasks on actions.
• Core Logic and Helper Libraries:
• CRX-to-ZIP conversion: See src/lib/crx-to-zip.js.
• File processing utilities: Libraries for beautification (src/lib/beautify), prettification/syntax highlighting (src/lib/prettify), and ZIP handling (src/lib/zip.js and its supporting files).
• Other utility scripts such as domain-fronter.js (possibly handling cross-domain issues) and modern-crypto.js for cryptographic functions.
• External / Build Scripts:
• Files under “external”, “chrome-web-store”, “firefox-stuff”, and “opera-addon-gallery” indicate support scripts to build or package the extension for different browsers.
• The make.js and package.json file are used for building and managing dependencies.
Relationships and Interactions
• User Interaction Flow:
• The extension installs and injects a browser button on CRX detection (or through context menu via bg-contextmenu.js).
• When the user clicks the button, the background scripts handle the event.
• Action handlers then either download the extension (CRX file) from an external source (e.g., the Chrome Web Store) or process local files provided by the user.
• Processing Flow:
• The CRX file is fetched and then processed by helper libraries (notably the “crx-to-zip” converter) to extract the embedded files.
• Once extracted, the file names and contents are passed to the viewer module.
• Additional libraries are used to beautify code, apply syntax highlighting, search through files, display file hashes, and optionally handle embedded zip files.
• Data Flow:
• Then, flow to the UI layer where the resulting source files are displayed and interacted with (filtering, searching, etc.).
Architectural Patterns and Design Principles
• Separation of Concerns:
• UI and view layers are clearly separated (HTML pages, CSS/LESS, JS handling user actions) from the background processing logic.
• Modularity:
• Core functionality (conversion, beautification, and file manipulation) is abstracted in library modules under src/lib.
• Cross-browser Compatibility:
• Presence of separate manifest files (manifest.json, manifest_firefox.json, manifest_opera.json) and specific directories for browser-specific resources indicate the design’s attention to supporting multiple browsers.
• Event-Driven Architecture:
• Using background scripts and service workers to respond to user events (context menu, toolbar clicks) and asynchronous file processing benefits from event-driven design.