diff options
author | Alex Yatskov <alex@foosoft.net> | 2017-02-05 16:39:40 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2017-02-05 16:39:40 -0800 |
commit | 4e3792aba319b52f957c70347e859f677972e4e2 (patch) | |
tree | 3ea382513378f852f2126d1113e21061c61adec8 /ext/fg/js | |
parent | 5076b80f9690a12fda23d06b2c3ddd1df08cff56 (diff) |
handle content script and background script desync on version update1.0.9
Diffstat (limited to 'ext/fg/js')
-rw-r--r-- | ext/fg/js/driver.js | 27 | ||||
-rw-r--r-- | ext/fg/js/frame.js | 27 | ||||
-rw-r--r-- | ext/fg/js/popup.js | 6 | ||||
-rw-r--r-- | ext/fg/js/util.js | 23 |
4 files changed, 60 insertions, 23 deletions
diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js index 5467a9f0..8904c12c 100644 --- a/ext/fg/js/driver.js +++ b/ext/fg/js/driver.js @@ -33,11 +33,11 @@ class Driver { window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('resize', e => this.searchClear()); - getOptions().then(options => { + Promise.all([getOptions(), isEnabled()]).then(([options, enabled]) => { this.options = options; - return isEnabled(); - }).then(enabled => { this.enabled = enabled; + }).catch(error => { + this.handleError(error); }); } @@ -119,14 +119,14 @@ class Driver { this.pendingLookup = true; this.searchTerms(textSource).then(found => { if (!found) { - this.searchKanji(textSource).then(found => { + return this.searchKanji(textSource).then(found => { if (!found && hideNotFound) { this.searchClear(); } }); } }).catch(error => { - window.alert('Error: ' + error); + this.handleError(error, textSource); }).then(() => { this.pendingLookup = false; }); @@ -157,9 +157,6 @@ class Driver { return true; } - }).catch(error => { - window.alert('Error: ' + error); - return false; }); } @@ -181,9 +178,6 @@ class Driver { return true; } - }).catch(error => { - window.alert('Error: ' + error); - return false; }); } @@ -197,6 +191,17 @@ class Driver { this.lastTextSource = null; } + handleError(error, textSource) { + if (window.orphaned) { + if (textSource) { + this.popup.showNextTo(textSource.getRect()); + this.popup.showOrphaned(); + } + } else { + showError(error); + } + } + api_setOptions(options) { this.options = options; } diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 66fa131d..dba59000 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -44,7 +44,7 @@ class Frame { window.scrollTo(0, 0); renderText(context, 'terms.html').then(content => { - $('.content').html(content); + $('#content').html(content); $('.action-add-note').click(this.onAddNote.bind(this)); $('.kanji-link').click(e => { @@ -59,6 +59,8 @@ class Frame { }); this.updateAddNoteButtons(['term_kanji', 'term_kana'], sequence); + }).catch(error => { + this.handleError(error); }); } @@ -74,13 +76,20 @@ class Frame { window.scrollTo(0, 0); renderText(context, 'kanji.html').then(content => { - $('.content').html(content); + $('#content').html(content); $('.action-add-note').click(this.onAddNote.bind(this)); this.updateAddNoteButtons(['kanji'], sequence); + }).catch(error => { + this.handleError(error); }); } + api_showOrphaned() { + $('#content').hide(); + $('#orphan').show(); + } + findAddNoteButton(index, mode) { return $(`.action-add-note[data-index="${index}"][data-mode="${mode}"]`); } @@ -98,10 +107,10 @@ class Frame { const button = this.findAddNoteButton(index, mode); button.addClass('disabled'); } else { - window.alert('Note could not be added'); + showError('note could not be added'); } }).catch(error => { - window.alert('Error: ' + error); + this.handleError(error); }).then(() => { this.showSpinner(false); }); @@ -129,6 +138,8 @@ class Frame { button.removeClass('pending'); } }); + }).catch(error => { + this.handleError(error); }); } @@ -155,6 +166,14 @@ class Frame { audio.currentTime = 0; audio.play(); } + + handleError(error) { + if (window.orphaned) { + this.api_showOrphaned(); + } else { + showError(error); + } + } } window.frame = new Frame(); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index d47ab4ae..74e25c7d 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -76,7 +76,11 @@ class Popup { this.invokeApi('showKanjiDefs', {definitions, options}); } - invokeApi(action, params) { + showOrphaned() { + this.invokeApi('showOrphaned'); + } + + invokeApi(action, params={}) { this.container.contentWindow.postMessage({action, params}, '*'); } } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index ef45d08c..ba872467 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -19,16 +19,25 @@ function invokeBgApi(action, params) { return new Promise((resolve, reject) => { - chrome.runtime.sendMessage({action, params}, ({result, error}) => { - if (error) { - reject(error); - } else { - resolve(result); - } - }); + try { + chrome.runtime.sendMessage({action, params}, ({result, error}) => { + if (error) { + reject(error); + } else { + resolve(result); + } + }); + } catch (e) { + window.orphaned = true; + reject(e.message); + } }); } +function showError(error) { + window.alert(`Error: ${error}`); +} + function isEnabled() { return invokeBgApi('getEnabled', {}); } |