From 669531fee6cdb8e8a85c4b6f1ad41f657c40b25c Mon Sep 17 00:00:00 2001 From: Ian Bicking Date: Tue, 22 Oct 2019 12:49:25 -0500 Subject: [PATCH] Fix #453, increase timeouts and limit permission checks This gives a bit more time before we decide a service is unable to play. It also limits the total number of warnings to 3 for each individual service. --- extension/limiter.js | 34 +++++++++++++++++++++++++++ extension/manifest.json.ejs | 1 + extension/services/spotify/spotify.js | 12 +++++++--- extension/services/youtube/youtube.js | 12 +++++++--- 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 extension/limiter.js diff --git a/extension/limiter.js b/extension/limiter.js new file mode 100644 index 000000000..0cc144e09 --- /dev/null +++ b/extension/limiter.js @@ -0,0 +1,34 @@ +this.limiter = (function() { + const exports = {}; + + exports.shouldDisplayWarning = async function( + warningName, + { times, frequency } + ) { + if (!(times || frequency)) { + throw new Error("Must provide times and/or frequency arguments"); + } + const key = `warning.${warningName}`; + const info = (await browser.storage.local.get(key))[key]; + if (!info) { + // it's never been run before + browser.storage.local.set({ [key]: { times: 1, lastTime: Date.now() } }); + return true; + } + if (times && info.times >= times) { + return false; + } + if (frequency && Date.now() - info.lastTime < frequency) { + return false; + } + browser.storage.local.set({ + [key]: { + times: info.times + 1, + lastTime: Date.now(), + }, + }); + return true; + }; + + return exports; +})(); diff --git a/extension/manifest.json.ejs b/extension/manifest.json.ejs index cf500f668..f62d96c3f 100644 --- a/extension/manifest.json.ejs +++ b/extension/manifest.json.ejs @@ -32,6 +32,7 @@ "settings.js", "js/vendor/fuse.js", "browserUtil.js", + "limiter.js", "background/pageMetadata.js", "background/main.js", "background/voiceSchema.js", diff --git a/extension/services/spotify/spotify.js b/extension/services/spotify/spotify.js index 03332da4c..c8da61c5a 100644 --- a/extension/services/spotify/spotify.js +++ b/extension/services/spotify/spotify.js @@ -1,4 +1,4 @@ -/* globals intents, serviceList, content */ +/* globals intents, serviceList, content, limiter */ this.services.spotify = (function() { class Spotify extends serviceList.Service { @@ -6,12 +6,18 @@ this.services.spotify = (function() { await this.initTab("/services/spotify/player.js"); await this.callTab("search", { query, thenPlay: true }); if (this.tabCreated) { - const isAudible = await this.pollTabAudible(this.tab.id, 2000); + const isAudible = await this.pollTabAudible(this.tab.id, 3000); if (!isAudible) { const activeTabId = (await this.context.activeTab()).id; this.context.makeTabActive(this.tab); const nowAudible = await this.pollTabAudible(this.tab.id, 1000); - if (nowAudible) { + if ( + nowAudible || + !(await limiter.shouldDisplayWarning("spotifyAudible", { + times: 3, + frequency: 1000, + })) + ) { if (this.tab.id !== activeTabId) { this.context.makeTabActive(activeTabId); } diff --git a/extension/services/youtube/youtube.js b/extension/services/youtube/youtube.js index 5c56f1df7..4ba1fa4b5 100644 --- a/extension/services/youtube/youtube.js +++ b/extension/services/youtube/youtube.js @@ -1,4 +1,4 @@ -/* globals intents, serviceList, content, util */ +/* globals intents, serviceList, content, util, limiter */ this.services.youtube = (function() { const SUPPORTED_URLS = /^https:\/\/www.youtube.com\/watch/i; @@ -36,8 +36,14 @@ this.services.youtube = (function() { if (!SUPPORTED_URLS.test(loadedTab.url)) { return; } - const isAudible = await this.pollTabAudible(this.tab.id, 2000); - if (!isAudible) { + const isAudible = await this.pollTabAudible(this.tab.id, 3000); + if ( + !isAudible && + (await limiter.shouldDisplayWarning("youtubeAudible", { + times: 3, + frequency: 1000, + })) + ) { this.context.failedAutoplay(this.tab); } }