Как получить весь текст элемента точно так, как он отображается в DevTools?Javascript

Форум по Javascript
Ответить
Anonymous
 Как получить весь текст элемента точно так, как он отображается в DevTools?

Сообщение Anonymous »

Если есть дубликат, я не могу его найти.
Я пытаюсь скопировать все сообщения между мной и ChatGPT-4o в текстовый файл в инструментах разработчика Firefox.< /p>
Вот что у меня получилось:

Код: Выделить всё

const prompts = $x("//div[@class='whitespace-pre-wrap']");
const replies = $x("//div[contains(@class, 'markdown prose')]");
const messages = [];

for (let i = 0; i < prompts.length; i++) {
messages.push(["Ξένη Γήινος", prompts[i].textContent]);
messages.push(["ChatGPT", replies[i].textContent]);
}
console.log(JSON.stringify(messages))
< /code>
Я затем расширяю вывод и щелкните правой кнопкой мыши, чтобы скопировать сообщение. Я хочу, чтобы текст был точно таким, как отображается на экране, вывод должен быть как если бы я копировал текст. .TextContent 
не сохраняет формат.

Код: Выделить всё

You're correct about the number of characters in your earlier sentence; I miscounted the total.  I appreciate your patience.

Regarding the Python code you've provided:

pythonCopyEditprint(sum(i.isalpha() for i in "How many Latin characters are there in this sentence?"))
This code will count only the alphabetic characters in the string, ignoring punctuation and spaces.

Let's break it down:
  • "How many Latin characters are there in this sentence?"Letters: H, o, w, m, a, n, y, L, a, t, i, n, c, h, a, r, a, c, t, e, r, s, a, r, e, t, h, e, r, i, n, t, h, i, s, s, e, n, t, e, n, c, e
The total count of alphabetic characters (Latin characters) in this sentence is 44.

Thus, running the code would output 44.


Я хочу, чтобы результат был таким:

Код: Выделить всё

You're correct about the number of characters in your earlier sentence; I miscounted the total.  I appreciate your patience.

Regarding the Python code you've provided:

print(sum(i.isalpha() for i in "How many Latin characters are there in this sentence?"))

This code will count only the alphabetic characters in the string, ignoring punctuation and spaces.

Let's break it down:

"How many Latin characters are there in this sentence?"
Letters: H, o, w, m, a, n, y, L, a, t, i, n, c, h, a, r, a, c, t, e, r, s, a, r, e, t, h, e, r, i, n, t, h, i, s, s, e, n, t, e, n, c, e

The total count of alphabetic characters (Latin characters) in this sentence is 44.

Thus, running the code would output 44.
Вывод точно такой же, как результат копирования (потому что я копировал его).
Результат .TextContent : < /p>

Код: Выделить всё

`You're correct about the number of characters in your earlier sentence; I miscounted the total. I appreciate your patience.Regarding the Python code you've provided:pythonCopyEditprint(sum(i.isalpha() for i in "How many Latin characters are there in this sentence?"))
This code will count only the alphabetic characters in the string, ignoring punctuation and spaces.Let's break it down:"How many Latin characters are there in this sentence?"Letters: H, o, w, m, a, n, y, L, a, t, i, n, c, h, a, r, a, c, t, e, r, s, a, r, e, t, h, e, r, i, n, t, h, i, s, s, e, n, t, e, n, c, eThe total count of alphabetic characters (Latin characters) in this sentence is 44.Thus, running the code would output 44.`
< /code>
Формат сломан. : < /p>
function flatten(x) {
let text, children;
children = x.children;
if (children.length) {
text = flatten(children[0])
for (let i = 0; i < children.length; i++) {
text += "\n" + flatten(children[i]);
}
return text;
}
return x.textContent;
}
Проще говоря, вывод — мусор.
Как скопировать весь текст элемента, включая все подэлементы (и их подэлементы. ..) с сохранением форматирования?

Подробнее здесь: https://stackoverflow.com/questions/793 ... n-devtools
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»