I have a script that reads my work WhatsApp every 15 minutes and lists the messages still
waiting on a reply. On a Mac it copies the app's SQLite database, which
[keeps every message unencrypted](/posts/whatsapp-for-mac-internals). In November I had
to make the same script run on Windows, for an always-on laptop. I built it in a Windows
11 virtual machine, and the Mac approach stopped working there.

On the Windows build I installed on 27 November 2026, the message text in the app's local
storage was encrypted, so copying its files got me nothing readable. The running app
holds the decrypted messages in memory, and the script reads them from there.

The script is read-only. It never sends a message, clicks anything or marks a chat as
read.

## WhatsApp for Windows runs in WebView2

WhatsApp for Windows is WhatsApp Web running inside Microsoft Edge WebView2, the
component Windows apps use to embed Chromium. WebView2 reads extra Chromium command-line
flags from an environment variable, including the flag that opens the DevTools remote
debugging port:

```powershell
[Environment]::SetEnvironmentVariable(
  'WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS',
  '--remote-debugging-port=9229 --remote-allow-origins=http://127.0.0.1:9229',
  'User')
```

After WhatsApp is started again, it listens on 127.0.0.1:9229 like any Chrome launched
with that flag. Playwright's `connectOverCDP` attaches to it, and the script finds the
page whose URL starts with `https://web.whatsapp.com`.

## Reading chats and messages

WhatsApp Web ships its own module system, and `window.require` returns internal modules
by name. `WAWebCollections` holds the chats and messages the app has in memory:

```js
import { chromium } from 'playwright-core';

const browser = await chromium.connectOverCDP('http://127.0.0.1:9229');
const page = browser.contexts().flatMap(c => c.pages())
  .find(p => p.url().startsWith('https://web.whatsapp.com'));

const { chats, msgs } = await page.evaluate(() => {
  const C = window.require('WAWebCollections');
  return {
    chats: C.Chat.getModelsArray().map(c => ({ jid: c.id._serialized, name: c.formattedTitle })),
    msgs: C.Msg.getModelsArray().map(m => ({
      id: m.id.toString(), chat: m.id.remote._serialized, t: m.t,
      me: !!m.id.fromMe, type: m.type, text: m.type === 'chat' ? m.body : (m.caption || ''),
    })),
  };
});
await browser.close();  // disconnects; the app keeps running
```

`m.t` is a Unix timestamp in seconds. `m.type` is a string here (`chat`, `image`, `ptt`
for a voice note, `document`, `revoked` for a deleted message). The Mac database uses integers for the
same types.

These are internal names, and any update can change them. On this build the message key
had no `_serialized` property, which is why the id comes from `toString()`.

## Keeping messages the app drops from memory

The app holds only recent messages in memory, so one read is never the whole history. The
script keeps every message it has ever seen in a JSON store and writes its CSV output from
that store, so the history only grows. Each chat and message gets an integer id the first
time it appears, which keeps the output in the same shape as on the Mac, where ids come
from the database's row ids.

This caused a problem on the first day. Searching or scrolling in the app loads old
messages into memory, and the next run sees them for the first time. With ids handed out
in first-seen order they looked new, and a search for one name produced alerts for 22
messages from weeks earlier. Now a message that turns up for the first time with a
timestamp more than ten minutes older than the newest message in the store gets a negative
id. It fills in history without crossing the watermark that decides what counts as new.

## Detecting a lost connection

If the laptop loses its connection, the app still holds the messages it already had, and
every read succeeds with nothing new in it, as on an afternoon when nobody writes. The
script reads the connection state on every run:

```js
window.require('WAWebSocketModel').Socket.state  // 'CONNECTED' when it is live
```

If it is anything other than `CONNECTED`, the script saves what it read and then exits
with an error, so the scheduler reports a failure.

## Downloading attachments

Voice notes and documents are encrypted blobs on WhatsApp's media servers, and the message
model in memory carries the key and hashes needed to fetch and decrypt them. The app's
`WAWebDownloadManager` does both. The script calls it with the fields from the message,
base64-encodes the bytes inside the page, and writes them to disk outside it:

```js
const buf = await window.require('WAWebDownloadManager').downloadManager.downloadAndMaybeDecrypt({
  directPath: m.directPath, encFilehash: m.encFilehash, filehash: m.filehash,
  mediaKey: m.mediaKey, mediaKeyTimestamp: m.mediaKeyTimestamp,
  type: m.type, mimetype: m.mimetype, signal: new AbortController().signal,
  downloadQpl: noop,
});
```

`downloadQpl` is a performance logger the app passes in from elsewhere, and the function
expects one. A `Proxy` that returns itself for every property access and every call
stands in for it:

```js
const noop = new Proxy(function () {}, {
  get: (t, k) => k === 'then' ? undefined : noop,
  apply: () => noop,
});
```

Some of these calls are awaited, which is why `then` returns `undefined`. Without that,
`noop.then` would be a function, `await` would treat the Proxy as a promise and call
`then`, and nothing would ever resolve it.

The script downloads at most 15 files a run, newest first, skips anything over 25 MB, and
gives up on a file after three failed attempts.

## Security

Anything that can reach 127.0.0.1:9229 can run code inside WhatsApp with the same power
this script has, including sending messages as you. The port listens only on localhost,
and `--remote-allow-origins` limits which web pages may open a DevTools connection, but
malware already running on the machine would not need a web page. I would only run this
on a machine that runs nothing else, under an account nobody else uses.

## Keeping it running

The environment variable must be set before WhatsApp
starts, so the setup script adds WhatsApp to startup after setting it. The laptop must not
sleep, and WhatsApp must stay linked to the phone. If it is logged out, the script fails
on the next run with "WhatsApp has no chats loaded".