From 631f55a95fe8314a5bf960778493761d69c0ee1c Mon Sep 17 00:00:00 2001 From: Omkar Shetkar Date: Sat, 5 Apr 2025 22:36:45 +0530 Subject: [PATCH] Replacing Google with https://dictionaryapi.dev/ --- background/background.js | 104 +++++++++++++++------------------- content_scripts/dictionary.js | 2 +- manifest.json | 4 +- 3 files changed, 49 insertions(+), 61 deletions(-) diff --git a/background/background.js b/background/background.js index f50f220..2b655e2 100644 --- a/background/background.js +++ b/background/background.js @@ -1,87 +1,75 @@ -const GOOGLE_SPEECH_URI = 'https://www.google.com/speech-api/v1/synthesize', - +const DICTIONARY_API_URL = 'https://api.dictionaryapi.dev/api/v2/entries/en/', DEFAULT_HISTORY_SETTING = { enabled: true }; browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - const { word, lang } = request, - url = `https://www.google.com/search?hl=${lang}&q=define+${word}&gl=US`; - - fetch(url, { + const { word } = request, + url = `${DICTIONARY_API_URL}${word}`; + + fetch(url, { method: 'GET', credentials: 'omit' }) - .then((response) => response.text()) - .then((text) => { - const document = new DOMParser().parseFromString(text, 'text/html'), - content = extractMeaning(document, { word, lang }); + .then((response) => { + if (!response.ok) { + throw new Error("Word not found"); + } + return response.json(); + }) + .then((data) => { + const content = extractMeaning(data, word); sendResponse({ content }); content && browser.storage.local.get().then((results) => { let history = results.history || DEFAULT_HISTORY_SETTING; - - history.enabled && saveWord(content) + + history.enabled && saveWord(content); }); }) + .catch((error) => { + console.error("Error fetching definition:", error); + sendResponse({ content: null }); + }); - return true; + return true; // Keep the message channel open for async response. }); -function extractMeaning (document, context) { - if (!document.querySelector("[data-dobid='hdw']")) { return null; } - - var word = document.querySelector("[data-dobid='hdw']").textContent, - definitionDiv = document.querySelector("div[data-dobid='dfn']"), - meaning = ""; - - if (definitionDiv) { - definitionDiv.querySelectorAll("span").forEach(function(span){ - if(!span.querySelector("sup")) - meaning = meaning + span.textContent; - }); +function extractMeaning(data, word) { + if (!data || !Array.isArray(data) || data.length === 0) { + return null; } - meaning = meaning[0].toUpperCase() + meaning.substring(1); + const meanings = data[0].meanings; + let definition = ""; - var audio = document.querySelector("audio[jsname='QInZvb']"), - source = document.querySelector("audio[jsname='QInZvb'] source"), - audioSrc = source && source.getAttribute('src'); + // Extract the first definition from the meanings array + if (meanings && meanings.length > 0) { + const firstMeaning = meanings[0]; + const definitions = firstMeaning.definitions; - if (audioSrc) { - !audioSrc.includes("http") && (audioSrc = audioSrc.replace("//", "https://")); + if (definitions && definitions.length > 0) { + definition = definitions[0].definition; + } } - else if (audio) { - let exactWord = word.replace(/ยท/g, ''), // We do not want syllable seperator to be present. - - queryString = new URLSearchParams({ - text: exactWord, - enc: 'mpeg', - lang: context.lang, - speed: '0.4', - client: 'lr-language-tts', - use_google_only_voices: 1 - }).toString(); - audioSrc = `${GOOGLE_SPEECH_URI}?${queryString}`; - } + const phonetic = data[0].phonetics?.[0]?.text || null; + const audioSrc = data[0].phonetics?.[0]?.audio || null; - return { word: word, meaning: meaning, audioSrc: audioSrc }; -}; + return { word, meaning: definition, phonetic, audioSrc }; +} -function saveWord (content) { +function saveWord(content) { let word = content.word, - meaning = content.meaning, - - storageItem = browser.storage.local.get('definitions'); + meaning = content.meaning; - storageItem.then((results) => { - let definitions = results.definitions || {}; + browser.storage.local.get('definitions').then((results) => { + let definitions = results.definitions || {}; - definitions[word] = meaning; - browser.storage.local.set({ - definitions - }); - }) -} \ No newline at end of file + definitions[word] = meaning; + browser.storage.local.set({ + definitions + }); + }); +} diff --git a/content_scripts/dictionary.js b/content_scripts/dictionary.js index fbb2595..b338972 100644 --- a/content_scripts/dictionary.js +++ b/content_scripts/dictionary.js @@ -108,7 +108,7 @@ audio.style.display = "none"; var moreInfo =document.createElement("a"); - moreInfo.href = `https://www.google.com/search?hl=${LANGUAGE}&q=define+${info.word}`; + moreInfo.href = `https://duckduckgo.com/?q=define+${info.word}`; moreInfo.style = "float: right; text-decoration: none;" moreInfo.target = "_blank"; diff --git a/manifest.json b/manifest.json index afb3465..e75a965 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "manifest_version": 2, "name": "Dictionary Anywhere", - "version": "1.1.0", + "version": "1.1.1", "description": "View definitions easily as you browse the web. Double-click any word to view its definition in a small pop-up bubble.", @@ -40,6 +40,6 @@ "permissions": [ "storage", - "https://www.google.com/" + "https://api.dictionaryapi.dev/api/v2/entries/en/" ] }