diff --git a/js/background.js b/js/background.js index ecc6b46..745b002 100644 --- a/js/background.js +++ b/js/background.js @@ -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) { diff --git a/js/settings.js b/js/settings.js index c9d67a0..481c1c1 100644 --- a/js/settings.js +++ b/js/settings.js @@ -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){ diff --git a/js/web-content.js b/js/web-content.js index 34c877f..25e1454 100644 --- a/js/web-content.js +++ b/js/web-content.js @@ -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');