Fix comment style overwrite

This commit is contained in:
ekzyis 2022-12-25 04:39:14 +01:00
parent 4437416d96
commit 11dc7f9c69
1 changed files with 18 additions and 5 deletions

View File

@ -47,12 +47,25 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const log = (msg) => console.log(`sn-translator:`, msg);
function createButton(content) {
function createButton(textNode) {
const btn = document.createElement('button');
btn.innerText = 'translate';
btn.onclick = async (e) => {
const t = await translate(content.innerText, 'auto', 'en').catch(console.error);
if (t) content.innerText = t;
const hasTranslation = !!textNode.querySelector('hr');
if (hasTranslation) return;
const t = await translate(textNode.innerText, 'auto', 'en').catch(console.error);
if (t) {
const divider = document.createElement('hr');
textNode.appendChild(divider);
const em = document.createElement('em');
const p = document.createElement('p');
p.innerText = 'Translation:';
const p2 = document.createElement('p');
p2.innerText = t;
em.appendChild(p);
em.appendChild(p2);
textNode.appendChild(em);
}
};
btn.classList.add('translate');
return btn;
@ -66,8 +79,8 @@ function addButtons() {
log(`Adding translate button to every comment ...`);
for (const comment of comments) {
const topBar = comment.querySelector('.item_other__qNlji');
const content = comment.querySelector('.comment_text__nHI0E');
const btn = createButton(content);
const textNode = comment.querySelector('.comment_text__nHI0E > .text_text__fuZIZ');
const btn = createButton(textNode);
topBar.appendChild(btn);
}
log(`Done`);