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

Commit

Permalink
Fix multi-window cases
Browse files Browse the repository at this point in the history
Previously all code would get the 'active tab' from the first window, even if another window was focused. Also when activating a tab it would only make it active in its window, without necessarily focusing that same window. This adds methods that handle both these cases properly.
  • Loading branch information
ianb committed Oct 15, 2019
1 parent d498842 commit ed1ec51
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 43 deletions.
12 changes: 10 additions & 2 deletions extension/background/intentRunner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals log, intentParser, telemetry, catcher, searching */
/* globals log, intentParser, telemetry, catcher, searching, browserUtil */
// This gets used elsewhere as a namespace for the intent modules:
this.intents = {};

Expand Down Expand Up @@ -45,7 +45,7 @@ this.intentRunner = (function() {

async failedAutoplay(tab) {
this.keepPopup();
await browser.tabs.update(tab.id, { active: true });
this.makeTabActive(tab);
await browser.runtime.sendMessage({ type: "displayAutoplayFailure" });
}

Expand Down Expand Up @@ -111,6 +111,14 @@ this.intentRunner = (function() {
});
}

activeTab() {
return browserUtil.activeTab();
}

makeTabActive(tab) {
return browserUtil.makeTabActive(tab);
}

onError(message) {
// Can be overridden
}
Expand Down
10 changes: 6 additions & 4 deletions extension/background/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals intentParser, intentRunner, intentExamples, log, intents, telemetry, util, buildSettings, settings */
/* globals intentParser, intentRunner, intentExamples, log, intents, telemetry, util, buildSettings, settings, browserUtil */

this.main = (function() {
const exports = {};
Expand Down Expand Up @@ -56,7 +56,8 @@ this.main = (function() {
}
return browser.tabs.sendMessage(recorderTabId, message);
} else if (message.type === "makeRecorderActive") {
browser.tabs.update(recorderTabId, { active: true });
// FIXME: consider focusing the window too
browserUtil.makeTabActive(recorderTabId);
return null;
}
log.error(
Expand Down Expand Up @@ -116,13 +117,14 @@ this.main = (function() {
}
}
let tab;
const activeTabId = (await browser.tabs.query({ active: true }))[0].id;
const activeTab = await browserUtil.activeTab();
const existing = await browser.tabs.query({ url: RECORDER_URL });
if (existing.length) {
if (existing.length > 1) {
browser.tabs.remove(existing.slice(1).map(e => e.id));
}
tab = existing[0];
// FIXME: make sure window is focused?
await browser.tabs.update(tab.id, {
url: RECORDER_URL,
active: true,
Expand All @@ -145,7 +147,7 @@ this.main = (function() {
} catch (e) {}
await util.sleep(100);
}
await browser.tabs.update(activeTabId, { active: true });
await browserUtil.makeTabActive(activeTab);
}

async function launchOnboarding() {
Expand Down
8 changes: 4 additions & 4 deletions extension/background/serviceList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals content, util, settings */
/* globals content, util, settings, browserUtil */

this.services = {};

Expand Down Expand Up @@ -132,7 +132,7 @@ this.serviceList = (function() {
if (!this.tab) {
throw new Error("No tab to activate");
}
await browser.tabs.update(this.tab.id, { active: true });
this.context.makeTabActive(this.tab.id);
}

get matchPatterns() {
Expand Down Expand Up @@ -169,7 +169,7 @@ this.serviceList = (function() {
}
}
if (activate) {
await browser.tabs.update(tabs[best].id, { active: activate });
await this.context.makeTabActive(tabs[best]);
}
return { created: false, tab: tabs[best] };
}
Expand Down Expand Up @@ -231,7 +231,7 @@ this.serviceList = (function() {
};

exports.detectServiceFromActiveTab = async function(services) {
const tab = (await browser.tabs.query({ active: true }))[0];
const tab = await browserUtil.activeTab();
for (const name in services) {
const service = services[name];
if (tab.url.startsWith(service.baseUrl)) {
Expand Down
24 changes: 24 additions & 0 deletions extension/browserUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
this.browserUtil = (function() {
const exports = {};
exports.activeTab = async function activeTab() {
return (await browser.tabs.query({
active: true,
lastFocusedWindow: true,
}))[0];
};

exports.makeTabActive = async function makeTabActive(tab) {
let tabId;
if (typeof tab === "string" || typeof tab === "number") {
// then it's a tab ID
tabId = tab;
tab = await browser.tabs.get(tabId);
} else {
tabId = tab.id;
}
await browser.tabs.update(tabId, { active: true });
await browser.windows.update(tab.windowId, { focused: true });
};

return exports;
})();
7 changes: 2 additions & 5 deletions extension/intents/aboutPage/aboutPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ intents.aboutPage = (function() {
(show | open |) comments
`,
async run(context) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
// FIXME: get the "best" URL using rel=self or og:url, etc.
const activeTab = await context.activeTab();
const url = (await pageMetadata.getMetadata(activeTab.id)).canonical;
let results = (await hnSearchResults(url)).concat(
await redditSearchResults(url)
);
console.log("results", results);
results = results.filter(r => {
return r.num_comments > 0;
});
Expand All @@ -65,7 +63,6 @@ intents.aboutPage = (function() {
e.displayMessage = "Nobody has commented on this article";
throw e;
}
console.log("Comment results:", results);
if (results.length > 1) {
context.displayText(
`${results.length} comment threads found. Use "next comments" to see more`
Expand All @@ -85,7 +82,7 @@ intents.aboutPage = (function() {
previous (result | comments | comment) [move=previous]
`,
async run(context) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
const activeTab = await context.activeTab();
const results = tabResults.get(activeTab.id);
if (!results) {
const e = new Error("No comment results for this tab");
Expand Down
2 changes: 1 addition & 1 deletion extension/intents/bookmarks/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ this.intents.bookmarks = (function() {
const id = matches[0].item;
const url = bookmarksById.get(id);
if (context.parameters.tab === "this") {
const activeTab = (await browser.tabs.query({ active: true }))[0];
const activeTab = await context.activeTab();
await browser.tabs.update(activeTab.id, { url });
} else {
await browser.tabs.create({ url });
Expand Down
8 changes: 3 additions & 5 deletions extension/intents/find/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ this.intents.find = (function() {
(find | bring me to) (my | the |) [query] (tab |)
go (to | to the |) [query] tab
`,
async run(desc) {
const query = desc.slots.query;
async run(context) {
const query = context.slots.query;
log.info("the most likely query text is:", query);

// Fuse options
Expand Down Expand Up @@ -63,9 +63,7 @@ this.intents.find = (function() {
throw e;
}
const topMatch = parseInt(matches[0].item);
await browser.tabs.update(topMatch, {
active: true,
});
await context.makeTabActive(topMatch);
},
});
})();
4 changes: 2 additions & 2 deletions extension/intents/navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ this.intents.navigation = (function() {
`,
examples: ["Translate this page"],
async run(context) {
const tab = (await browser.tabs.query({ active: true }))[0];
const tab = await context.activeTab();
const translation = `https://translate.google.com/translate?hl=&sl=auto&tl=en&u=${encodeURIComponent(
tab.url
)}`;
Expand All @@ -90,7 +90,7 @@ this.intents.navigation = (function() {
`,
examples: ["Translate selection"],
async run(context) {
const tab = (await browser.tabs.query({ active: true }))[0];
const tab = await context.activeTab();
const selection = await pageMetadata.getSelection(tab.id);
if (!selection || !selection.text) {
const e = new Error("No text selected");
Expand Down
6 changes: 3 additions & 3 deletions extension/intents/notes/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ this.intents.notes = (function() {
(write | add | make) (notes |) (here | this page | this tab)
`,
async run(context) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
const activeTab = await context.activeTab();
const tabId = activeTab.id;
await content.lazyInject(tabId, SCRIPT);
const failureMessage = await browser.tabs.sendMessage(tabId, {
Expand All @@ -50,7 +50,7 @@ this.intents.notes = (function() {
`,
async run(context) {
await checkHasTab();
const activeTab = (await browser.tabs.query({ active: true }))[0];
const activeTab = await context.activeTab();
const metadata = await pageMetadata.getMetadata(activeTab.id);
const success = await browser.tabs.sendMessage(writingTabId, {
type: "addLink",
Expand Down Expand Up @@ -96,7 +96,7 @@ this.intents.notes = (function() {
e.displayMessage = "You have not set a tab to write";
throw e;
}
await browser.tabs.update(writingTabId, { active: true });
await context.makeTabActive(writingTabId);
},
});
})();
12 changes: 6 additions & 6 deletions extension/intents/read/read.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* globals util, content, log */
/* globals util, content, log, browserUtil */

this.intents.read = (function() {
const exports = {};

exports.stopReading = async function() {
const activeTab = (await browser.tabs.query({ active: true }))[0];
const activeTab = await browserUtil.activeTab();
if (!activeTab) {
return false;
}
Expand Down Expand Up @@ -43,8 +43,8 @@ this.intents.read = (function() {
match: `
read (this |) (tab | page |)
`,
async run(desc) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
async run(context) {
const activeTab = await context.activeTab();
if (!activeTab.url.startsWith("about:reader")) {
try {
await browser.tabs.toggleReaderMode();
Expand Down Expand Up @@ -83,8 +83,8 @@ this.intents.read = (function() {
match: `
stop reading (this |) (tab | page |)
`,
async run(desc) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
async run(context) {
const activeTab = await context.activeTab();
if (!activeTab.url.startsWith("about:reader")) {
const e = new Error(`Not a Reader Mode page`);
e.displayMessage = "Page isn't narrating";
Expand Down
6 changes: 3 additions & 3 deletions extension/intents/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ this.intents.search = (function() {
}
if (lastSearchIndex >= lastSearchInfo.searchResults.length - 1) {
const tabId = await openSearchTab();
await browser.tabs.update(tabId, { active: true });
await context.makeTabActive(tabId);
return;
}
lastSearchIndex++;
Expand All @@ -110,7 +110,7 @@ this.intents.search = (function() {
// eslint-disable-next-line require-atomic-updates
lastTabId = tab.id;
} else {
await browser.tabs.update(lastTabId, { url: item.url, active: true });
await context.makeTabActive(lastTabId);
}
},
});
Expand All @@ -123,7 +123,7 @@ this.intents.search = (function() {
`,
async run(context) {
const tabId = await openSearchTab();
await browser.tabs.update(tabId, { active: true });
await context.makeTabActive(tabId);
},
});
})();
4 changes: 2 additions & 2 deletions extension/intents/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ this.intents.tabs = (function() {
close tab
close this tab
`,
async run(desc) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
async run(context) {
const activeTab = await context.activeTab();
await browser.tabs.remove(activeTab.id);
},
});
Expand Down
1 change: 1 addition & 0 deletions extension/manifest.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"catcher.js",
"settings.js",
"js/vendor/fuse.js",
"browserUtil.js",
"background/pageMetadata.js",
"background/main.js",
"background/voiceSchema.js",
Expand Down
2 changes: 1 addition & 1 deletion extension/services/readerMode/readerMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ this.services.readerMode = (function() {
}

async unpause() {
const activeTab = (await browser.tabs.query({ active: true }))[0];
const activeTab = await this.context.activeTab();
if (!activeTab.url.startsWith("about:reader")) {
const e = new Error("Cannot unpause a non-reader tab");
e.displayMessage = "Cannot unpause";
Expand Down
7 changes: 3 additions & 4 deletions extension/services/spotify/spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ this.services.spotify = (function() {
if (this.tabCreated) {
const isAudible = await this.pollTabAudible(this.tab.id, 2000);
if (!isAudible) {
const activeTabId = (await browser.tabs.query({ active: true }))[0]
.id;
await browser.tabs.update(this.tab.id, { active: true });
const activeTabId = (await this.context.activeTab()).id;
this.context.makeTabActive(this.tab);
const nowAudible = await this.pollTabAudible(this.tab.id, 1000);
if (nowAudible) {
if (this.tab.id !== activeTabId) {
await browser.tabs.update(activeTabId, { active: true });
this.context.makeTabActive(activeTabId);
}
} else {
this.context.failedAutoplay(this.tab);
Expand Down
2 changes: 1 addition & 1 deletion extension/services/youtube/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ this.services.youtube = (function() {
}
let tabs = await this.getAllTabs({ audible: true });
if (!tabs.length) {
const currentTab = (await browser.tabs.query({ active: true }))[0];
const currentTab = await this.context.activeTab();
if (currentTab.url.startsWith(this.baseUrl)) {
tabs = [currentTab];
} else {
Expand Down

0 comments on commit ed1ec51

Please sign in to comment.