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

Commit

Permalink
Fix #122, mute all tabs while listening
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Aug 30, 2019
1 parent d6a8e28 commit 439b192
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
7 changes: 6 additions & 1 deletion extension/background/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals intentParser, intentRunner, intentExamples, log */
/* globals intentParser, intentRunner, intentExamples, log, intents */

this.main = (function() {
const exports = {};
Expand All @@ -13,6 +13,11 @@ this.main = (function() {
return exports.inDevelopment();
} else if (message.type === "getIntentSummary") {
return intentRunner.getIntentSummary();
} else if (message.type === "microphoneStarted") {
return intents.muting.temporaryMute();
} else if (message.type === "microphoneStopped") {
console.log("stopping!");
return intents.muting.temporaryUnmute();
}
log.error(
`Received message with unexpected type (${message.type}): ${message}`
Expand Down
40 changes: 40 additions & 0 deletions extension/intents/muting/muting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* globals log, intents */

this.intents.muting = (function() {
// Always undo temporary muting after this amount of time:
const TEMPORARY_MUTE_TIMEOUT = 10000; // 10 seconds
const exports = {};

this.intentRunner.registerIntent({
name: "mute.mute",
examples: ["mute all tabs"],
Expand Down Expand Up @@ -56,4 +60,40 @@ this.intents.muting = (function() {
// TODO: show confirmation
},
});

let muteTimeoutId = null;
let mutedTabIds = [];

exports.temporaryMute = async function() {
const tabsToMute = await browser.tabs.query({
audible: true,
muted: false,
});
if (!tabsToMute.length) {
return;
}
if (muteTimeoutId) {
clearTimeout(muteTimeoutId);
}
muteTimeoutId = setTimeout(() => {
exports.temporaryUnmute();
}, TEMPORARY_MUTE_TIMEOUT);
for (const tab of tabsToMute) {
browser.tabs.update(tab.id, { muted: true });
}
mutedTabIds = mutedTabIds.concat(tabsToMute.map(t => t.id));
};

exports.temporaryUnmute = function() {
if (muteTimeoutId) {
clearTimeout(muteTimeoutId);
muteTimeoutId = null;
}
for (const tabId of mutedTabIds) {
browser.tabs.update(tabId, { muted: false });
}
mutedTabIds = [];
};

return exports;
})();
5 changes: 4 additions & 1 deletion extension/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ this.popup = (function() {
let isWaitingForPermission = null;

async function init() {
document.addEventListener("beforeunload", () => {
window.addEventListener("unload", () => {
browser.runtime.sendMessage({ type: "microphoneStopped" });
if (
isWaitingForPermission &&
Date.now() - isWaitingForPermission < FAST_PERMISSION_CLOSE
Expand Down Expand Up @@ -67,6 +68,7 @@ this.popup = (function() {
}, 500);
recorder.onBeginRecording = () => {
console.info("started recording");
browser.runtime.sendMessage({ type: "microphoneStarted" });
ui.setState("listening");
ui.onStartTextInput = () => {
log.debug("detected text from the popup");
Expand All @@ -81,6 +83,7 @@ this.popup = (function() {
};
};
recorder.onEnd = json => {
browser.runtime.sendMessage({ type: "microphoneStopped" });
console.info("Got a response:", json && json.data);
clearInterval(intervalId);
ui.setState("success");
Expand Down

0 comments on commit 439b192

Please sign in to comment.