Put button creation code into function

This commit is contained in:
ekzyis 2022-12-25 03:06:04 +01:00
parent f079c352db
commit f2fc06fe01
1 changed files with 11 additions and 6 deletions

View File

@ -46,6 +46,16 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const log = (msg) => console.log(`sn-translator:`, msg); const log = (msg) => console.log(`sn-translator:`, msg);
function createButton(content) {
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;
};
return btn;
}
function addButtons() { function addButtons() {
const commentSection = document.querySelector('.item_comments__cN57K'); const commentSection = document.querySelector('.item_comments__cN57K');
if (!commentSection) return; if (!commentSection) return;
@ -57,12 +67,7 @@ function addButtons() {
const content = comment.querySelector('.comment_text__nHI0E'); const content = comment.querySelector('.comment_text__nHI0E');
const padding = document.createElement('span'); const padding = document.createElement('span');
padding.innerText = ' '; padding.innerText = ' ';
const btn = document.createElement('button'); const btn = createButton(content);
btn.innerText = 'Translate';
btn.onclick = async (e) => {
const t = await translate(content.innerText, 'auto', 'en').catch(console.error);
if (t) content.innerText = t;
};
topBar.appendChild(padding); topBar.appendChild(padding);
topBar.appendChild(btn); topBar.appendChild(btn);
} }