# niemingxing/search-recommendations architecture

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

- Interactive diagram: https://gitdiagram.com/niemingxing/search-recommendations
- Repository: https://github.com/niemingxing/search-recommendations

## Overview

##### Project Identification
- Project Type: Chrome extension.
- Purpose: Automate keyword and search recommendation collection, generate content using ChatGPT prompts, and post to external sites like WordPress. The extension provides user interfaces (popup and options pages) and leverages content scripts running in web pages.

##### Key Components
a. Manifest & Configuration:
- manifest.json: The central configuration file where permissions, script bindings, and extension metadata are defined.
b. UI Components:
- Popup UI: Represented by popup.html and its logic defined in js/popup.js, along with popup.css.
- Options UI: Represented by options.html and associated with js/options.js and options.css.
c. Background & Core Logic:
- Background Script: js/background.js handles extension lifecycle, event listening, and inter-component communication (e.g., message passing between components).
d. Content Interaction:
- Content Script: js/content-script.js, which interacts directly with the DOM of webpages to assist with data collection/injection.
- Inject Script: js/inject.js (if used to dynamically inject functionality into webpages).
e. Styling:
- CSS files (gpt_keywords_list.css, options.css, popup.css) that provide styling for the UI elements.
f. Other Utility Constructs:
- ChatGPT integration: Although not a separate physical file, the README describes prompt formats for generating and cleaning content – treat these as part of the business logic integrated within the extension’s operations.
g. External Dependencies & Services:
- External sites (小红书, 抖音, etc.) where keywords are gathered.
- WordPress (or other sites) for content posting.
- ChatGPT related API services (for content generation and cleaning).

##### Relationships and Interactions
- The extension starts by loading manifest.json, which declares the entry points.
- User interacts with the extension through the Popup UI (popup.html + js/popup.js) or Configuration via Options UI (options.html + js/options.js).
- The Background Script (js/background.js) listens to events and coordinates tasks. It can receive messages from the Popup, Options, and Content Scripts.
- Content Scripts (js/content-script.js) are injected into targeted webpages to collect search recommendation data. They may also trigger events routed back to the background script for processing.
- The Inject Script (js/inject.js) can add additional code to the pages if necessary.
- CSS files ensure that all UI elements have a consistent visual design.
- The extension may call external APIs (as per the ChatGPT prompt instructions in the README) to generate and clean content, and then post results to the configured site (such as a WordPress site).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Layer
    subgraph "Configuration Layer"
        M["manifest.json"]:::config
    end

    %% User Interface Layer
    subgraph "User Interface Layer"
        Popup["popup.html"]:::ui
        PopupJS["js/popup.js"]:::ui
        PopupCSS["css/popup.css"]:::ui
        Options["options.html"]:::ui
        OptionsJS["js/options.js"]:::ui
        OptionsCSS["css/options.css"]:::ui
    end

    %% Background Layer
    subgraph "Background Layer"
        BG["js/background.js"]:::bg
    end

    %% Content Interaction Layer
    subgraph "Content Interaction Layer"
        CS["js/content-script.js"]:::content
        Inject["js/inject.js"]:::content
    end

    %% Styling
    subgraph "Styling"
        GPTCSS["css/gpt_keywords_list.css"]:::css
    end

    %% External Services
    subgraph "External Services"
        ChatGPT["ChatGPT API"]:::external
        Search["外部搜索平台"]:::external
        WP["WordPress Content Posting"]:::external
    end

    %% Relationships and Interactions
    M -->|"declares"| Popup
    M -->|"declares"| Options
    M -->|"declares"| BG

    Popup -->|"UserAction"| BG
    Options -->|"ConfigUpdate"| BG
    BG -->|"UpdateUI"| Popup
    BG -->|"UpdateUI"| Options

    BG -->|"CollectKeywords"| Search
    BG -->|"DataCollection"| CS
    CS -->|"Response"| BG
    BG -->|"InjectFunction"| Inject
    BG -->|"GenerateContent"| ChatGPT
    BG -->|"PostContent"| WP

    %% Styles
    classDef config fill:#cce5ff,stroke:#0000FF,stroke-width:2px;
    classDef ui fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef bg fill:#98FB98,stroke:#006400,stroke-width:2px;
    classDef content fill:#DDA0DD,stroke:#800080,stroke-width:2px;
    classDef css fill:#FFDAB9,stroke:#FF8C00,stroke-width:2px;
    classDef external fill:#FFCC99,stroke:#FF8C00,stroke-width:2px;

    %% Click Events
    click M "https://github.com/niemingxing/search-recommendations/blob/master/manifest.json"
    click Popup "https://github.com/niemingxing/search-recommendations/blob/master/popup.html"
    click PopupJS "https://github.com/niemingxing/search-recommendations/blob/master/js/popup.js"
    click PopupCSS "https://github.com/niemingxing/search-recommendations/blob/master/css/popup.css"
    click Options "https://github.com/niemingxing/search-recommendations/blob/master/options.html"
    click OptionsJS "https://github.com/niemingxing/search-recommendations/blob/master/js/options.js"
    click OptionsCSS "https://github.com/niemingxing/search-recommendations/blob/master/css/options.css"
    click BG "https://github.com/niemingxing/search-recommendations/blob/master/js/background.js"
    click CS "https://github.com/niemingxing/search-recommendations/blob/master/js/content-script.js"
    click Inject "https://github.com/niemingxing/search-recommendations/blob/master/js/inject.js"
    click GPTCSS "https://github.com/niemingxing/search-recommendations/blob/master/css/gpt_keywords_list.css"
```
