Skip to content

Commit

Permalink
refactor: rename func
Browse files Browse the repository at this point in the history
  • Loading branch information
PleahMaCaka committed Feb 21, 2023
1 parent fb9c94f commit 8c0ff7c
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/content/translator/AddTranslator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@ import { postData } from "../../background/Translator";
import { TranslatorConfig } from "./TranslatorConfig"

function addTranslator() {
// opened options
const optionsContent = document.querySelector(".options-content")
optionsContent?.setAttribute("style", "top: -50px")

// toggle button
const toggleTranslator = document.createElement("a")
toggleTranslator.id = "option_toggle_translator"
toggleTranslator.text = "Toggle Translator"

// button icon
const translatorIcon =
"https://cdn.discordapp.com/attachments/1072301775606001744/1072301837304221716/translator.png"
toggleTranslator.innerHTML = `<img src="${translatorIcon}" width="20" height="20" alt="translator">Toggle Translator`

// add toggle button in options
optionsContent?.appendChild(toggleTranslator)

// toggle to change config
document
.getElementById("option_toggle_translator")
?.addEventListener("click", () => {
TranslatorConfig.enabled = !TranslatorConfig.enabled
console.log("TavernAI Translator: " + TranslatorConfig.enabled)
if (TranslatorConfig.enabled) enableTranslatorTextarea()
else disableTranslatorTextarea()
if (TranslatorConfig.enabled) enableTranslator()
else disableTranslator()
})
}

function enableTranslatorTextarea() {
function enableTranslator() {
// sendTextArea : existing message box (right)
// translatorTextArea : new message box for translate (left)
const sendTextarea = document.querySelector("#send_textarea")
const translatorTextarea = document.createElement("textarea")

// sendForm : existing form
const sendForm = document.getElementById("send_form")

sendTextarea?.setAttribute("style", "width: 50%; height: 89px; margin-left: 0;")
translatorTextarea.setAttribute("placeholder", "Message to translate");
translatorTextarea.setAttribute("placeholder", "Message to translate...");
translatorTextarea.id = "translator_textarea"
translatorTextarea.setAttribute("style", "width: 50%; height: 89px; margin-left: 0;")
sendTextarea?.parentNode?.insertBefore(translatorTextarea, sendTextarea)
Expand Down Expand Up @@ -59,7 +68,7 @@ function enableTranslatorTextarea() {
/**
* revert all css changes
*/
function disableTranslatorTextarea() {
function disableTranslator() {
const sendTextarea = document.querySelector("#send_textarea")
const translatorTextarea = document.querySelector("#translator_textarea")
const sendForm = document.getElementById("send_form")
Expand All @@ -72,6 +81,6 @@ function disableTranslatorTextarea() {
}

export function initTranslator() {
if (TranslatorConfig.enabled) enableTranslatorTextarea()
if (TranslatorConfig.enabled) enableTranslator()
addTranslator()
}

0 comments on commit 8c0ff7c

Please sign in to comment.