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

Commit

Permalink
Fix #335. Fix #318. Make 'pause' pause everything, and pause everythi…
Browse files Browse the repository at this point in the history
…ng when playing a new thing

This adds a .pauseAny() method on services, as well as a special case for Reader Mode.
  • Loading branch information
ianb committed Sep 30, 2019
1 parent 6db100a commit 2ba2cec
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 19 deletions.
29 changes: 24 additions & 5 deletions extension/background/serviceList.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ this.serviceList = (function() {
this.context.onError = this.onError.bind(this);
}

get id() {
const id = this.constructor.id;
if (!id) {
throw new Error(`Class has no id: ${this.constructor.name}`);
}
return id;
}

get baseUrl() {
return this.constructor.baseUrl;
}
Expand Down Expand Up @@ -140,26 +148,37 @@ this.serviceList = (function() {
async getTab(activate = false) {
const tabs = await this.getAllTabs();
if (!tabs.length) {
return browser.tabs.create({ url: this.baseUrl, active: activate });
return this.context.createTab({
url: this.baseUrl,
active: activate,
});
}
if (activate) {
await browser.tabs.update(tabs[0].id, { active: activate });
}
return tabs[0];
}

async getAllTabs() {
return browser.tabs.query({ url: this.matchPatterns });
async getAllTabs(extraQuery) {
const query = Object.assign(
{ url: this.matchPatterns },
extraQuery || {}
);
return browser.tabs.query(query);
}

async initTab(scripts) {
this.tab = await this.getTab();
await content.lazyInject(this.tab.id, scripts);
}

async callTab(name, args) {
callTab(name, args) {
return this.callOneTab(this.tab.id, name, args);
}

async callOneTab(tabId, name, args) {
args = args || {};
const response = browser.tabs.sendMessage(this.tab.id, {
const response = browser.tabs.sendMessage(tabId, {
type: name,
...args,
});
Expand Down
24 changes: 21 additions & 3 deletions extension/intents/music/music.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals intentRunner, serviceList, log */
/* globals intentRunner, serviceList, log, intents */

this.intents.music = (function() {
const exports = {};
Expand Down Expand Up @@ -31,6 +31,16 @@ this.intents.music = (function() {
return new ServiceClass(context);
}

async function pauseAnyBut(context, serviceId) {
for (const ServiceClass of Object.values(SERVICES)) {
if (ServiceClass.id === serviceId) {
continue;
}
const service = new ServiceClass(context);
await service.pauseAny();
}
}

intentRunner.registerIntent({
name: "music.play",
examples: ["Play Green Day"],
Expand All @@ -41,6 +51,10 @@ this.intents.music = (function() {
async run(context) {
const service = await getService(context);
await service.playQuery(context.slots.query);
// FIXME: this won't pause other YouTube tabs when you play a new YouTube tab,
// though maybe YouTube should handle that itself?
await pauseAnyBut(context, service.id);
await intents.read.pauseAny();
},
});

Expand All @@ -49,12 +63,16 @@ this.intents.music = (function() {
examples: ["Pause music"],
match: `
pause [service:musicServiceName]
pause
pause music
stop music
`,
async run(context) {
const service = await getService(context);
await service.pause();
for (const ServiceClass of Object.values(SERVICES)) {
const service = new ServiceClass(context);
await service.pauseAny();
}
await intents.read.pauseAny();
},
});

Expand Down
21 changes: 18 additions & 3 deletions extension/intents/read/read.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals util, content */
/* globals util, content, log */

this.intents.read = (function() {
const exports = {};
Expand All @@ -11,11 +11,26 @@ this.intents.read = (function() {
if (!activeTab.url.startsWith("about:reader")) {
return false;
}
await content.lazyInject(activeTab.id, ["/intents/read/startNarration.js"]);
await browser.tabs.sendMessage(activeTab.id, {
return stopReadingTab(activeTab.id);
};

async function stopReadingTab(tabId) {
await content.lazyInject(tabId, ["/intents/read/startNarration.js"]);
await browser.tabs.sendMessage(tabId, {
type: "stopReading",
});
return true;
}

exports.pauseAny = async function() {
const tabs = await browser.tabs.query({ url: "about:reader*" });
for (const tab of tabs) {
try {
await stopReadingTab(tab.id);
} catch (e) {
log.info("Error pausing reading in tab:", String(e));
}
}
};

this.intentRunner.registerIntent({
Expand Down
9 changes: 8 additions & 1 deletion extension/services/spotify/spotify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals intents, serviceList */
/* globals intents, serviceList, content */

this.services.spotify = (function() {
class Spotify extends serviceList.Service {
Expand All @@ -16,6 +16,13 @@ this.services.spotify = (function() {
await this.initTab("/services/spotify/player.js");
await this.callTab("unpause");
}

async pauseAny() {
for (const tab of await this.getAllTabs({ audible: true })) {
await content.lazyInject(tab.id, "/services/spotify/player.js");
await this.callOneTab(tab.id, "pause");
}
}
}

Object.assign(Spotify, {
Expand Down
4 changes: 1 addition & 3 deletions extension/services/youtube/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ this.player = (function() {
class Player extends helpers.Runner {
action_play() {
const button = this.querySelector("button.ytp-large-play-button");
console.log("clicking button", button);
button.click();
console.log("clicked");
}

action_pause() {
const button = this.querySelector(
"buttonytp-play-button[aria-label^='Pause']"
"button.ytp-play-button[aria-label^='Pause']"
);
button.click();
}
Expand Down
14 changes: 10 additions & 4 deletions extension/services/youtube/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
this.services.youtube = (function() {
class YouTube extends serviceList.Service {
async playQuery(query) {
this.tab = await browser.tabs.create({
this.tab = await this.context.createTab({
url: searching.googleSearchUrl(`${query} youtube.com`, true),
active: true,
});
browser.tabs.update(this.tab.id, { muted: true });
await content.lazyInject(this.tab.id, "/services/youtube/player.js");
await this.callTab("play");
}

async pause() {
await this.initTab("/services/spotify/player.js");
await this.initTab("/services/youtube/player.js");
await this.callTab("pause");
}

async unpause() {
await this.initTab("/services/spotify/player.js");
await this.initTab("/services/youtube/player.js");
await this.callTab("unpause");
}

async pauseAny() {
for (const tab of await this.getAllTabs({ audible: true })) {
await content.lazyInject(tab.id, "/services/youtube/player.js");
await this.callOneTab(tab.id, "pause");
}
}
}

Object.assign(YouTube, {
Expand Down

0 comments on commit 2ba2cec

Please sign in to comment.