From 6de35ea4ca37eec5f6daf1374e4f38b6953e24bd Mon Sep 17 00:00:00 2001 From: Sandeep Suman Date: Sat, 22 Jul 2023 22:03:49 +0530 Subject: [PATCH] v1.2.1 handle urls --- content.js | 3 +++ manifest.json | 9 ++++++--- popup/popup.js | 24 +++++++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/content.js b/content.js index 2ff5641..3b7107f 100644 --- a/content.js +++ b/content.js @@ -6,6 +6,7 @@ function handleSelection() { selectedText = window.getSelection().toString().replace(/\s/g, ""); } +// receive the message from popup. chrome.runtime.onMessage.addListener(gotMessage); function gotMessage(message, sender, sendResponse) { @@ -13,5 +14,7 @@ function gotMessage(message, sender, sendResponse) { selectedText && selectedText.length > 0 ? selectedText : "_TextNotSelected_"; + + // send the selected text to the popup.js as a response to the message. sendResponse({ swor: msg }); } diff --git a/manifest.json b/manifest.json index ef443ad..d7f264d 100644 --- a/manifest.json +++ b/manifest.json @@ -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", @@ -25,5 +25,8 @@ ], "js": ["content.js"] } + ], + "permissions": [ + "activeTab" ] } \ No newline at end of file diff --git a/popup/popup.js b/popup/popup.js index 5ee5a09..0332036 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -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, ""); @@ -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);