Last active
April 28, 2017 02:12
-
-
Save jkwok91/e37ced8ba54edb5630bbc8ab1283fb76 to your computer and use it in GitHub Desktop.
chrome extension because i felt like it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dagensOrd; | |
chrome.tabs.onCreated.addListener(function(tab) { | |
if (!dagensOrd) { | |
checkIfOrdSet(); | |
} | |
getOrd(); | |
}); | |
function checkIfOrdSet() { | |
chrome.storage.sync.get('ord', function(obj) { | |
if (!obj.ord) { | |
promptForNewOrd(); | |
} else if (Date.now() - obj.ord.created > 86400000) { | |
var created = obj.ord.created; | |
// if it's time for a new word of the day! | |
chrome.storage.sync.remove('ord', function() { | |
alert("word has expired since " + created + "! please set a new one"); | |
promptForNewOrd(); | |
}); | |
} else { | |
dagensOrd = obj.ord; | |
} | |
}); | |
} | |
function promptForNewOrd() { | |
// ugh fuck this. let's chain some fuckin prompts | |
var sv = prompt("no word was set! please set one now, by entering the swedish word", "ditt ord"); | |
if (sv) { | |
var eng = prompt("great, now the english definition:", "your word"); | |
if (eng) { | |
setOrd(eng, sv, Date.now()); | |
} | |
} | |
} | |
// quiz me | |
function getOrd() { | |
var ord = dagensOrd; | |
var ans = prompt('what is the swedish for "' + ord.english + '"?'); | |
ord.numTimesQuizzed++; | |
if (ans === ord.swedish) { | |
// right on the first try! | |
ord.numTimesRight++; | |
} | |
while (ans != ord.swedish) { | |
ans = prompt('TRY AGAIN!!!!!! what is the swedish word for "' + ord.english + '"? look it up on your phone if u forgot'); | |
} | |
chrome.tts.speak(ord.swedish, {'lang': 'sv-SE'}); | |
alert('yay! "' + ord.swedish + '" does mean "' + ord.english + '"!'); | |
updateStats(); | |
} | |
function updateStats() { | |
chrome.storage.sync.set({ | |
'ord': dagensOrd | |
}, function() { | |
// saved stats | |
}); | |
} | |
function setOrd(english, swedish, date) { | |
var ord = { | |
'english': english, | |
'swedish': swedish, | |
'created': date, | |
'numTimesRight': 0, | |
'numTimesQuizzed': 0 | |
}; | |
chrome.storage.sync.set({ | |
'ord': ord | |
}, function() { | |
alert("your word has been saved"); | |
dagensOrd = ord; | |
chrome.tts.speak(swedish, {'lang': 'sv-SE'}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "word of the day", | |
"description": "a super simple vocab builder", | |
"version": "0.1", | |
"browser_action": { | |
"default_icon": "icon.png", | |
"default_popup": "popup.html" | |
}, | |
"background": { | |
"scripts": ["bg.js"], | |
"persistent": false | |
}, | |
"permissions": [ | |
"tabs", | |
"tts", | |
"storage", | |
"https://ajax.googleapis.com/" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>dagens ord</title> | |
<style> | |
body { | |
font-family: sans-serif; | |
font-size: 100%; | |
} | |
#box { | |
/* avoid an excessively wide status text */ | |
/* sure */ | |
height: 400px; | |
width: 400px; | |
} | |
#ord { | |
color: green; | |
} | |
</style> | |
<script src="popup.js"></script> | |
</head> | |
<body> | |
<div id="box"></div> | |
<button id="resetOrd"></button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var background = chrome.extension.getBackgroundPage(); | |
/** | |
* Get current word of day from storage | |
*/ | |
function getDagensOrdStats() { | |
var box = document.getElementById('box'); | |
chrome.storage.sync.get('ord', function(obj) { | |
if (!obj.ord) { | |
box.innerHTML = '<p>no word set! please set word</p>'; | |
} else { | |
// put everything into the popup | |
var ord = obj.ord; | |
box.innerHTML = '<h1>Dagens Ord</h1>' + | |
'<h6>' + new Date(ord.created) + '</h6>' + | |
'<p><span id="ord">' + ord.swedish + '</span> - ' + ord.english + '</p>' + | |
'<p>Quizzed: ' + ord.numTimesQuizzed + ' times</p>' + | |
'<p>Correctly answered: ' + ord.numTimesRight + ' times</p>'; | |
// eventually i'd like to have "yesterday's word" or something idk | |
} | |
}); | |
} | |
function resetOrd() { | |
background.dagensOrd = undefined; | |
chrome.storage.sync.remove('ord', getDagensOrdStats); | |
} | |
document.addEventListener('DOMContentLoaded', function() { | |
getDagensOrdStats(); | |
var reset = document.getElementById('resetOrd'); | |
reset.addEventListener('click', resetOrd); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
still using this thing 20 days later
but also i rly should have a place that keeps track of all the words i've claimed to have learned
today's word is "en sked" which is a spoon.