optimize retrieval
This commit is contained in:
@@ -62,7 +62,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
function hasMeaningfulChildContent(element) {
|
||||
return element.querySelector(
|
||||
'img, table, pre, code, ul, ol, h1, h2, h3, h4, h5, h6, a, hr'
|
||||
'img, table, pre, code, ul, ol, h1, h2, h3, h4, h5, h6, a, hr, .badge'
|
||||
) !== null;
|
||||
}
|
||||
|
||||
@@ -86,68 +86,23 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
});
|
||||
}
|
||||
|
||||
function stripAllThinkContent(container) {
|
||||
const blockSelector = 'p, div, li, blockquote';
|
||||
const thinkSpans = Array.from(container.querySelectorAll('.think'));
|
||||
|
||||
if (thinkSpans.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const handledBlocks = new Set();
|
||||
|
||||
thinkSpans.forEach((span) => {
|
||||
const block = span.closest(blockSelector) || span.parentElement;
|
||||
|
||||
if (!block || handledBlocks.has(block)) {
|
||||
return;
|
||||
}
|
||||
|
||||
handledBlocks.add(block);
|
||||
|
||||
const thinksInBlock = Array.from(block.querySelectorAll('.think'));
|
||||
const lastThinkInBlock = thinksInBlock[thinksInBlock.length - 1];
|
||||
|
||||
if (!lastThinkInBlock) {
|
||||
return;
|
||||
}
|
||||
|
||||
let node = block.firstChild;
|
||||
|
||||
while (node) {
|
||||
const next = node.nextSibling;
|
||||
const isLastThink = node === lastThinkInBlock;
|
||||
|
||||
node.remove();
|
||||
|
||||
if (isLastThink) {
|
||||
break;
|
||||
}
|
||||
|
||||
node = next;
|
||||
}
|
||||
|
||||
while (
|
||||
block.firstChild &&
|
||||
(
|
||||
(block.firstChild.nodeType === Node.TEXT_NODE &&
|
||||
block.firstChild.textContent.trim() === '') ||
|
||||
(block.firstChild.nodeType === Node.ELEMENT_NODE &&
|
||||
block.firstChild.tagName === 'BR')
|
||||
)
|
||||
) {
|
||||
block.firstChild.remove();
|
||||
}
|
||||
function removeThinkSpansOnly(container) {
|
||||
container.querySelectorAll('.think').forEach((span) => {
|
||||
span.remove();
|
||||
});
|
||||
|
||||
cleanupEmptyBlocks(container);
|
||||
}
|
||||
|
||||
function hasNonThinkContent(container) {
|
||||
function cloneWithoutThinkContent(container) {
|
||||
const clone = container.cloneNode(true);
|
||||
|
||||
stripAllThinkContent(clone);
|
||||
clone.querySelectorAll('.think').forEach((span) => span.remove());
|
||||
cleanupEmptyBlocks(clone);
|
||||
return clone;
|
||||
}
|
||||
|
||||
function hasNonThinkContent(container) {
|
||||
const clone = cloneWithoutThinkContent(container);
|
||||
|
||||
if ((clone.textContent || '').trim() !== '') {
|
||||
return true;
|
||||
@@ -156,6 +111,49 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
return hasMeaningfulChildContent(clone);
|
||||
}
|
||||
|
||||
function keepOnlyLastThink(container) {
|
||||
const thinkSpans = Array.from(container.querySelectorAll('.think'));
|
||||
|
||||
if (thinkSpans.length <= 1) {
|
||||
cleanupEmptyBlocks(container);
|
||||
return;
|
||||
}
|
||||
|
||||
const lastThink = thinkSpans[thinkSpans.length - 1];
|
||||
|
||||
thinkSpans.slice(0, -1).forEach((span) => {
|
||||
span.remove();
|
||||
});
|
||||
|
||||
const blockSelector = 'p, div, li, blockquote';
|
||||
const lastBlock = lastThink.closest(blockSelector) || lastThink.parentElement;
|
||||
|
||||
if (lastBlock && lastThink.parentElement === lastBlock) {
|
||||
Array.from(lastBlock.childNodes).forEach((node) => {
|
||||
if (node === lastThink) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
node.nodeType === Node.TEXT_NODE &&
|
||||
node.textContent.trim() === ''
|
||||
) {
|
||||
node.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
node.nodeType === Node.ELEMENT_NODE &&
|
||||
node.tagName === 'BR'
|
||||
) {
|
||||
node.remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
cleanupEmptyBlocks(container);
|
||||
}
|
||||
|
||||
function cleanupThinkSpans(container) {
|
||||
if (!container) {
|
||||
return;
|
||||
@@ -168,54 +166,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
if (hasNonThinkContent(container)) {
|
||||
stripAllThinkContent(container);
|
||||
removeThinkSpansOnly(container);
|
||||
return;
|
||||
}
|
||||
|
||||
if (thinkSpans.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const blockSelector = 'p, div, li, blockquote';
|
||||
const lastThink = thinkSpans[thinkSpans.length - 1];
|
||||
const lastBlock = lastThink.closest(blockSelector) || lastThink.parentElement;
|
||||
|
||||
thinkSpans.slice(0, -1).forEach((span) => {
|
||||
const block = span.closest(blockSelector) || span.parentElement;
|
||||
|
||||
if (block && block !== lastBlock) {
|
||||
block.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
if (block === lastBlock) {
|
||||
span.remove();
|
||||
}
|
||||
});
|
||||
|
||||
if (lastBlock && lastBlock.contains(lastThink)) {
|
||||
let node = lastBlock.firstChild;
|
||||
|
||||
while (node && node !== lastThink) {
|
||||
const next = node.nextSibling;
|
||||
node.remove();
|
||||
node = next;
|
||||
}
|
||||
|
||||
while (
|
||||
lastThink.nextSibling &&
|
||||
(
|
||||
(lastThink.nextSibling.nodeType === Node.TEXT_NODE &&
|
||||
lastThink.nextSibling.textContent.trim() === '') ||
|
||||
(lastThink.nextSibling.nodeType === Node.ELEMENT_NODE &&
|
||||
lastThink.nextSibling.tagName === 'BR')
|
||||
)
|
||||
) {
|
||||
lastThink.nextSibling.remove();
|
||||
}
|
||||
}
|
||||
|
||||
cleanupEmptyBlocks(container);
|
||||
keepOnlyLastThink(container);
|
||||
}
|
||||
|
||||
function renderBubbleContent(bubble, raw) {
|
||||
|
||||
Reference in New Issue
Block a user