commit ecf3c625bc2e83d4ff05ba06f3c35610b5ecf5f1 Author: OverPoweredDev Date: Thu Jul 1 13:33:47 2021 +0530 started work on webpage translation diff --git a/README.md b/README.md index e7c21ff..d8d3dc2 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Refer to the [Edge Documentation](https://docs.microsoft.com/en-us/microsoft-edg │   ├── lib │   │   ├── bootstrap.min.css │   │   ├── jquery.min.js -│   │   ├── hover.js +│   │   ├── translate.js │   │   └── storage.js │   ├── background │   │   └── background.html/js @@ -98,26 +98,26 @@ Refer to the [Edge Documentation](https://docs.microsoft.com/en-us/microsoft-edg ``` - **lib/** -All library-ish files i.e. those that contain only function definitions which are called from multiple places. jQuery and Bootstrap are well known, storage.js is meant for all functions that deal with localStorage, and hover for hover-translation related stuff. +All library-ish files i.e. those that contain only function definitions which are called from multiple places. There's jQuery 3.6 and Bootstrap 5.0. storage.js is meant for all functions that deal with localStorage, and hover for hover-translation related stuff. -- **Background/** +- **background/** background.js contains all background scripts such as the ContextMenu option, the script to redirect the user to the settings page on being installed and so on. -- **Content/** +- **content/** Modifying the page DOM can only be done through content scripts. The scripts here are triggered on enabled sites and are the ones responsible for the word-hover and webpage translation functionality -- **Pop-Up/** +- **popup/** holds all the files necessary to build the pop-ups for the main pop-up and the smaller settings pop-up. Also has a local copy of bootstrap 5. -- **Settings/** +- **settings/** files related to the main settings page with all options. -- **Manifest/** +- **manifest.json** The manifest for the extension, outlining background scripts, pop-up data and permissions.
diff --git a/src/background/background.js b/src/background/background.js index 20193b5..bd4f5b0 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -5,8 +5,6 @@ window.browser = (function () { browser.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.method === "getSettings") { sendResponse({settings: getGlobalSettings()}); - } else { - sendResponse({}); // snub them. } }); diff --git a/src/content/content.js b/src/content/content.js index d40929d..8039c8e 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -1,15 +1,23 @@ -chrome.runtime.sendMessage({method: "getSettings"}, function (response) { +window.browser = (function () { + return window.browser || window.chrome; +})(); + +browser.runtime.sendMessage({method: "getSettings"}, function (response) { let settings = response.settings; addHoverElements(settings); }); +browser.runtime.onMessage.addListener(function(request, sender, sendResponse) { + if (request.method === "translateWebpage") { + console.log('hi') + sendResponse({}); + } +}); + async function addHoverElements(settings) { let enabledList = settings.enabledWebsites; if (enabledList.includes(window.location.hostname)) { let sourceLanguage = await getWebsiteLanguage(); - - console.log(sourceLanguage); - addHoverTag(settings.defaultLanguage, sourceLanguage); } } diff --git a/src/lib/hover.js b/src/lib/translate.js similarity index 89% rename from src/lib/hover.js rename to src/lib/translate.js index 08ffb9d..43a6d51 100644 --- a/src/lib/hover.js +++ b/src/lib/translate.js @@ -13,7 +13,7 @@ function addHoverTag(targetLanguage, sourceLanguage) { setTimeoutConst = setTimeout(async function () { let text = self.attr('data-translation'); if(self.text() === text) { - let translation = await translate(text, sourceLanguage, targetLanguage); + let translation = await translateWord(text, sourceLanguage, targetLanguage); self.attr('data-translation', translation); } }, 1000); @@ -22,7 +22,7 @@ function addHoverTag(targetLanguage, sourceLanguage) { }); } -async function translate(inputText, sourceLanguage, targetLanguage) { +async function translateWord(inputText, sourceLanguage, targetLanguage) { let outputText; let langPair = ""; langPair = langPair.concat(sourceLanguage, "|", targetLanguage); diff --git a/src/manifest.json b/src/manifest.json index fdc2622..1457590 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -34,7 +34,7 @@ "all_frames": true, "js": [ "lib/jquery.min.js", - "lib/hover.js", + "lib/translate.js", "lib/storage.js", "content/content.js" ], diff --git a/src/popup/popup.html b/src/popup/popup.html index 7cf6aa3..1e687be 100644 --- a/src/popup/popup.html +++ b/src/popup/popup.html @@ -5,7 +5,7 @@ Apertium - + diff --git a/src/popup/popup.js b/src/popup/popup.js index 60e1dd8..d3d6ae5 100644 --- a/src/popup/popup.js +++ b/src/popup/popup.js @@ -101,17 +101,24 @@ $("#translate-button").on('click', async function () { // TODO: Translate the entire current webpage $("#translate-webpage-button").on('click', function () { + window.browser = (function () { + return window.browser || window.chrome; + })(); + + chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ + chrome.tabs.sendMessage(tabs[0].id, {method: "translateWebpage"}, function(response) {}); + }); }); // Enable hover if inactive before, disable if active $("#enable-hover-checkbox").on('click', async function () { await chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => { let url = tabs[0].url; + let hostname = new URL(url).hostname; if($(this).is(":checked")){ - addToEnabledWebsiteList(globalSettings, url); + addToEnabledWebsiteList(globalSettings, hostname); } else { - let hostname = new URL(url).hostname; removeFromEnabledWebsiteList(globalSettings, hostname); } });