From 4e3792aba319b52f957c70347e859f677972e4e2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 5 Feb 2017 16:39:40 -0800 Subject: [PATCH] handle content script and background script desync on version update --- ext/fg/css/frame.css | 4 ++++ ext/fg/frame.html | 6 +++++- ext/fg/js/driver.js | 27 ++++++++++++++++----------- ext/fg/js/frame.js | 27 +++++++++++++++++++++++---- ext/fg/js/popup.js | 6 +++++- ext/fg/js/util.js | 23 ++++++++++++++++------- ext/manifest.json | 2 +- 7 files changed, 70 insertions(+), 25 deletions(-) diff --git a/ext/fg/css/frame.css b/ext/fg/css/frame.css index 7e9c3b19..27aaadb6 100644 --- a/ext/fg/css/frame.css +++ b/ext/fg/css/frame.css @@ -119,6 +119,10 @@ hr { height: 1px; } +#orphan { + display: none; +} + /* term styles */ .term-expression { diff --git a/ext/fg/frame.html b/ext/fg/frame.html index 959855ba..b7e2b41a 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -10,7 +10,11 @@ -
+
+
+

Yomichan Updated!

+

The Yomichan extension has been updated to a new version! In order to continue viewing definitions on this page you must reload this tab or restart your browser.

+
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', {}); } diff --git a/ext/manifest.json b/ext/manifest.json index 31b2c8c4..e296ec06 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Yomichan", - "version": "1.0.8", + "version": "1.0.9", "description": "Japanese dictionary with Anki integration", "icons": {"16": "img/icon16.png", "48": "img/icon48.png", "128": "img/icon128.png"},