From 095c487952aa0602d9813476f73177af45ed3223 Mon Sep 17 00:00:00 2001 From: sienori Date: Tue, 30 Aug 2022 23:14:40 +0900 Subject: [PATCH] Add option to open page translation result --- src/_locales/en/messages.json | 17 ++++++++++++++++- src/background/keyboardShortcuts.js | 8 +++----- src/background/menus.js | 17 ++++++++++++----- src/common/openUrl.js | 6 ++++-- src/popup/components/Footer.js | 4 +++- src/settings/defaultSettings.js | 22 ++++++++++++++++++++++ 6 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 88eb115..de4f708 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -212,6 +212,21 @@ "ifShowMenuCaptionLabel": { "message": "Add items to the context menu displayed when right clicking on the web page or the tab." }, + "pageTranslationLabel": { + "message": "Page translation" + }, + "pageTranslationOpenToLabel": { + "message": "Tab to display translation results" + }, + "pageTranslationOpenToCaptionLabel": { + "message": "Specify the tab to display the translation results when you excute \"Translate this page\"." + }, + "newTabLabel": { + "message": "New tab" + }, + "currentTabLabel": { + "message": "Current tab" + }, "styleLabel": { "message": "Style" }, @@ -788,4 +803,4 @@ "lang_zh": { "message": "Chinese" } -} +} \ No newline at end of file diff --git a/src/background/keyboardShortcuts.js b/src/background/keyboardShortcuts.js index 4734ff7..70da658 100644 --- a/src/background/keyboardShortcuts.js +++ b/src/background/keyboardShortcuts.js @@ -4,6 +4,7 @@ import log from "loglevel"; import { getSettings, setSettings } from "src/settings/settings"; import getShortcut from "src/common/getShortcut"; import manifest from "src/manifest-chrome.json"; +import openUrl from "../common/openUrl"; const logDir = "background/keyboardShortcuts"; @@ -55,10 +56,7 @@ const translatePage = async () => { const targetLang = getSettings("targetLang"); const encodedPageUrl = encodeURIComponent(tabUrl); const translationUrl = `https://translate.google.com/translate?hl=${targetLang}&tl=${targetLang}&sl=auto&u=${encodedPageUrl}`; + const isCurrentTab = getSettings("pageTranslationOpenTo") === "currentTab"; - browser.tabs.create({ - url: translationUrl, - active: true, - index: tab.index + 1 - }); + openUrl(translationUrl, isCurrentTab); }; diff --git a/src/background/menus.js b/src/background/menus.js index 6b3b518..3119c91 100644 --- a/src/background/menus.js +++ b/src/background/menus.js @@ -82,12 +82,19 @@ function translatePage(info, tab) { const targetLang = getSettings("targetLang"); const encodedPageUrl = encodeURIComponent(info.pageUrl); const translationUrl = `https://translate.google.com/translate?hl=${targetLang}&tl=${targetLang}&sl=auto&u=${encodedPageUrl}`; + const isCurrentTab = getSettings("pageTranslationOpenTo") === "currentTab"; - browser.tabs.create({ - url: translationUrl, - active: true, - index: tab.index + 1 - }); + if (isCurrentTab) { + browser.tabs.update(tab.id, { + url: translationUrl + }); + } else { + browser.tabs.create({ + url: translationUrl, + active: true, + index: tab.index + 1 + }); + } } function translateLink(info, tab) { diff --git a/src/common/openUrl.js b/src/common/openUrl.js index 04623ef..6ed2e7d 100644 --- a/src/common/openUrl.js +++ b/src/common/openUrl.js @@ -1,6 +1,8 @@ import browser from "webextension-polyfill"; -export default async url => { +export default async (url, isCurrentTab = false) => { const activeTab = (await browser.tabs.query({ currentWindow: true, active: true }))[0]; - browser.tabs.create({ url: url, index: activeTab.index + 1 }); + + if (isCurrentTab) browser.tabs.update({ url: url }); + else browser.tabs.create({ url: url, index: activeTab.index + 1 }); }; diff --git a/src/popup/components/Footer.js b/src/popup/components/Footer.js index 1c6d325..f77dd34 100644 --- a/src/popup/components/Footer.js +++ b/src/popup/components/Footer.js @@ -2,6 +2,7 @@ import React, { Component } from "react"; import browser from "webextension-polyfill"; import openUrl from "src/common/openUrl"; import "../styles/Footer.scss"; +import { getSettings } from "../../settings/settings"; export default class Footer extends Component { constructor(props) { @@ -12,7 +13,8 @@ export default class Footer extends Component { const { tabUrl, targetLang } = this.props; const encodedUrl = encodeURIComponent(tabUrl); const translateUrl = `https://translate.google.com/translate?hl=${targetLang}&tl=${targetLang}&sl=auto&u=${encodedUrl}`; - openUrl(translateUrl); + const isCurrentTab = getSettings("pageTranslationOpenTo") === "currentTab"; + openUrl(translateUrl, isCurrentTab); }; handleChange = e => { diff --git a/src/settings/defaultSettings.js b/src/settings/defaultSettings.js index 650e59e..7b29a44 100644 --- a/src/settings/defaultSettings.js +++ b/src/settings/defaultSettings.js @@ -299,6 +299,28 @@ export default [ } ] }, + { + category: "pageTranslationLabel", + elements: [ + { + id: "pageTranslationOpenTo", + title: "pageTranslationOpenToLabel", + captions: ["pageTranslationOpenToCaptionLabel"], + type: "select", + default: "newTab", + options: [ + { + name: "newTabLabel", + value: "newTab" + }, + { + name: "currentTabLabel", + value: "currentTab" + }, + ] + } + ] + }, { category: "styleLabel", elements: [