From 15b93060c76ee4d59eb8ef90f6e1f8dea4c34ac9 Mon Sep 17 00:00:00 2001 From: guidocella Date: Sat, 16 Dec 2017 19:56:53 +0100 Subject: Add option to play audio automatically --- ext/bg/js/options.js | 1 + ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 4 ++++ ext/fg/js/float.js | 18 ++++++++++++++++++ ext/fg/js/frontend.js | 1 + ext/fg/js/popup.js | 6 ++++++ ext/mixed/js/display.js | 15 +++++++++++++-- 7 files changed, 45 insertions(+), 2 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 925c8432..373a1a6b 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -185,6 +185,7 @@ function optionsSetDefaults(options) { enable: true, audioSource: 'jpod101', audioVolume: 100, + autoPlayAudio: false, resultOutputMode: 'group', debugInfo: false, maxResults: 32, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index dcc9c43d..7bc6a651 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -24,6 +24,7 @@ async function formRead() { optionsNew.general.showGuide = $('#show-usage-guide').prop('checked'); optionsNew.general.compactTags = $('#compact-tags').prop('checked'); optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked'); + optionsNew.general.autoPlayAudio = $('#auto-play-audio').prop('checked'); optionsNew.general.resultOutputMode = $('#result-output-mode').val(); optionsNew.general.audioSource = $('#audio-playback-source').val(); optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); @@ -153,6 +154,7 @@ async function onReady() { $('#show-usage-guide').prop('checked', options.general.showGuide); $('#compact-tags').prop('checked', options.general.compactTags); $('#compact-glossaries').prop('checked', options.general.compactGlossaries); + $('#auto-play-audio').prop('checked', options.general.autoPlayAudio); $('#result-output-mode').val(options.general.resultOutputMode); $('#audio-playback-source').val(options.general.audioSource); $('#audio-playback-volume').val(options.general.audioVolume); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 65043d44..c77f550c 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -43,6 +43,10 @@ +
+ +
+
diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index ff50483d..1f08702d 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -20,6 +20,8 @@ class DisplayFloat extends Display { constructor() { super($('#spinner'), $('#definitions')); + this.autoPlayAudioTimer = null; + $(window).on('message', utilAsync(this.onMessage.bind(this))); } @@ -54,6 +56,10 @@ class DisplayFloat extends Display { this.kanjiShow(definitions, options, context); }, + clearAutoPlayTimer: () => { + this.clearAutoPlayTimer(); + }, + orphaned: () => { this.onOrphaned(); } @@ -83,6 +89,18 @@ class DisplayFloat extends Display { super.onKeyDown(e); } } + + autoPlayAudio() { + this.clearAutoPlayTimer(); + this.autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400); + } + + clearAutoPlayTimer() { + if (this.autoPlayAudioTimer) { + window.clearTimeout(this.autoPlayAudioTimer); + this.autoPlayAudioTimer = null; + } + } } window.yomichan_display = new DisplayFloat(); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 3a90b3ad..266f9640 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -254,6 +254,7 @@ class Frontend { searchClear() { docImposterDestroy(); this.popup.hide(); + this.popup.clearAutoPlayTimer(); if (this.options.scanning.selectText && this.textSourceLast) { this.textSourceLast.deselect(); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index d1009fe9..a17b184a 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -125,6 +125,12 @@ class Popup { this.invokeApi('kanjiShow', {definitions, options, context}); } + clearAutoPlayTimer() { + if (this.injected) { + this.invokeApi('clearAutoPlayTimer'); + } + } + invokeApi(action, params={}) { this.container.contentWindow.postMessage({action, params}, '*'); } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 5d3c4f2e..43eb93c1 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -185,8 +185,7 @@ class Display { 80: /* p */ () => { if (e.altKey) { if ($('.entry').eq(this.index).data('type') === 'term') { - const expressionIndex = this.options.general.resultOutputMode === 'merge' ? 0 : -1; - this.audioPlay(this.definitions[this.index], expressionIndex); + this.audioPlay(this.definitions[this.index], this.firstExpressionIndex); } return true; @@ -258,6 +257,10 @@ class Display { this.container.html(content); this.entryScrollIntoView(context && context.index || 0); + if (this.options.general.autoPlayAudio && this.options.general.audioSource !== 'disabled') { + this.autoPlayAudio(); + } + $('.action-add-note').click(this.onNoteAdd.bind(this)); $('.action-view-note').click(this.onNoteView.bind(this)); $('.action-play-audio').click(this.onAudioPlay.bind(this)); @@ -309,6 +312,10 @@ class Display { } } + autoPlayAudio() { + this.audioPlay(this.definitions[0], this.firstExpressionIndex); + } + async adderButtonUpdate(modes, sequence) { try { const states = await apiDefinitionsAddable(this.definitions, modes); @@ -422,6 +429,10 @@ class Display { } } + get firstExpressionIndex() { + return this.options.general.resultOutputMode === 'merge' ? 0 : -1; + } + static clozeBuild(sentence, source) { const result = { sentence: sentence.text.trim() -- cgit v1.2.3