> ## 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.

# ScramjetClient

> Core client for controlling sandboxing and proxying

The `ScramjetClient` class is the core client that runs inside proxified contexts. It handles DOM API interception, URL rewriting, and provides the sandboxing infrastructure.

<Note>
  Most users won't need to interact with `ScramjetClient` directly. It's automatically created and managed by `ScramjetController` and `ScramjetFrame`.
</Note>

## Constructor

Creates a new `ScramjetClient` instance.

```typescript theme={null}
new ScramjetClient(global: typeof globalThis)
```

<ParamField path="global" type="typeof globalThis" required>
  The global object to apply Scramjet sandboxing to (typically `window` or `self`)
</ParamField>

<Warning>
  Only one `ScramjetClient` instance can exist per global context. Attempting to create multiple instances will throw an error.
</Warning>

## Properties

### global

The global object this client is sandboxing.

```typescript theme={null}
global: typeof globalThis
```

### url

Gets or sets the current proxified URL.

```typescript theme={null}
get url(): URL
set url(url: URL | string)
```

#### Example

```typescript theme={null}
console.log("Current URL:", client.url.href);

// Navigate to a new URL
client.url = "https://example.com";
```

### locationProxy

The proxified location object.

```typescript theme={null}
locationProxy: Location
```

### bare

The `BareClient` instance for making bare requests.

```typescript theme={null}
bare: BareClient
```

### cookieStore

The cookie store for cookie emulation.

```typescript theme={null}
cookieStore: CookieStore
```

### natives

Store for accessing native (unproxied) browser APIs.

```typescript theme={null}
natives: {
  store: Record<string, any>;
  call(target: string, that: any, ...args): any;
  construct(target: string, ...args): any;
}
```

#### Example

```typescript theme={null}
// Call native fetch without proxy interception
const response = client.natives.call(
  "fetch",
  window,
  "https://example.com"
);
```

### descriptors

Store for accessing native property descriptors.

```typescript theme={null}
descriptors: {
  store: Record<string, PropertyDescriptor>;
  get(target: string, that: any): any;
  set(target: string, that: any, value: any): void;
}
```

### frame

Returns the `ScramjetFrame` instance if this client is inside a frame, or `null` if top-level.

```typescript theme={null}
get frame(): ScramjetFrame | null
```

### isSubframe

Returns `true` if this client is running in a subframe (not created by `ScramjetFrame`).

```typescript theme={null}
get isSubframe(): boolean
```

## Methods

### hook()

Applies all Scramjet hooks to the global context. This method is called automatically during initialization.

```typescript theme={null}
hook(): void
```

<Info>
  This method loads and applies all DOM and worker hooks based on the current context.
</Info>

### loadcookies()

Loads cookies from a cookie string.

```typescript theme={null}
loadcookies(cookiestr: string): void
```

<ParamField path="cookiestr" type="string" required>
  The cookie string to load
</ParamField>

### Proxy()

Proxies a function or constructor to intercept calls.

```typescript theme={null}
Proxy(name: string | string[], handler: Proxy): void
```

<ParamField path="name" type="string | string[]" required>
  The name(s) of the API to proxy (e.g., `"fetch"` or `"XMLHttpRequest.prototype.open"`)
</ParamField>

<ParamField path="handler" type="Proxy" required>
  Handler object with `apply` and/or `construct` methods
</ParamField>

#### Example

```typescript theme={null}
client.Proxy("fetch", {
  apply(ctx) {
    console.log("Fetch called with:", ctx.args);
    // Modify args before calling
    ctx.args[0] = rewriteUrl(ctx.args[0]);
    return ctx.call();
  }
});
```

### Trap()

Traps a property to intercept get/set operations.

```typescript theme={null}
Trap<T>(name: string | string[], descriptor: Trap<T>): PropertyDescriptor
```

<ParamField path="name" type="string | string[]" required>
  The name(s) of the property to trap (e.g., `"window.location.href"`)
</ParamField>

<ParamField path="descriptor" type="Trap<T>" required>
  Descriptor object with `get` and/or `set` methods
</ParamField>

#### Example

```typescript theme={null}
client.Trap("document.cookie", {
  get(ctx) {
    // Return proxified cookies
    return client.cookieStore.getCookies();
  },
  set(ctx, value) {
    // Handle cookie setting
    client.cookieStore.setCookie(value);
  }
});
```

### RawProxy()

Lower-level proxy method that operates directly on objects.

```typescript theme={null}
RawProxy(target: any, prop: string, handler: Proxy): void
```

<ParamField path="target" type="any" required>
  The target object
</ParamField>

<ParamField path="prop" type="string" required>
  The property name
</ParamField>

<ParamField path="handler" type="Proxy" required>
  Handler object
</ParamField>

### RawTrap()

Lower-level trap method that operates directly on objects.

```typescript theme={null}
RawTrap<T>(target: any, prop: string, descriptor: Trap<T>): PropertyDescriptor
```

<ParamField path="target" type="any" required>
  The target object
</ParamField>

<ParamField path="prop" type="string" required>
  The property name
</ParamField>

<ParamField path="descriptor" type="Trap<T>" required>
  Descriptor object
</ParamField>

## Type definitions

### Proxy

Handler for proxying functions and constructors.

```typescript theme={null}
type Proxy = {
  construct?(ctx: ProxyCtx): any;
  apply?(ctx: ProxyCtx): any;
};

type ProxyCtx = {
  fn: Function;
  this: any;
  args: any[];
  newTarget: Function;
  return: (r: any) => void;
  call: () => any;
};
```

### Trap

Handler for trapping properties.

```typescript theme={null}
type Trap<T> = {
  writable?: boolean;
  value?: any;
  enumerable?: boolean;
  configurable?: boolean;
  get?: (ctx: TrapCtx<T>) => T;
  set?: (ctx: TrapCtx<T>, v: T) => void;
};

type TrapCtx<T> = {
  this: any;
  get: () => T;
  set: (v: T) => void;
};
```

See [module types](/api/module-types) for complete type definitions.
