Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Fix #453, increase timeouts and limit permission checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ianb committed Oct 22, 2019
1 parent 1669d1e commit 669531f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
34 changes: 34 additions & 0 deletions extension/limiter.js
Original file line number Diff line number Diff line change
@@ -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;
})();
1 change: 1 addition & 0 deletions extension/manifest.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"settings.js",
"js/vendor/fuse.js",
"browserUtil.js",
"limiter.js",
"background/pageMetadata.js",
"background/main.js",
"background/voiceSchema.js",
Expand Down
12 changes: 9 additions & 3 deletions extension/services/spotify/spotify.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/* globals intents, serviceList, content */
/* globals intents, serviceList, content, limiter */

this.services.spotify = (function() {
class Spotify extends serviceList.Service {
async playQuery(query) {
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);
}
Expand Down
12 changes: 9 additions & 3 deletions extension/services/youtube/youtube.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 669531f

Please sign in to comment.