Я пытаюсь загрузить свой HTML, пока мой JS-код не переведет его с помощью PHP. Затем я хотел бы получить содержимое этого переведенного HTML. Я провел много исследований, но не могу этого сделать. Вот код моего HTML:
Translate Page
This example displays a simple translate button, with no text.
This example displays a simple translate button, with no text.
This is a paragraph.
A particle is kept at rest at the top of a sphere of diameter \(42 m\). When disturbed slightly,
it slides down. At what height \( h \) from the bottom, the particle will leave the sphere
(a) \( 14m \)
(b) \( 16m \)
(c) \( 35m \)
(d) \( 70m \)
function translateContent() {
const allTextNodes = getAllTextNodes(document.body);
const textContent = allTextNodes.map(node => node.textContent.trim()).join(' ');
// Translate content to French using Google Translate
fetch('https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=fr&dt=t&q=' + encodeURIComponent(textContent))
.then(response => response.json())
.then(data => {
const translatedText = data[0].map(sentence => sentence[0]);
replaceTextNodes(allTextNodes, translatedText.join(' '));
})
.catch(error => console.error('Error translating content:', error));
}
function getAllTextNodes(element) {
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
const textNodes = [];
while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}
return textNodes;
}
function replaceTextNodes(nodes, newText) {
let currentIndex = 0;
nodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
const originalText = node.textContent.trim();
const newTextSlice = newText.slice(currentIndex, currentIndex + originalText.length);
node.textContent = newTextSlice;
currentIndex += originalText.length;
}
});
}
// Trigger translation on page load
window.onload = translateContent;
Я пытаюсь загрузить свой HTML, пока мой JS-код не переведет его с помощью PHP. Затем я хотел бы получить содержимое этого переведенного HTML. Я провел много исследований, но не могу этого сделать. [b]Вот код моего HTML:[/b] [code]
Translate Page
This example displays a simple translate button, with no text. This example displays a simple translate button, with no text.
This is a paragraph.
A particle is kept at rest at the top of a sphere of diameter \(42 m\). When disturbed slightly, it slides down. At what height \( h \) from the bottom, the particle will leave the sphere