Implement keyboard shortcuts
This commit is contained in:
parent
9d7bc76ffd
commit
6e560f4046
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
64
src/background/keyboardShortcuts.js
Normal file
64
src/background/keyboardShortcuts.js
Normal file
|
@ -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
|
||||
});
|
||||
};
|
|
@ -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;
|
||||
|
|
|
@ -45,5 +45,23 @@
|
|||
"matches": ["http://*/*", "https://*/*", "<all_urls>"],
|
||||
"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__"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,5 +51,17 @@
|
|||
"matches": ["http://*/*", "https://*/*", "<all_urls>"],
|
||||
"js": ["content/content.js"]
|
||||
}
|
||||
]
|
||||
],
|
||||
|
||||
"commands": {
|
||||
"_execute_browser_action": {
|
||||
"description": "__MSG_openPopupDescription__"
|
||||
},
|
||||
"translateSelectedText": {
|
||||
"description": "__MSG_translateTextMenu__"
|
||||
},
|
||||
"translatePage": {
|
||||
"description": "__MSG_translatePageMenu__"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue