Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add several meanings result #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .web-extension-id
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file was created by https://github.com/mozilla/web-ext
# Your auto-generated extension ID for addons.mozilla.org is:
[email protected]
93 changes: 75 additions & 18 deletions background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ const GOOGLE_SPEECH_URI = 'https://www.google.com/speech-api/v1/synthesize',
enabled: true
};

function getContentOfSiteCrossOrigin (url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.responseText);
} else {
reject(xhr.statusText);
}
};
xhr.onerror = () => {
reject(xhr.statusText);
};
xhr.send();
});
}

browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
const { word, lang } = request,
url = `https://www.google.com/search?hl=${lang}&q=define+${word}&gl=US`;
url = makeLink(word, lang);

fetch(url, {
method: 'GET',
credentials: 'omit'
})
method: 'GET',
credentials: 'omit'
})
.then((response) => response.text())
.then((text) => {
const document = new DOMParser().parseFromString(text, 'text/html'),
Expand All @@ -29,21 +47,60 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
return true;
});

function extractMeaning (document, context) {
function extractMeaningFromGoogle (document) {
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;
definitionDivNodeList = document.querySelectorAll("div[data-dobid='dfn']"),
meaningArray = [];

if(definitionDivNodeList) {
definitionDivNodeList.forEach((definitionDiv) => {
if (definitionDiv) {
var meaning = "";
definitionDiv.querySelectorAll("span").forEach( function(span) {
if(!span.querySelector("sup")) {
meaning = meaning + span.textContent;
}
});
meaningArray.push(meaning);
}
});
}

for(var i = 0; i < meaningArray.length; i++) {
meaningArray[i] = meaningArray[i][0].toUpperCase() + meaningArray[i].substring(1);
}
return { word: word, meaningArray: meaningArray };
}

meaning = meaning[0].toUpperCase() + meaning.substring(1);
function extractMeaningFromDoxonline (document) {
var targetSpan = document.querySelector(".def");
if (!targetSpan) { return null; }

var word = targetSpan.querySelector("b").textContent.slice(0,-1),
meaningArray = [targetSpan.innerHTML];

if(meaningArray[0].length > 1000) {
meaningArray[0] = meaningArray[0].substr(0, 1000) + "...";
}

return { word: word, meaningArray: meaningArray };
}


function extractMeaning (document, context) {
var word,
meaningArray,
wordAndMeaningArray;

if (context.lang === 'ro') {
wordAndMeaningArray = extractMeaningFromDoxonline(document);
} else {
wordAndMeaningArray = extractMeaningFromGoogle(document);
}
word = wordAndMeaningArray.word;
meaningArray = wordAndMeaningArray.meaningArray;

var audio = document.querySelector("audio[jsname='QInZvb']"),
source = document.querySelector("audio[jsname='QInZvb'] source"),
Expand All @@ -67,21 +124,21 @@ function extractMeaning (document, context) {
audioSrc = `${GOOGLE_SPEECH_URI}?${queryString}`;
}

return { word: word, meaning: meaning, audioSrc: audioSrc };
return { word: word, meaningArray: meaningArray, audioSrc: audioSrc };
};

function saveWord (content) {
let word = content.word,
meaning = content.meaning,
meaningArray = content.meaningArray,

storageItem = browser.storage.local.get('definitions');

storageItem.then((results) => {
let definitions = results.definitions || {};

definitions[word] = meaning;
definitions[word] = meaningArray.join(' ');
browser.storage.local.set({
definitions
});
})
}
}
15 changes: 15 additions & 0 deletions common/dictionary-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function makeDexonlineLink(word) {
return `https://dexonline.ro/definitie/${word}`;
}

function makeGoogleLink(word, language) {
return `https://www.google.com/search?hl=${language}&q=define+${word}`;
}

function makeLink(word, language) {
if(language === 'ro') {
return makeDexonlineLink(word);
} else {
return makeGoogleLink(word, language);
}
}
Loading