mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-25 03:19:48 +08:00
fix(baoyu-post-to-wechat): fix placeholder matching to avoid WECHATIMGPH_1 matching WECHATIMGPH_10
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user