# Browser Markdown Scrape

URL: /docs/experiments/browser-markdown-scrape
> Convert any URL to Markdown or scrape CSS with Browser Rendering quickAction - JS-rendered pages to clean text for LLMs
Tags: browser, markdown, scraping, llms, browser-rendering, content-extraction
Bindings: BROWSER
Markdown: /llms.mdx/experiments/browser-markdown-scrape/content.md



Convert a URL to Markdown and scrape CSS selectors using Cloudflare Browser Rendering **quickAction** (REST-style binding - not Puppeteer).

## Features [#features]

* `GET /markdown?url=` → page Markdown via `quickAction("markdown")`
* `GET /scrape?url=&selector=` → structured elements via `quickAction("scrape")`
* HTTP/HTTPS URL validation only

## API Reference [#api-reference]

### GET /markdown [#get-markdown]

**`url`** `string` (required)

Target page (`http://` or `https://`).

#### Example Request [#example-request]

```bash
curl "https://your-worker.workers.dev/markdown?url=https://example.com"
```

#### Success Response [#success-response]

```json
{
  "url": "https://example.com/",
  "markdown": "# Example Domain\n\n..."
}
```

#### Error Codes [#error-codes]

* `400` - Missing or non-http(s) url (`INVALID_URL`)
* `502` - quickAction failure (`MARKDOWN_ERROR`)

### GET /scrape [#get-scrape]

**`url`** `string` (required)

Target page.

**`selector`** `string` (required)

CSS selector (sent as one scrape element).

#### Example Request [#example-request-1]

```bash
curl "https://your-worker.workers.dev/scrape?url=https://example.com&selector=h1"
```

#### Success Response [#success-response-1]

```json
{
  "url": "https://example.com/",
  "results": [
    {
      "selector": "h1",
      "results": [{ "text": "Example Domain", "html": "Example Domain" }]
    }
  ]
}
```

#### Error Codes [#error-codes-1]

* `400` - Invalid url (`INVALID_URL`)
* `400` - Missing selector (`MISSING_SELECTOR`)
* `502` - quickAction failure (`SCRAPE_ERROR`)

## Use Cases [#use-cases]

* Normalize pages to Markdown for RAG / LLM pipelines
* Extract specific DOM fields without writing Puppeteer scripts
* Compare quickAction REST bindings vs Puppeteer-based experiments

## Limitations [#limitations]

* Requires Browser Rendering (often `wrangler dev --remote` locally)
* Single CSS selector per scrape request
* JS-heavy SPAs may need advanced goto options (not exposed in this demo)

## Use in your project [#use-in-your-project]

<UseInYourProject
  experiment="browser-markdown-scrape"
  bindings="[&#x22;Browser Rendering&#x22;]"
  platform="[&#x22;Workers runtime&#x22;]"
  files="[
  &#x22;src/lib/browser.ts&#x22;,
  &#x22;src/lib/url.ts&#x22;,
  &#x22;src/constants/defaults.ts&#x22;,
  &#x22;examples/usage.ts&#x22;,
]"
  sources="[{&#x22;path&#x22;:&#x22;src/lib/browser.ts&#x22;,&#x22;title&#x22;:&#x22;browser.ts&#x22;,&#x22;lang&#x22;:&#x22;ts&#x22;,&#x22;code&#x22;:&#x22;import type { BrowserRun } from \&#x22;../types/env\&#x22;;\nimport type { QuickActionPayload } from \&#x22;../types/browser\&#x22;;\n\nfunction isResponse(value: unknown): value is Response {\n  return (\n    typeof value === \&#x22;object\&#x22; &&\n    value !== null &&\n    typeof (value as Response).json === \&#x22;function\&#x22; &&\n    typeof (value as Response).ok === \&#x22;boolean\&#x22;\n  );\n}\n\n/**\n * Call Browser Rendering quickAction and normalize Response or result object to a payload.\n */\nexport async function runQuickAction(\n  browser: BrowserRun,\n  action: \&#x22;markdown\&#x22; | \&#x22;scrape\&#x22;,\n  params: Record<string, unknown>\n): Promise<QuickActionPayload> {\n  const raw = await browser.quickAction(action, params);\n\n  if (isResponse(raw)) {\n    let body: QuickActionPayload;\n    try {\n      body = (await raw.json()) as QuickActionPayload;\n    } catch {\n      throw new Error(`Browser ${action} returned non-JSON response`);\n    }\n    if (!raw.ok || body.success === false) {\n      throw new Error(body.error ?? `Browser ${action} failed (${raw.status})`);\n    }\n    return body;\n  }\n\n  if (raw.success === false) {\n    throw new Error(typeof raw.result === \&#x22;string\&#x22; ? raw.result : `Browser ${action} failed`);\n  }\n\n  return raw;\n}\n\nexport function extractMarkdown(payload: QuickActionPayload): string {\n  const { result } = payload;\n  if (typeof result === \&#x22;string\&#x22;) return result;\n  if (\n    result &&\n    typeof result === \&#x22;object\&#x22; &&\n    \&#x22;markdown\&#x22; in result &&\n    typeof (result as { markdown: unknown }).markdown === \&#x22;string\&#x22;\n  ) {\n    return (result as { markdown: string }).markdown;\n  }\n  throw new Error(\&#x22;Browser markdown response missing result string\&#x22;);\n}\n\nexport function extractScrapeResults(payload: QuickActionPayload): unknown {\n  const { result } = payload;\n  if (Array.isArray(result)) return result;\n  if (\n    result &&\n    typeof result === \&#x22;object\&#x22; &&\n    \&#x22;results\&#x22; in result &&\n    Array.isArray((result as { results: unknown }).results)\n  ) {\n    return (result as { results: unknown[] }).results;\n  }\n  throw new Error(\&#x22;Browser scrape response missing results array\&#x22;);\n}&#x22;},{&#x22;path&#x22;:&#x22;src/lib/url.ts&#x22;,&#x22;title&#x22;:&#x22;url.ts&#x22;,&#x22;lang&#x22;:&#x22;ts&#x22;,&#x22;code&#x22;:&#x22;import { ALLOWED_SCHEMES } from \&#x22;../constants/defaults\&#x22;;\n\nexport function validateUrl(input: string | undefined): string | null {\n  if (!input || typeof input !== \&#x22;string\&#x22;) return null;\n  const trimmed = input.trim();\n  if (!trimmed) return null;\n  try {\n    const url = new URL(trimmed);\n    if (!ALLOWED_SCHEMES.includes(url.protocol as (typeof ALLOWED_SCHEMES)[number])) {\n      return null;\n    }\n    return url.href;\n  } catch {\n    return null;\n  }\n}&#x22;},{&#x22;path&#x22;:&#x22;src/constants/defaults.ts&#x22;,&#x22;title&#x22;:&#x22;defaults.ts&#x22;,&#x22;lang&#x22;:&#x22;ts&#x22;,&#x22;code&#x22;:&#x22;export const ALLOWED_SCHEMES = [\&#x22;http:\&#x22;, \&#x22;https:\&#x22;] as const;&#x22;},{&#x22;path&#x22;:&#x22;examples/usage.ts&#x22;,&#x22;title&#x22;:&#x22;usage.ts&#x22;,&#x22;lang&#x22;:&#x22;ts&#x22;,&#x22;code&#x22;:&#x22;/**\n * Minimal paste example for docs (\&#x22;Use in your project\&#x22;).\n * Not part of the Worker runtime — copy into your own project.\n *\n * Typical API: See this experiment's routes for the HTTP API.\n * Wire extractMarkdown() the same way src/routes/ does (see that file for args / bindings).\n */\nimport { extractMarkdown } from \&#x22;../src/lib/browser\&#x22;;\n\nexport default {\n  async fetch(request: Request, env: unknown): Promise<Response> {\n    // Typical API: See this experiment's routes for the HTTP API.\n    // Replace the placeholder call with the arguments used in this experiment's routes.\n    const result = extractMarkdown(/* args from src/routes/, env if needed */ env as never);\n    return Response.json(result);\n  },\n};&#x22;}]"
/>

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/browser-markdown-scrape)
  </Step>

  <Step>
    ### Confirm browser binding [#confirm-browser-binding]

    `wrangler.json` declares `"browser": { "binding": "BROWSER" }`.
  </Step>

  <Step>
    ### Test your deployment [#test-your-deployment]

    ```bash
    curl "https://your-worker.workers.dev/markdown?url=https://example.com"
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/browser-markdown-scrape
npm install
npm run dev -- --remote
```

```bash
curl "http://localhost:8787/markdown?url=https://example.com"
```

## Configuration [#configuration]

* **Browser binding** `BROWSER` - uses `quickAction("markdown" | "scrape", …)`

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Browser Rendering](https://developers.cloudflare.com/browser-rendering/)** - Headless browser at the edge
* **[Markdown endpoint](https://developers.cloudflare.com/browser-rendering/rest-api/markdown-endpoint/)** - URL → Markdown quick action
* **[Scrape endpoint](https://developers.cloudflare.com/browser-rendering/rest-api/scrape-endpoint/)** - CSS selector scrape quick action
