> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mercuryworkshop/scramjet/llms.txt
> Use this file to discover all available pages before exploring further.

# Basic setup

> Step-by-step guide to set up Scramjet in your web application

This guide walks you through setting up Scramjet, a powerful web proxy framework that enables you to create isolated browsing contexts.

## Prerequisites

Before you begin, ensure you have:

* A web server capable of serving static files
* Service worker support in your target browsers
* The Scramjet distribution files (`scramjet.all.js`, `scramjet.wasm.wasm`, `scramjet.sync.js`)

## Installation

<Steps>
  <Step title="Include Scramjet files">
    Place the Scramjet distribution files in your project directory. These files should be accessible via your web server:

    ```
    /public/
      scramjet.all.js
      scramjet.wasm.wasm
      scramjet.sync.js
    ```
  </Step>

  <Step title="Register the service worker">
    Create a script to register Scramjet's service worker. This is essential for intercepting and proxying requests:

    ```javascript theme={null}
    // Register the service worker
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/sw.js', {
        scope: '/',
      }).then((registration) => {
        console.log('Scramjet service worker registered:', registration);
      }).catch((error) => {
        console.error('Service worker registration failed:', error);
      });
    }
    ```

    <Note>
      The service worker file (`sw.js`) should import Scramjet using `importScripts('/scramjet.all.js')` and set up the fetch handler. See the [quickstart guide](/quickstart) for a complete service worker example.
    </Note>

    <Note>
      The `scope` parameter defines the URL prefix that the service worker will intercept. By default, Scramjet uses `/scramjet/`.
    </Note>
  </Step>

  <Step title="Initialize the controller">
    After the service worker is registered and ready, load and initialize the Scramjet controller:

    ```javascript theme={null}
    // Wait for service worker to be ready
    await navigator.serviceWorker.ready;

    // Load the controller module
    const { ScramjetController } = $scramjetLoadController();

    // Create a controller instance with configuration
    const scramjet = new ScramjetController({
      prefix: '/scramjet/',
      flags: {
        strictRewrites: true,
        captureErrors: true,
      },
    });

    // Initialize the controller
    await scramjet.init();
    ```
  </Step>

  <Step title="Create and use a frame">
    Create an isolated iframe context for proxied browsing:

    ```javascript theme={null}
    // Create a new Scramjet frame
    const frame = scramjet.createFrame();

    // Add the iframe to your page
    document.body.appendChild(frame.frame);

    // Navigate to a URL
    frame.go('https://example.com');
    ```
  </Step>
</Steps>

## Complete example

Here's a full working example that ties everything together:

```html theme={null}
<!DOCTYPE html>
<html>
<head>
  <title>Scramjet Example</title>
  <style>
    iframe {
      width: 100%;
      height: 600px;
      border: 1px solid #ccc;
    }
  </style>
</head>
<body>
  <div id="controls">
    <input type="text" id="urlInput" placeholder="Enter URL" />
    <button id="goBtn">Go</button>
  </div>
  <div id="frameContainer"></div>

  <script>
    (async () => {
      // Step 1: Register service worker
      if ('serviceWorker' in navigator) {
        await navigator.serviceWorker.register('/scramjet.all.js', {
          scope: '/scramjet/',
        });
      }

      // Step 2: Wait for service worker to be ready
      await navigator.serviceWorker.ready;

      // Step 3: Initialize Scramjet controller
      const { ScramjetController } = $scramjetLoadController();
      const scramjet = new ScramjetController({
        prefix: '/scramjet/',
      });
      await scramjet.init();

      // Step 4: Create frame
      const frame = scramjet.createFrame();
      document.getElementById('frameContainer').appendChild(frame.frame);

      // Step 5: Set up navigation controls
      const urlInput = document.getElementById('urlInput');
      const goBtn = document.getElementById('goBtn');

      goBtn.addEventListener('click', () => {
        const url = urlInput.value;
        if (url) {
          frame.go(url);
        }
      });

      // Navigate on Enter key
      urlInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
          goBtn.click();
        }
      });

      // Optional: Listen for URL changes
      frame.addEventListener('urlchange', (event) => {
        console.log('URL changed to:', event.url);
        urlInput.value = event.url;
      });
    })();
  </script>
</body>
</html>
```

## Configuration options

The `ScramjetController` constructor accepts a configuration object with the following options:

<Accordion title="prefix">
  The URL prefix for the proxy. Defaults to `/scramjet/`.

  ```javascript theme={null}
  const scramjet = new ScramjetController({
    prefix: '/proxy/',
  });
  ```
</Accordion>

<Accordion title="flags">
  Feature flags that control Scramjet's behavior. See [Configuration flags](/guides/configuration-flags) for details.

  ```javascript theme={null}
  const scramjet = new ScramjetController({
    flags: {
      strictRewrites: true,
      captureErrors: true,
      sourcemaps: true,
    },
  });
  ```
</Accordion>

<Accordion title="codec">
  Custom URL encoding/decoding functions. See [Custom codecs](/guides/custom-codecs) for details.

  ```javascript theme={null}
  const scramjet = new ScramjetController({
    codec: {
      encode: (url) => btoa(url),
      decode: (url) => atob(url),
    },
  });
  ```
</Accordion>

<Accordion title="files">
  Paths to Scramjet distribution files.

  ```javascript theme={null}
  const scramjet = new ScramjetController({
    files: {
      wasm: '/scramjet.wasm.wasm',
      all: '/scramjet.all.js',
      sync: '/scramjet.sync.js',
    },
  });
  ```
</Accordion>

## Next steps

<CardGroup cols={2}>
  <Card title="Working with frames" icon="window" href="/guides/frames">
    Learn how to create and manage isolated browsing contexts
  </Card>

  <Card title="Event handling" icon="bolt" href="/guides/event-handling">
    Handle navigation and download events
  </Card>

  <Card title="Configuration flags" icon="flag" href="/guides/configuration-flags">
    Customize Scramjet's behavior with feature flags
  </Card>

  <Card title="Custom codecs" icon="code" href="/guides/custom-codecs">
    Implement custom URL encoding strategies
  </Card>
</CardGroup>
