Skip to content

Commit

Permalink
filtered out special characters from selected text
Browse files Browse the repository at this point in the history
  • Loading branch information
SandeepKrSuman committed Mar 28, 2022
1 parent 2965ea2 commit f386739
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions popup/popup.js
Original file line number Diff line number Diff line change
@@ -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}`;
}
}
} else if (json.title) {
document.getElementById("error").innerHTML = "⚠ " + json.title;
}
}

0 comments on commit f386739

Please sign in to comment.