Open translation tab next to the current one
This new behavior will hopefully improve the user experience, by making
it identical to the behavior when clicking a link with the
target="_blank" attribute.
In particular, apart from open the tab next to the current one, when
closing it, the previous tab will be focused so the user can continue
reading the page.
Change-Id: I2c3629f7220d72928ee38481deb859c7f8bce591
diff --git a/src/background.ts b/src/background.ts
index 1bed978..b0af7de 100644
--- a/src/background.ts
+++ b/src/background.ts
@@ -55,7 +55,9 @@
// #!endif
];
-function translationClick(info: chrome.contextMenus.OnClickData): void {
+function translationClick(
+ info: chrome.contextMenus.OnClickData,
+ initiatorTab: chrome.tabs.Tab): void {
const optionsPromise = Options.getOptions();
const ssPromise =
ExtSessionStorage.get(['contextMenuItems', 'translatorTab']);
@@ -69,12 +71,17 @@
const url = URLFactory.getTranslationURL(
contextMenuItem?.language, info, contextMenuItem?.dataType);
+ const newTabOptions: Parameters<typeof chrome.tabs.create>[0] = {
+ url,
+ openerTabId: initiatorTab.id,
+ };
+ if (initiatorTab.index) newTabOptions.index = initiatorTab.index + 1;
if (contextMenuItem?.dataType !== DataType.DataTypeText) {
// Always create a simple new tab for data types other than text.
// @TODO(https://iavm.xyz/b/translateselectedtext/7): Review this
// behavior in the future.
- chrome.tabs.create({url});
+ chrome.tabs.create(newTabOptions);
} else if (translatorTab && options.uniqueTab == 'yep') {
chrome.tabs.update(translatorTab, {url}, tab => {
chrome.tabs.highlight(
@@ -85,7 +92,7 @@
} else if (options.uniqueTab == 'popup') {
chrome.windows.create({type: 'popup', url, width: 1000, height: 382});
} else {
- chrome.tabs.create({url}, tab => {
+ chrome.tabs.create(newTabOptions, tab => {
ExtSessionStorage.set({translatorTab: tab.id});
});
}
@@ -198,7 +205,7 @@
chrome.notifications.clear(notification_id);
});
-chrome.contextMenus.onClicked.addListener(info => {
+chrome.contextMenus.onClicked.addListener((info, tab) => {
for (const type of MENU_ITEM_TYPES) {
if (info.menuItemId == `${type.prefix}tr_options`) {
chrome.runtime.openOptionsPage();
@@ -206,7 +213,7 @@
}
}
- translationClick(info);
+ translationClick(info, tab);
});
chrome.tabs.onRemoved.addListener(tabId => {