Skip to content
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
8 changes: 7 additions & 1 deletion js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ function onMessage(request, sender, callback) {
}
}

chrome.runtime.onMessage.addListener(onMessage);
chrome.runtime.onMessage.addListener(onMessage);

chrome.runtime.onInstalled.addListener(details => {
chrome.storage.local.set({'lang_list': []}, function() {
console.log('lang_list set to: ' + []);
});
});
49 changes: 34 additions & 15 deletions js/hoverdict.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -297,4 +312,8 @@ function documentMouseDown(event) {
}
}

$(document).mousemove(documentMouseMove).keydown(documentKeyDown).keyup(documentKeyUp).mousedown(documentMouseDown);
function documentMouseDown(event) {
closePopup();
}

$(document).mousemove(documentMouseMove).keydown(documentKeyDown).keyup(documentKeyUp).mousedown(documentMouseDown);
31 changes: 31 additions & 0 deletions js/options.js
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 4 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "Hover Lookup",
"version": "1.1",
"version": "1.1.2",
"description": "Holding <CTRL> + <SHIFT> 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",
Expand Down
12 changes: 12 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<h3>Languages to include (uncheck all for no language filtering)</h3>
<body hidden>
<input type="checkbox" id="latin" name="lang"><label>Latin</label><br>
<input type="checkbox" id="japanese" name="lang"><label>Japanese</label><br>
<input type="checkbox" id="russian" name="lang"><label>Russian</label><br>
<input type="submit" id="save_options">
</body>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/options.js"></script>
</html>
2 changes: 1 addition & 1 deletion wiktionary_lookup.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var baseURL = 'http://en.wiktionary.org';

Expand Down