commit 22ced0c4b4075e29851184a6878436f0debe4573 Author: OverPoweredDev Date: Thu Jun 24 16:27:11 2021 +0530 got detectLanguage to run on the hover-on thing diff --git a/src/background/storage.js b/src/background/storage.js index 6c51664..0b276b8 100644 --- a/src/background/storage.js +++ b/src/background/storage.js @@ -216,6 +216,8 @@ function getLanguageCodeMap(){ } async function detectLanguage(text){ + text = text.replace(/\W/g, ' ') + let url = new URL(getDetectLanguageEndpoint()); let params = {q: text}; url.search = new URLSearchParams(params).toString(); diff --git a/src/content/content.js b/src/content/content.js index 56f2383..8836db6 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -1,12 +1,21 @@ + chrome.runtime.sendMessage({method: "getSettings"}, function (response) { - let enabledList = response.settings; - addHoverElements(enabledList); + let settings = response.settings; + addHoverElements(settings); }); -function addHoverElements(settings) { +async function addHoverElements(settings) { let enabledList = settings.enabledWebsites; if (enabledList.includes(window.location.hostname)) { - addHoverTag(settings.defaultLanguage); + let sourceLanguage = await getWebsiteLanguage(); + + console.log(sourceLanguage); + + addHoverTag(settings.defaultLanguage, sourceLanguage); } } +function getWebsiteLanguage(){ + let text = $('body').text().substring(0,300); + return detectLanguage(text); +} \ No newline at end of file diff --git a/src/content/hover.js b/src/content/hover.js index a11a4de..bac04b6 100644 --- a/src/content/hover.js +++ b/src/content/hover.js @@ -1,7 +1,7 @@ -function addHoverTag(targetLanguage) { +function addHoverTag(targetLanguage, sourceLanguage) { // (?![^<]*?>)([A-z0-9']+) - // - (?![^<]*?>) to not consider any html tags. or rather, to not consider anything wrapped in <>. But since all <'s are coded as < in html, it only affects tags - // - ([A-z0-9']+) apart from tags, we consider all words, numbers or words containing "'" + // - (?![^<]*?>) to not consider any html elements. or rather, to not consider anything wrapped in <>. But since all <'s are coded as < in html, it only affects tags + // - ([A-z0-9']+) apart from elements, we consider all words, numbers or words containing "'" $('p').each(function () { $(this).html($(this).html().replace(/(?![^<]*?>)([A-z0-9']+)/g , '$1')); @@ -13,7 +13,7 @@ function addHoverTag(targetLanguage) { setTimeoutConst = setTimeout(async function () { let text = self.attr('data-tooltip'); if(self.text() === text) { - let translation = await translate(text, 'eng', targetLanguage); + let translation = await translate(text, sourceLanguage, targetLanguage); self.attr('data-tooltip', translation); } }, 1000);