From db918d36754529933ae4896032146755675b891a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 19 Oct 2016 09:26:26 -0700 Subject: [PATCH] Relog when ankiweb auth settings change --- ext/bg/js/ankiweb.js | 4 ++++ ext/bg/js/options-form.js | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ext/bg/js/ankiweb.js b/ext/bg/js/ankiweb.js index 1393f668..bed605ec 100644 --- a/ext/bg/js/ankiweb.js +++ b/ext/bg/js/ankiweb.js @@ -75,6 +75,10 @@ class AnkiWeb { }); } + logout() { + return AnkiWeb.loadPage('https://ankiweb.net/account/logout', null); + } + static scrape(username, password) { return AnkiWeb.loadAccountPage('https://ankiweb.net/edit/', null, username, password).then(response => { const modelsMatch = /editor\.models = (.*}]);/.exec(response); diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 170b1e8c..fdaf059f 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -198,12 +198,15 @@ function onOptionsChanged(e) { yomichan().setOptions(optsNew); updateVisibility(optsNew); - const invalidated = - optsNew.ankiMethod !== optsOld.ankiMethod || + const loginChanged = optsNew.ankiUsername !== optsOld.ankiUsername || optsNew.ankiPassword !== optsOld.ankiPassword; - if (invalidated) { + if (loginChanged && optsNew.ankiMethod === 'ankiweb') { + anki().logout().then(() => populateAnkiDeckAndModel(optsNew)).catch(error => { + $('#anki-error').show().find('span').text(error); + }); + } else if (loginChanged || optsNew.ankiMethod !== optsOld.ankiMethod) { populateAnkiDeckAndModel(optsNew); } });