Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating review/lesson count when visiting a wanikani.com page #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
21 changes: 15 additions & 6 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,34 @@ window.onload = function() {
});

// update data every x milliseconds
function loopRequestUserData(){
function loopRequestUserData(notify = true) {
var wkUserData = JSON.parse(localStorage.wkUserData);
requestUserData(true, function(){
requestUserData(notify, function() {
loopRequestId = window.setTimeout(loopRequestUserData, wkUserData.refreshInterval, true, true);
});
}

function restartLoop(notify) {
window.clearTimeout(loopRequestId);
loopRequestId = loopRequestUserData(notify);
}

// when the update interval is changed, restart loopRequestUserData with the updated interval
chrome.storage.onChanged.addListener(function(changes, namespace) {
if ('wkUserData' in changes && loopRequestId !== undefined) {
var oldValues = changes.wkUserData.oldValue;
var newValues = changes.wkUserData.newValue;
if (newValues.refreshInterval != oldValues.refreshInterval) {
window.clearTimeout(loopRequestId);
loopRequestId = loopRequestUserData();
}
if (newValues.refreshInterval != oldValues.refreshInterval)
restartLoop(true);
}
});

// when a wanikani page is loaded, restart loopRequestUserData
chrome.runtime.onMessage.addListener(function(request) {
if (request === 'wkRestartLoop')
restartLoop(false);
});

// disable 'X-Frame-Options' header to allow inlining pages within an iframe
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
Expand Down
2 changes: 1 addition & 1 deletion js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ window.onload = function() {
// a new key has been entered: save
} else if (key != wkUserData.userPublicKey){

getApiData(key, "user-information", function(obj){
getApiData(key, "user-information", function(obj){

// the key is not valid
if (obj.user_information === undefined){
Expand Down
4 changes: 3 additions & 1 deletion js/web-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ window.onload = function() {
);
}
}
});

// send a message to background.js to restart loopRequestUserData (and update wkUserData)
chrome.runtime.sendMessage("wkRestartLoop");

// hide the alert messages (ex: if the user is already logged)
var isError = document.getElementsByClassName('alert alert-error fade in');
Expand Down