diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index e9190f9..54be1f5 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -207,6 +207,12 @@ "invalidShortcutMessage": { "message": "The shortcut can not be used" }, + "clear": { + "message": "Clear" + }, + "reset": { + "message": "Reset" + }, "informationLabel": { "message": "Information" @@ -251,5 +257,9 @@ }, "translateLinkMenu": { "message": "Translate selected link" + }, + + "openPopupDescription": { + "message": "Open toolbar popup" } } diff --git a/src/background/background.js b/src/background/background.js index fbbcb09..e6c65ad 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -5,6 +5,7 @@ import { initSettings, handleSettingsChange } from "src/settings/settings"; import { updateLogLevel, overWriteLogLevel } from "src/common/log"; import onInstalledListener from "./onInstalledListener"; import { showMenus, onMenusShownListener, onMenusClickedListener } from "./menus"; +import { onCommandListener } from "./keyboardShortcuts"; const logDir = "background/background"; @@ -30,3 +31,4 @@ const init = async () => { init(); browser.runtime.onInstalled.addListener(onInstalledListener); +browser.commands.onCommand.addListener(onCommandListener); diff --git a/src/background/keyboardShortcuts.js b/src/background/keyboardShortcuts.js new file mode 100644 index 0000000..b506289 --- /dev/null +++ b/src/background/keyboardShortcuts.js @@ -0,0 +1,64 @@ +import browser from "webextension-polyfill"; +import browserInfo from "browser-info"; +import log from "loglevel"; +import { getSettings, setSettings } from "src/settings/settings"; +import getShortcut from "src/common/getShortcut"; +import manifest from "src/manifest-chrome.json"; + +const logDir = "background/keyboardShortcuts"; + +export const initShortcuts = async () => { + const isValidShortcuts = browserInfo().name == "Firefox" && browserInfo().version >= 60; + if (!isValidShortcuts) return; + log.info(logDir, "initShortcuts()"); + + let initedShortcuts = getSettings("initedShortcuts") || []; + + const commands = manifest.commands; + for (const commandId of Object.keys(commands)) { + if (initedShortcuts.includes(commandId)) continue; + + try { + await browser.commands.update({ name: commandId, shortcut: getShortcut(commandId) }); + initedShortcuts.push(commandId); + } catch (e) { + log.error(logDir, "initShortcuts()", e); + } + } + setSettings("initedShortcuts", initedShortcuts); +}; + +export const onCommandListener = async command => { + log.log(logDir, "onCommandListener()", command); + switch (command) { + case "translateSelectedText": { + translateSelectedText(); + break; + } + case "translatePage": { + translatePage(); + break; + } + } +}; + +const translateSelectedText = async () => { + const tab = (await browser.tabs.query({ active: true }))[0]; + browser.tabs.sendMessage(tab.id, { + message: "translateSelectedText" + }); +}; +const translatePage = async () => { + const tab = (await browser.tabs.query({ active: true }))[0]; + const tabInfo = await browser.tabs.sendMessage(tab.id, { message: "getTabInfo" }); + + const targetLang = getSettings("targetLang"); + const encodedPageUrl = encodeURIComponent(tabInfo.url); + const translationUrl = `https://translate.google.com/translate?hl=${targetLang}&sl=auto&u=${encodedPageUrl}`; + + browser.tabs.create({ + url: translationUrl, + active: true, + index: tab.index + 1 + }); +}; diff --git a/src/common/getShortcut.js b/src/common/getShortcut.js index 2c4ac77..1319c83 100644 --- a/src/common/getShortcut.js +++ b/src/common/getShortcut.js @@ -1,5 +1,5 @@ import browserInfo from "browser-info"; -import manifest from "src/manifest-firefox.json"; +import manifest from "src/manifest-chrome.json"; export default commandId => { const suggestedKeys = manifest.commands[commandId].suggested_key || null; diff --git a/src/manifest-chrome.json b/src/manifest-chrome.json index 1630c45..fcdc9a7 100644 --- a/src/manifest-chrome.json +++ b/src/manifest-chrome.json @@ -45,5 +45,23 @@ "matches": ["http://*/*", "https://*/*", ""], "js": ["content/content.js"] } - ] + ], + + "commands": { + "_execute_browser_action": { + "description": "__MSG_openPopupDescription__", + "suggested_key": { + "default": "Alt+F1" + } + }, + "translateSelectedText": { + "description": "__MSG_translateTextMenu__", + "suggested_key": { + "default": "Alt+F2" + } + }, + "translatePage": { + "description": "__MSG_translatePageMenu__" + } + } } diff --git a/src/manifest-firefox.json b/src/manifest-firefox.json index ecb9501..7a45a55 100644 --- a/src/manifest-firefox.json +++ b/src/manifest-firefox.json @@ -51,5 +51,17 @@ "matches": ["http://*/*", "https://*/*", ""], "js": ["content/content.js"] } - ] + ], + + "commands": { + "_execute_browser_action": { + "description": "__MSG_openPopupDescription__" + }, + "translateSelectedText": { + "description": "__MSG_translateTextMenu__" + }, + "translatePage": { + "description": "__MSG_translatePageMenu__" + } + } }