1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
(async () => { const CONFIG = { targetPrivacyText: "仅自己可见", totalCountPattern: /全部\s*(\d+)/, maxIdleRounds: 5, modalTimeoutMs: 20000, optionTimeoutMs: 10000, scrollWaitMs: 1800, postConfirmWaitMs: 500, };
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const waitFor = async (fn, timeoutMs, errorMessage) => { const start = Date.now(); while (Date.now() - start < timeoutMs) { const value = fn(); if (value) return value; await sleep(100); } throw new Error(errorMessage); };
const getTotalCount = () => { const match = document.body.innerText.match(CONFIG.totalCountPattern); return match ? Number(match[1]) : null; };
const getCards = () => [...document.querySelectorAll(".note-card")]; const getScrollContainer = () => document.querySelector(".content") || document.scrollingElement;
const getNoteId = (card) => { const raw = card.getAttribute("data-impression"); if (!raw) return null; try { return JSON.parse(raw)?.noteTarget?.value?.noteId ?? null; } catch { return null; } };
const isAlreadyPrivate = (card) => card.querySelector(".permission_msg")?.textContent?.trim() === CONFIG.targetPrivacyText;
const getPrivacyTrigger = () => [...document.querySelectorAll(".d-select *")] .find((el) => { const text = el.textContent?.trim(); return ( [ "公开可见", "仅自己可见", "仅互关好友可见", "部分人可见", "部分人不可见", ].includes(text) && el.closest(".d-select") ); }) ?.closest(".d-select");
const getPrivacyOption = () => [...document.querySelectorAll(".d-options-wrapper .d-grid-item")] .find((el) => el.textContent?.trim() === CONFIG.targetPrivacyText);
const getConfirmButton = () => [...document.querySelectorAll("button")] .find((button) => button.textContent?.trim() === "确定");
const setNotePrivate = async (card) => { const noteId = getNoteId(card); if (!noteId) throw new Error("Missing note id");
if (isAlreadyPrivate(card)) { return { noteId, changed: false }; }
const privacyBtn = card.querySelector(".note-card__actions .note-card__action-btn"); if (!privacyBtn) throw new Error(`Missing privacy button: ${noteId}`);
privacyBtn.click();
await waitFor( () => getConfirmButton(), CONFIG.modalTimeoutMs, `Privacy dialog did not open: ${noteId}` );
const trigger = await waitFor( () => getPrivacyTrigger(), CONFIG.optionTimeoutMs, `Privacy selector missing: ${noteId}` ); trigger.click();
const option = await waitFor( () => getPrivacyOption(), CONFIG.optionTimeoutMs, `Privacy option missing: ${noteId}` ); option.click();
const confirm = await waitFor( () => getConfirmButton(), CONFIG.optionTimeoutMs, `Confirm button missing: ${noteId}` ); confirm.click();
await waitFor( () => !getConfirmButton(), CONFIG.modalTimeoutMs, `Privacy dialog did not close: ${noteId}` );
await sleep(CONFIG.postConfirmWaitMs); return { noteId, changed: true }; };
const total = getTotalCount(); if (!total) { throw new Error("Could not read note total from the page"); }
const seen = new Set(); let changedCount = 0; let skippedCount = 0; let idleRounds = 0;
console.log(`[xhs-private] target total: ${total}`);
while (seen.size < total && idleRounds < CONFIG.maxIdleRounds) { const cards = getCards(); let roundProgress = 0;
for (const card of cards) { const noteId = getNoteId(card); if (!noteId || seen.has(noteId)) continue;
const result = await setNotePrivate(card); seen.add(noteId); roundProgress += 1;
if (result.changed) { changedCount += 1; console.log(`[xhs-private] changed ${changedCount}: ${noteId}`); } else { skippedCount += 1; console.log(`[xhs-private] already private ${skippedCount}: ${noteId}`); } }
const beforeCount = cards.length; const scrollContainer = getScrollContainer(); if (scrollContainer) { scrollContainer.scrollTop = scrollContainer.scrollHeight; } await sleep(CONFIG.scrollWaitMs); const afterCount = getCards().length;
if (roundProgress === 0 && afterCount === beforeCount) { idleRounds += 1; } else { idleRounds = 0; }
console.log( `[xhs-private] seen=${seen.size}/${total}, loaded=${afterCount}, changed=${changedCount}, skipped=${skippedCount}, idleRounds=${idleRounds}` ); }
const success = seen.size >= total; const summary = { success, total, seen: seen.size, changed: changedCount, skipped: skippedCount, idleRounds, };
console.log("[xhs-private] summary", summary); return summary; })().catch((error) => { console.error("[xhs-private] failed", error); throw error; });
|