diff --git a/js/background.js b/js/background.js index bf3f397..1a44ac8 100644 --- a/js/background.js +++ b/js/background.js @@ -5,4 +5,10 @@ function onMessage(request, sender, callback) { } } -chrome.runtime.onMessage.addListener(onMessage); \ No newline at end of file +chrome.runtime.onMessage.addListener(onMessage); + +chrome.runtime.onInstalled.addListener(details => { + chrome.storage.local.set({'lang_list': []}, function() { + console.log('lang_list set to: ' + []); + }); +}); \ No newline at end of file diff --git a/js/hoverdict.js b/js/hoverdict.js index 560db1a..5079230 100644 --- a/js/hoverdict.js +++ b/js/hoverdict.js @@ -10,6 +10,7 @@ var POPUP_HEIGHT = 284; var POPUP_WAITING_WIDTH = 220; var POPUP_WAITING_HEIGHT = 57; + function documentMouseMove(event) { // Store mouse position mousePagePosition = {top:event.pageY, left:event.pageX}; @@ -51,6 +52,13 @@ function lookupWord(word, initialPosition) { position.left = Math.min(position.left + 10, document.body.scrollWidth - POPUP_WAITING_WIDTH - 40); position.top = Math.min(position.top + 10, document.body.scrollHeight - POPUP_WAITING_HEIGHT - 40); popup.css({"top": Math.round(position.top), "left": Math.round(position.left), "max-width": POPUP_WAITING_WIDTH, "min-width": POPUP_WAITING_WIDTH, "max-height": POPUP_WAITING_HEIGHT, "min-height": POPUP_WAITING_HEIGHT}); + + // Get available languages. + var languages = null; + chrome.storage.local.get('lang_list', function(properties) { + if (DEBUG == true) console.log('lang_list: ' + properties['lang_list']); + languages = properties['lang_list']; + }); // Make API call word = word.toLowerCase(); @@ -92,13 +100,16 @@ function lookupWord(word, initialPosition) { if(child.children.length > 0 && child.children[0].className == "mw-headline") { language = child.textContent.substr(0, child.textContent.length - 6); state = SearchState.FIND_NEW_SECTION; - - // Add language to map if it doesn't exist - if(!(language in results)) { - results[language] = { - pronunciation: null, - entries: [] - } + + // Skip if language is not included + if (languages.length == 0 || languages.includes(language.toLowerCase())) { + // Add language to map if it doesn't exist + if(!(language in results)) { + results[language] = { + pronunciation: null, + entries: [] + } + } } if(DEBUG == true) console.log("Current language:", language); @@ -109,7 +120,8 @@ function lookupWord(word, initialPosition) { case SearchState.FIND_NEW_SECTION: { // Add entry if(entry != null) { - results[language].entries.push(entry); + if (language in results) + results[language].entries.push(entry); if(DEBUG == true) console.log("Entry added:", entry); @@ -147,12 +159,12 @@ function lookupWord(word, initialPosition) { // Find and store pronunciation if(child.tagName == "UL" && child.children[0].tagName == "LI") { var ipa = child.children[0].getElementsByClassName("IPA"); - if(ipa.length > 0) { + if(language in results && ipa.length > 0) { results[language].pronunciation = ipa[0].textContent; } state = SearchState.FIND_NEW_SECTION; - if(DEBUG == true) console.log("Pronunciation found:", results[language].pronunciation); + if(language in results && DEBUG == true) console.log("Pronunciation found:", results[language].pronunciation); } } break; @@ -202,7 +214,7 @@ function lookupWord(word, initialPosition) { } // Add entry - if(entry != null) { + if(language in results && entry != null) { results[language].entries.push(entry); if(DEBUG == true) console.log("Entry added:", entry); @@ -258,7 +270,10 @@ function lookupWord(word, initialPosition) { function documentKeyDown(event) { pressedKeys[event.keyCode] = true; - if(popup == null && pressedKeys[16] && pressedKeys[17]) { + if(pressedKeys[16] && pressedKeys[17]) { + // close existing popup + closePopup(); + // Get selected text var word = getSelectionText(); @@ -285,9 +300,9 @@ function documentKeyUp(event) { pressedKeys[event.keyCode] = false; } -function documentMouseDown(event) { +function closePopup() { if(popup != null && !popup[0].contains(event.target)) { - popup.stop(true, true).fadeOut(100, + popup.stop(true, true).fadeOut(0, function() { // When animation is done, remove the popup popup.remove(); @@ -297,4 +312,8 @@ function documentMouseDown(event) { } } -$(document).mousemove(documentMouseMove).keydown(documentKeyDown).keyup(documentKeyUp).mousedown(documentMouseDown); \ No newline at end of file +function documentMouseDown(event) { + closePopup(); +} + +$(document).mousemove(documentMouseMove).keydown(documentKeyDown).keyup(documentKeyUp).mousedown(documentMouseDown); diff --git a/js/options.js b/js/options.js new file mode 100644 index 0000000..42df318 --- /dev/null +++ b/js/options.js @@ -0,0 +1,31 @@ +loadOptions(); + +document.getElementById('save_options').addEventListener('click', saveOptions); + +function loadOptions() { + chrome.storage.local.get('lang_list', function(properties) { + console.log('lang_list: ' + properties['lang_list']) + + var langList = properties['lang_list'] + if (langList == undefined) { + // When language list is empty, there is no filtering; when non-empty, this list gives the available languages. + langList = []; + } + langList.forEach(function(value, index, array) { + document.getElementById(value).checked = true; + }); + }) + $('body').attr("hidden", false); +} + +function saveOptions() { + var langList = $("input[name='lang']:checked").toArray().map(function getId(e) { return e.id} ); + langList.forEach(function(value, index, array) { + document.getElementById(value).checked = true; + }); + + chrome.storage.local.set({'lang_list': langList}, function() { + console.log('lang_list set to: ' + langList); + }) + setTimeout(function() { alert("Options saved!"); }, 100); +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 9fe4495..9a0a2b1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,12 @@ { "name": "Hover Lookup", - "version": "1.1", + "version": "1.1.2", "description": "Holding + while hovering over a word will show the wiktionary entry for that word in an inline window.", "manifest_version": 2, + "options_page": "options.html", "permissions": [ - "*://*/" + "*://*/", + "storage" ], "icons": { "16": "icon/icon_16.png", diff --git a/options.html b/options.html new file mode 100644 index 0000000..8e08c57 --- /dev/null +++ b/options.html @@ -0,0 +1,12 @@ + + +

Languages to include (uncheck all for no language filtering)

+ +
+
+
+ + + + + \ No newline at end of file diff --git a/wiktionary_lookup.html b/wiktionary_lookup.html index 1661af4..8a83abc 100644 --- a/wiktionary_lookup.html +++ b/wiktionary_lookup.html @@ -1,7 +1,7 @@ - +