From defe6ebc4d56d93d9aebf3a7cab22096fbcbc4d3 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 25 Dec 2022 02:38:03 +0100 Subject: [PATCH] Formatting --- .prettierrc | 7 +++ sn_translator/sn-translator.js | 87 ++++++++++++++++------------------ 2 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..d86fe7a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": false, + "singleQuote": true, + "printWidth": 120 +} \ No newline at end of file diff --git a/sn_translator/sn-translator.js b/sn_translator/sn-translator.js index 0557781..6fff6d2 100644 --- a/sn_translator/sn-translator.js +++ b/sn_translator/sn-translator.js @@ -10,66 +10,63 @@ // ==/UserScript== const headers = { - origin: "https://libretranslate.com", - accept: "*/*", - "accept-language": "de-DE,de;q=0.9,ru-DE;q=0.8,ru;q=0.7,en-US;q=0.6,en;q=0.5", - "sec-ch-ua": - '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"', - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-platform": '"Linux"', - "sec-fetch-dest": "empty", - "sec-fetch-mode": "cors", - "sec-fetch-site": "same-origin", -}; + origin: 'https://libretranslate.com', + accept: '*/*', + 'accept-language': 'de-DE,de;q=0.9,ru-DE;q=0.8,ru;q=0.7,en-US;q=0.6,en;q=0.5', + 'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"Linux"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', +} function translate(text, source, target) { - const formData = new FormData(); - formData.append("q", text); - formData.append("source", source); - formData.append("target", target); + const formData = new FormData() + formData.append('q', text) + formData.append('source', source) + formData.append('target', target) return new Promise((resolve, reject) => { GM_xmlhttpRequest({ - method: "POST", - url: "https://libretranslate.com/translate", + method: 'POST', + url: 'https://libretranslate.com/translate', data: formData, headers, synchronous: true, onload: function (res) { - const body = JSON.parse(res.responseText); - if (res.status !== 200) return reject(body); - return resolve(body.translatedText); + const body = JSON.parse(res.responseText) + if (res.status !== 200) return reject(body) + return resolve(body.translatedText) }, - }); - }); + }) + }) } -const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) function addButtons() { - console.log("sn-translator: Adding button to every comment ..."); - const commentSection = document.querySelector(".item_comments__cN57K"); - const comments = commentSection.querySelectorAll(".comment_comment__5uvl3"); - const commentNodes = commentSection.querySelectorAll(".comment_text__nHI0E"); + console.log('sn-translator: Adding button to every comment ...') + const commentSection = document.querySelector('.item_comments__cN57K') + const comments = commentSection.querySelectorAll('.comment_comment__5uvl3') + const commentNodes = commentSection.querySelectorAll('.comment_text__nHI0E') for (const comment of comments) { - const topBar = comment.querySelector(".item_other__qNlji"); - const content = comment.querySelector(".comment_text__nHI0E"); - const padding = document.createElement("span"); - padding.innerText = " "; - const btn = document.createElement("button"); - btn.innerText = "Translate"; + const topBar = comment.querySelector('.item_other__qNlji') + const content = comment.querySelector('.comment_text__nHI0E') + const padding = document.createElement('span') + padding.innerText = ' ' + 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; - }; - topBar.appendChild(padding); - topBar.appendChild(btn); + const t = await translate(content.innerText, 'auto', 'en').catch(console.error) + if (t) content.innerText = t + } + topBar.appendChild(padding) + topBar.appendChild(btn) } - console.log("Done"); + console.log('Done') } -(async function () { - await sleep(1000); - addButtons(); -})(); +;(async function () { + await sleep(1000) + addButtons() +})()