diff --git a/popup/popup.js b/popup/popup.js index 2e8a48b..0d76367 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -1,35 +1,39 @@ - -let query = {active: true, currentWindow: true}; +let query = { active: true, currentWindow: true }; chrome.tabs.query(query, gotTabs); function gotTabs(tabs) { - let msg = { - txt: "hello from popup" - } - - chrome.tabs.sendMessage(tabs[0].id, msg, function(response){ - if(response.swor === "error"){ - document.getElementById('word').innerHTML = "Please select a word!"; - } - else{ - dictionary(response.swor) - } - }); + let msg = { + txt: "hello from popup", + }; - } + chrome.tabs.sendMessage(tabs[0].id, msg, function (response) { + if (response.swor === "error") { + document.getElementById("word").innerHTML = "Please select a word!"; + } else { + let swo = response.swor; + swo = swo.replace(/[^a-zA-Z ]/g, ""); + dictionary(swo); + } + }); +} -async function dictionary(query){ - let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${query}`; - let response = await fetch(url); - let json = await response.json(); - if(json && !json.title){ - document.getElementById('word').innerHTML = json[0].word; - document.getElementById('phonetic').innerHTML = `${json[0].phonetic ? json[0].phonetic : ""} (${json[0].meanings[0].partOfSpeech})`; - document.getElementById('definition').innerHTML = json[0].meanings[0].definitions[0].definition; - if(json[0].meanings[0].definitions[0].example){ - document.getElementById('example').innerHTML = `Example: ${json[0].meanings[0].definitions[0].example}`; - } - }else if(json.title){ - document.getElementById('error').innerHTML = "⚠ " + json.title; +async function dictionary(query) { + let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${query}`; + let response = await fetch(url); + let json = await response.json(); + if (json && !json.title) { + document.getElementById("word").innerHTML = json[0].word; + document.getElementById("phonetic").innerHTML = `${ + json[0].phonetic ? json[0].phonetic : "" + } (${json[0].meanings[0].partOfSpeech})`; + document.getElementById("definition").innerHTML = + json[0].meanings[0].definitions[0].definition; + if (json[0].meanings[0].definitions[0].example) { + document.getElementById( + "example" + ).innerHTML = `Example: ${json[0].meanings[0].definitions[0].example}`; } -} \ No newline at end of file + } else if (json.title) { + document.getElementById("error").innerHTML = "⚠ " + json.title; + } +}