fix(baoyu-post-to-wechat): fix placeholder matching to avoid WECHATIMGPH_1 matching WECHATIMGPH_10

This commit is contained in:
Jim Liu 宝玉
2026-02-03 11:06:41 -06:00
parent b0554f8d0c
commit b9e48d9483
@@ -273,22 +273,31 @@ async function selectAndReplacePlaceholder(session: ChromeSession, placeholder:
const editor = document.querySelector('.ProseMirror'); const editor = document.querySelector('.ProseMirror');
if (!editor) return false; if (!editor) return false;
const placeholder = ${JSON.stringify(placeholder)};
const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT, null, false); const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT, null, false);
let node; let node;
while ((node = walker.nextNode())) { while ((node = walker.nextNode())) {
const text = node.textContent || ''; const text = node.textContent || '';
const idx = text.indexOf(${JSON.stringify(placeholder)}); let searchStart = 0;
if (idx !== -1) { let idx;
node.parentElement.scrollIntoView({ behavior: 'smooth', block: 'center' }); // Search for exact match (not prefix of longer placeholder like XIMGPH_1 in XIMGPH_10)
while ((idx = text.indexOf(placeholder, searchStart)) !== -1) {
const afterIdx = idx + placeholder.length;
const charAfter = text[afterIdx];
// Exact match if next char is not a digit
if (charAfter === undefined || !/\\d/.test(charAfter)) {
node.parentElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
const range = document.createRange(); const range = document.createRange();
range.setStart(node, idx); range.setStart(node, idx);
range.setEnd(node, idx + ${placeholder.length}); range.setEnd(node, idx + placeholder.length);
const sel = window.getSelection(); const sel = window.getSelection();
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange(range); sel.addRange(range);
return true; return true;
}
searchStart = afterIdx;
} }
} }
return false; return false;