Detecting automatic Ajax requests that update Gmail inbox without user interaction

I’m building a userscript for Gmail and need help with something tricky. I can easily detect when users manually click the inbox or refresh buttons, but Gmail also updates the inbox automatically in the background to show new emails. These automatic updates happen through Ajax calls that I can’t seem to catch. Is there a way to monitor or intercept these background Ajax requests using JavaScript? I’ve tried various event listeners but none seem to work for the automatic refreshes.

You’re hitting Gmail’s service worker architecture - it handles background sync separately from normal Ajax patterns. I ran into the same thing building inbox monitoring features. Gmail uses a complex messaging system between frames and workers that completely bypasses standard request interception. Don’t try hooking into the network layer. Instead, override native fetch and XMLHttpRequest prototypes before Gmail loads, or use periodic polling with hash comparison of inbox content. I also had luck monitoring the browser’s Performance API entries for network requests, but you’ll need to filter carefully for Gmail-specific calls. The DOM observation method others mentioned works well, but combining it with network monitoring gives you better context on what triggered each update.

Had this exact problem last year building a Gmail extension. Gmail’s auto-updates don’t trigger normal Ajax listeners because they use Google’s internal APIs that skip XMLHttpRequest monitoring entirely. MutationObserver saved me - forget trying to catch the requests and just watch for DOM changes in the inbox container. Set it up to monitor the main email list for childList changes and attribute modifications. It’ll catch new emails and updates whether they’re from manual refresh or background sync. Works consistently for both user actions and automatic updates, which sounds like exactly what you’re after.

Hook into window.fetch and XMLHttpRequest.prototype.open before Gmail loads - put this in your userscript header. Since Gmail loads async, you’ll catch those background calls if you’re early enough. Check the network tab in devtools to see which endpoints Gmail actually hits, then filter for those URLs in your interceptor. This worked for my Gmail notification script last month, but Gmail changes things constantly so it might break later.