From d8dbb625763821dfc01ef2f43ae19a88eac800a1 Mon Sep 17 00:00:00 2001 From: gmachine1 Date: Sun, 15 Aug 2021 13:09:48 +0800 Subject: [PATCH 1/6] allow lookup when there is already a popup --- js/hoverdict.js | 13 ++++++++++--- manifest.json | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/js/hoverdict.js b/js/hoverdict.js index 560db1a..5a24a08 100644 --- a/js/hoverdict.js +++ b/js/hoverdict.js @@ -258,7 +258,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 +288,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 +300,8 @@ function documentMouseDown(event) { } } +function documentMouseDown(event) { + closePopup(); +} + $(document).mousemove(documentMouseMove).keydown(documentKeyDown).keyup(documentKeyUp).mousedown(documentMouseDown); \ No newline at end of file diff --git a/manifest.json b/manifest.json index 9fe4495..e2ee8c7 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "Hover Lookup", - "version": "1.1", + "version": "1.1.1", "description": "Holding + while hovering over a word will show the wiktionary entry for that word in an inline window.", "manifest_version": 2, "permissions": [ From 1c5eeb68932a12301449814dc0e5cd3852dbccdd Mon Sep 17 00:00:00 2001 From: gmachine1 Date: Sun, 15 Aug 2021 16:35:15 +0800 Subject: [PATCH 2/6] check a subset of languages to persist in browser storage --- js/options.js | 22 ++++++++++++++++++++++ manifest.json | 6 ++++-- options.html | 12 ++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 js/options.js create mode 100644 options.html diff --git a/js/options.js b/js/options.js new file mode 100644 index 0000000..0bd5031 --- /dev/null +++ b/js/options.js @@ -0,0 +1,22 @@ +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']) + + properties['lang_list'].forEach(function(value, index, array) { + document.getElementById(value).checked = true; + }); + }) + $('body').css("visibility","visible"); +} + +function saveOptions() { + var langList = $("input[name='lang']:checked").toArray().map(function getId(e) { return e.id} ) + chrome.storage.local.set({'lang_list': langList}, function() { + console.log('lang_list set to: ' + langList) + }) + alert("Options saved!"); +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index e2ee8c7..9a0a2b1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,12 @@ { "name": "Hover Lookup", - "version": "1.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..feb9af0 --- /dev/null +++ b/options.html @@ -0,0 +1,12 @@ + + +

Languages to include

+ +
+
+
+ + + + + \ No newline at end of file From 14dea9f7e6024ca82ff89aa6a3401f94690fd4d1 Mon Sep 17 00:00:00 2001 From: gmachine1 Date: Sun, 15 Aug 2021 19:49:02 +0800 Subject: [PATCH 3/6] use hidden attribute instead of css, set all if no checkboxes are selected --- js/options.js | 23 +++++++++++++++++------ options.html | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/js/options.js b/js/options.js index 0bd5031..260b9f3 100644 --- a/js/options.js +++ b/js/options.js @@ -6,17 +6,28 @@ function loadOptions() { chrome.storage.local.get('lang_list', function(properties) { console.log('lang_list: ' + properties['lang_list']) - properties['lang_list'].forEach(function(value, index, array) { + var langList = properties['lang_list'] + if (langList == undefined || langList.length == 0) { + langList = $("input[name='lang']").toArray().map(function getId(e) { return e.id} ); + } + langList.forEach(function(value, index, array) { document.getElementById(value).checked = true; }); }) - $('body').css("visibility","visible"); + $('body').attr("hidden", false); } function saveOptions() { - var langList = $("input[name='lang']:checked").toArray().map(function getId(e) { return e.id} ) + var langList = $("input[name='lang']:checked").toArray().map(function getId(e) { return e.id} ); + display_all_languages = langList.length == 0; + if (display_all_languages) { + langList = $("input[name='lang']").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) - }) - alert("Options saved!"); + console.log('lang_list set to: ' + langList); + }) + setTimeout(function() { alert("Options saved!"); }, 100); } \ No newline at end of file diff --git a/options.html b/options.html index feb9af0..712e016 100644 --- a/options.html +++ b/options.html @@ -1,7 +1,7 @@

Languages to include

- +


From e5caec2b69b48a614b84331aa84a460396344c99 Mon Sep 17 00:00:00 2001 From: gmachine1 Date: Sun, 15 Aug 2021 21:58:25 +0800 Subject: [PATCH 4/6] language filtering appears to work when run on Latin as well as word 'is' in browser --- js/background.js | 8 +++++++- js/hoverdict.js | 36 ++++++++++++++++++++++++------------ wiktionary_lookup.html | 2 +- 3 files changed, 32 insertions(+), 14 deletions(-) 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 5a24a08..ad27f36 100644 --- a/js/hoverdict.js +++ b/js/hoverdict.js @@ -1,4 +1,4 @@ -var DEBUG = false; +var DEBUG = true; var pressedKeys = {}; var mousePagePosition = {}; @@ -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) { + 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); 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 @@ - +