Skip to content

Commit

Permalink
v1.2.1 handle urls
Browse files Browse the repository at this point in the history
  • Loading branch information
SandeepKrSuman committed Jul 22, 2023
1 parent ad0934e commit 6de35ea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
3 changes: 3 additions & 0 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ function handleSelection() {
selectedText = window.getSelection().toString().replace(/\s/g, "");
}

// receive the message from popup.
chrome.runtime.onMessage.addListener(gotMessage);

function gotMessage(message, sender, sendResponse) {
let msg =
selectedText && selectedText.length > 0
? selectedText
: "_TextNotSelected_";

// send the selected text to the popup.js as a response to the message.
sendResponse({ swor: msg });
}
9 changes: 6 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"manifest_version": 3,
"name": "Dictionary",
"version": "1.2.0",
"version": "1.2.1",
"action": {
"default_icon": {
"16": "assets/icon16.png",
"24": "assets/icon24.png",
"32": "assets/icon32.png"
},
"default_title": "Word Lookup",
"default_title": "Dictionary",
"default_popup": "popup/popup.html"
},
"description": "Select a word on the webpage and open popup to look up in the dictionary",
"description": "Select a word on the webpage and open popup to instantly look up in the dictionary",
"icons": {
"16": "assets/icon16.png",
"48": "assets/icon48.png",
Expand All @@ -25,5 +25,8 @@
],
"js": ["content.js"]
}
],
"permissions": [
"activeTab"
]
}
24 changes: 21 additions & 3 deletions popup/popup.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
// get the currently active tab in the current window
// and then invoke the callback function gotTabs.
let query = { active: true, currentWindow: true };

chrome.tabs.query(query, gotTabs);

// function to check current url and eliminate offline urls.
function safeUrl(url) {
return url.startsWith("https://") || url.startsWith("http://");
}

// callback function
function gotTabs(tabs) {
// prevent offline urls to run the extension by throwing error.
if (!safeUrl(tabs[0].url)) {
document.getElementById("error").innerHTML = "Oh no!";
document.getElementById("definition").innerHTML = "Unsupported Page.";
return;
}

let msg = {
txt: "hello from popup",
};

// send message to the content script
chrome.tabs.sendMessage(tabs[0].id, msg, function (response) {
if (!response) {
document.getElementById("phonetic").innerHTML =
"Refresh the page and try again.";
} else if (response.swor === "_TextNotSelected_") {
document.getElementById("phonetic").innerHTML = "Welcome!";
document.getElementById("example").innerHTML =
"Please select a word to find its definition.";
} else if (response.swor === "_TextNotSelected_") {
document.getElementById("error").innerHTML = "Please select a word!";
} else {
let swo = response.swor;
swo = swo.replace(/[^a-zA-Z ]/g, "");
Expand All @@ -31,6 +48,7 @@ let wordef,
index = 0,
indlimit;

// function to fetch and show definition on the popup
async function dictionary(query) {
let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${query}`;
let response = await fetch(url);
Expand Down

0 comments on commit 6de35ea

Please sign in to comment.