Skip to content

Commit 85603ab

Browse files
committed
fix(xiaohongshu): address copilot review on creator-note-detail hook
Two polish items from the Copilot review on #1732: - Buffer reset: window.__xhsCapture is now cleared on every install call so stale captures from a previous run on the same tab cannot leak into the current navigation's harvest. The wrapper-install guard moves to a separate __xhsCaptureInstalled flag so the fetch/XHR monkey-patches themselves are still installed exactly once per page lifetime. - XHR static constants: HookedXHR now copies the readyState constants (UNSENT / OPENED / HEADERS_RECEIVED / LOADING / DONE) from the original constructor so dashboard code that reads XMLHttpRequest.DONE etc against the constructor keeps working.
1 parent f636191 commit 85603ab

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

clis/xiaohongshu/creator-note-detail.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,11 @@ const CAPTURE_POLL_INTERVAL_S = 0.5;
255255
// since a direct fetch() from page.evaluate bypasses the x-s signing and gets 406.
256256
async function installXhsFetchCaptureHook(page) {
257257
await page.evaluate(`(() => {
258-
if (window.__xhsCapture) return;
258+
// Reset the buffer every call so stale captures from a previous run on
259+
// the same tab cannot leak into the current navigation's harvest.
259260
window.__xhsCapture = {};
261+
if (window.__xhsCaptureInstalled) return;
262+
window.__xhsCaptureInstalled = true;
260263
const origFetch = window.fetch;
261264
window.fetch = async function(...args) {
262265
const resp = await origFetch.apply(this, args);
@@ -289,6 +292,11 @@ async function installXhsFetchCaptureHook(page) {
289292
return xhr;
290293
}
291294
HookedXHR.prototype = OrigXHR.prototype;
295+
// Preserve readyState constants (UNSENT / OPENED / HEADERS_RECEIVED / LOADING / DONE)
296+
// since dashboard code may read XMLHttpRequest.DONE etc against the constructor.
297+
for (const key of ['UNSENT', 'OPENED', 'HEADERS_RECEIVED', 'LOADING', 'DONE']) {
298+
if (key in OrigXHR) HookedXHR[key] = OrigXHR[key];
299+
}
292300
window.XMLHttpRequest = HookedXHR;
293301
})()`);
294302
}

0 commit comments

Comments
 (0)