Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/webviewjs/webview/llms.txt

Use this file to discover all available pages before exploring further.

WebviewJS gives Node.js direct read/write access to the cookies stored in any webview, plus coarser controls to nuke all browsing data or to start a fully in-memory session. When you need two webviews to share the same cookie jar and localStorage — for example a split-pane browser or a side-by-side comparison view — wrap them in a shared WebContext. When you need the opposite, full isolation with no persistence at all, use incognito mode.

WebContext: Shared Storage Across Webviews

A WebContext is a container for all browsing data: cookies, cache, localStorage, IndexedDB, and service workers. By default every webview gets its own isolated context. Create a named context with app.createWebContext() and pass it to createWebview() to make two or more webviews share the same data directory.
import { resolve } from 'node:path';
import { Application } from '@webviewjs/webview';

const app = new Application();

// Create a named context backed by a directory on disk
const context = app.createWebContext({
  dataDirectory: resolve('.my-app-profile'),
});

console.log('Data directory:', context.dataDirectory);

const win1 = app.createBrowserWindow({ title: 'Tab 1', width: 700, height: 500 });
const win2 = app.createBrowserWindow({ title: 'Tab 2', width: 700, height: 500, x: 740 });

// Both webviews share the same cookies and localStorage
win1.createWebview({ url: 'https://example.com', webContext: context });
win2.createWebview({ url: 'https://example.com', webContext: context });

app.on('application-close-requested', () => app.exit());
app.run();
A WebContext is also a disposable resource — call context.dispose() or use using context = ... to release it when you are done. Check context.isDisposed() before accessing a context stored in long-lived state.

Reading Cookies

webview.getCookies(url?) returns an array of WebviewCookie objects. Pass a URL to get only the cookies that would be sent with a request to that URL, or omit it to get every cookie the webview holds.
// Cookies scoped to a specific URL
const cookies = webview.getCookies('https://example.com');

// Every cookie in the webview's jar
const allCookies = webview.getCookies();

for (const c of cookies) {
  console.log(`${c.name}=${c.value}  domain:${c.domain}  path:${c.path}`);
  if (c.httpOnly) console.log('  (httpOnly)');
  if (c.secure)   console.log('  (secure)');
}

WebviewCookie Shape

FieldTypeDescription
namestringCookie name
valuestringCookie value
domainstring?Owning domain (e.g. "example.com")
pathstring?URL path scope (e.g. "/")
httpOnlyboolean?Not accessible from JavaScript
secureboolean?Sent only over HTTPS
sameSite'strict' | 'lax' | 'none'?Cross-site policy

webview.setCookie(cookie) stores a cookie in the webview’s current session. All fields except name and value are optional.
// Minimal cookie
webview.setCookie({ name: 'theme', value: 'dark' });

// Full cookie with all attributes
webview.setCookie({
  name: 'session',
  value: 'tok_abc123',
  domain: 'example.com',
  path: '/',
  httpOnly: true,
  secure: true,
  sameSite: 'strict',
});
Cookies set with httpOnly: true are invisible to document.cookie in the page. Use this for session tokens that should only be read by the server (or in this case, by your Node process through getCookies()).

webview.deleteCookie(name, domain?, path?) removes a matching cookie. Supplying domain and path narrows the match to a specific cookie when multiple cookies share the same name across different scopes.
// Delete by name across all domains and paths
webview.deleteCookie('theme');

// Delete a specific scoped cookie
webview.deleteCookie('session', 'example.com', '/');

// Delete a session cookie on a subdomain
webview.deleteCookie('auth', 'api.example.com', '/v2');

Clearing All Browsing Data

webview.clearAllBrowsingData() wipes everything: cookies, cache, localStorage, IndexedDB, and session data. This is equivalent to “Clear browsing data” in a browser’s settings.
// Sign out and wipe all local data
async function signOut() {
  await saveUserPreferences();  // save anything you want to keep
  webview.clearAllBrowsingData();
  webview.loadUrl('app://localhost/login');
}
clearAllBrowsingData() is irreversible and affects the entire webview context, including any data shared with other webviews using the same WebContext. Call it only when you intend to reset everything.

Incognito Mode

Pass incognito: true in WebviewOptions to start a webview with no persistent storage. Cookies, cache, localStorage, and IndexedDB exist only in memory for the lifetime of that webview instance and are discarded when the webview is disposed or the application exits.
const webview = win.createWebview({
  url: 'https://example.com',
  incognito: true,
});
Incognito webviews:
  • Do not persist cookies between runs
  • Do not share state with non-incognito webviews
  • Do not write to any dataDirectory
  • Behave like a fresh browser profile on every launch
You can mix incognito and non-incognito webviews in the same application. They will have completely separate cookie jars and storage even if they navigate to the same origin.

WebContext Utility Methods

Check whether a custom URL scheme has already been registered on a context. Useful when you conditionally register protocols based on configuration.
const context = app.createWebContext({ dataDirectory: './profile' });

if (!context.isCustomProtocolRegistered('app')) {
  win.registerProtocol('app', fileHandler);
}

import { resolve } from 'node:path';
import { Application } from '@webviewjs/webview';

const app = new Application();
const context = app.createWebContext({
  dataDirectory: resolve('.my-app-profile'),
});

const win = app.createBrowserWindow({ title: 'Cookie Demo' });
const webview = win.createWebview({
  url: 'https://example.com',
  webContext: context,
});

// Inject a session cookie before the page loads
webview.setCookie({
  name: 'session',
  value: 'demo-token',
  domain: 'example.com',
  path: '/',
  httpOnly: true,
  secure: true,
  sameSite: 'strict',
});

// Read it back after the page finishes loading
webview.on('page-load-finished', () => {
  const cookies = webview.getCookies('https://example.com');
  console.log('Cookies for example.com:');
  for (const c of cookies) {
    console.log(`  ${c.name}=${c.value}`);
  }
});

app.on('application-close-requested', () => app.exit());
app.run();

Build docs developers (and LLMs) love