Building Browser Extensions with Natural Language: A Complete Workflow

    This article walks through building a browser extension that turns any clicked image into a reusable generation prompt, using natural language to drive the development process. It covers how to translate plain-language requirements into technical specifications, the minimum viable structure of an extension, API key and parameter configuration, prompt engineering for output quality, a troubleshooting table for common failures, and directions for caching, batch processing, and result management. The focus stays on configuration management, security boundaries, and output optimization rather than syntax memorization.

    1. Introduction: From Writing Code to Stating Requirements

    Building even a simple browser extension used to require clearing several technical hurdles at once: understanding the manifest file structure, knowing how content scripts communicate with background scripts, declaring the right permissions for cross-origin requests, and reading the API documentation of vision-capable models. A mistake in any one of these areas could stop the extension from loading or break its functionality.

    Mature AI coding tools are now restructuring that workflow. A developer describes the requirement in natural language, the tool generates a runnable code skeleton, and the developer then handles installation, configuration, and debugging. This article uses an extension that generates prompts when you click an image on a web page as its technical vehicle, breaking down the full path from requirement description to daily use, with emphasis on configuration management, security boundaries, and output quality.

    2. Requirement Abstraction: Turning Plain Language into a Technical Spec

    The key to natural-language-driven development is not elegant phrasing but completeness. A requirement a machine can interpret accurately usually contains four kinds of information:

    • Trigger: the scenario and the user action that activates the feature;
    • Input target: what the feature operates on and which information must be extracted;
    • Processing logic: how the data is transformed and whether an external capability is called;
    • Output form: how the result is presented and whether it needs to be copied or exported.

    For the scenario in this article, the requirement can be stated like this:

    1. Build a browser extension that:
    2. 1. When I click any image on a web page, reads the image URL and its visible content;
    3. 2. Sends the image to a vision model and generates a descriptive prompt suitable for image generation tasks;
    4. 3. Displays the result in an overlay near the image, with a one-click copy button;
    5. 4. Allows the model service endpoint, API key, and model name to be configured on a settings page.

    The value of this description is that it converts a vague “I want an extension” into verifiable acceptance criteria. The more specific the requirement, the less rework the generated code needs.

    3. Architecture: The Minimum Viable Structure of an Extension

    Even when the code is generated by a tool, the developer still needs to read and judge its structure. A typical browser extension contains:

    1. Manifest file: declares the extension name, version, permission scope, content script match rules, and the settings page entry;
    2. Content script: runs in the page context, listens for click events, locates the image element, and renders the result overlay;
    3. Background script or configuration page: handles API key retrieval, request forwarding, and retry logic;
    4. Settings interface: provides visual configuration for the endpoint, API key, and model name.

    The principle of least privilege deserves special attention. Request only the permissions the feature genuinely needs, such as script injection on specific pages and storage read/write access, and avoid overly broad host match patterns. This is both a security requirement and a basic precondition for passing extension review.

    4. Configuration: API Key Management and Parameter Setup

    Once the extension is generated, the most critical step is connecting it to a model service. Using a cloud vendor’s large model platform as an example, the flow is common across the industry:

    • Enable the service in the console and create an access key;
    • Paste the key into the API key field on the extension’s settings page;
    • Fill in the endpoint and model name according to the platform documentation; paths differ between vendors and must not be mixed;
    • After saving, verify the connection with a test image.

    An illustrative request structure follows. It only explains what the fields mean and does not represent the real API of any specific platform:

    1. {
    2. "model": "<your model name>",
    3. "messages": [
    4. { "role": "user", "content": [
    5. { "type": "image_url", "image_url": { "url": "<image URL>" } },
    6. { "type": "text", "text": "Summarize the main subject, style, composition, and lighting in one structured description" }
    7. ]}
    8. ]
    9. }

    Security note: the API key should be stored in the extension’s local storage. Never hard-code it into the source, and never distribute it together with the extension package. For team collaboration, each member should use a separate key so that usage tracking and access revocation stay straightforward.

    5. Prompt Engineering: The Core Variable Behind Output Quality

    The same image can produce very different results depending on how the prompt is written. Three principles work well in practice:

    • Define the role and task clearly: tell the model it is an image describer whose output target is a reusable generation prompt;
    • Constrain the output structure: require the response to follow the order of subject, scene, style, camera, lighting, and color, avoiding long prose;
    • Control length and language: set a word range and specify the output language so the result can be pasted directly.

    A reusable system prompt example:

    1. You are an image prompt assistant. Observe the input image and output a Chinese prompt
    2. covering, in order: subject, action, environment, composition, camera, lighting, color, and image quality.
    3. Keep the total length under 80 characters and do not output explanatory text.

    6. Common Errors and Debugging Path

    In practice, problems cluster into four categories:

    Symptom Common cause Fix
    Click does nothing Content script does not match the current page Check the manifest match rules and reload the page
    Request fails Wrong API key or mistyped endpoint Copy the key again and verify the API path
    Empty response The model does not accept image input Switch to a model with vision capability
    Result too long Prompt lacks a length constraint Add word count and structure limits

    When debugging, open the developer tools log panel and inspect the request status code and response body. First determine whether no request was sent or the request was rejected, then work through the layers from there.

    7. Optimization and Next Steps

    Once the basic feature works, three directions are worth pursuing:

    • Caching: cache results locally by image URL to reduce repeated calls and waiting;
    • Batch processing: support region selection or queue-based processing of all images on a page;
    • Result management: add history and favorites so one-off outputs become reusable assets.

    If you use Baidu AI Cloud model services, the same general path applies: create a key in the console, fill in the endpoint and model name, and keep the key in local storage. Refer to the official documentation for the exact fields.