Skip to content

Commit

Permalink
add: separate translate window's auto respond
Browse files Browse the repository at this point in the history
  • Loading branch information
chunibyocola committed Mar 10, 2021
1 parent 53647a2 commit 99f170f
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
"optionsFontSizeOfTranslatePanel": {
"message": "Font size of translate panel"
},
"optionsStwAutoRespondDescription": {
"message": "Separate translate window will auto respond to the translate request which you click the \"translate\" item of context menus in the extension unavailable page."
},
"themePreset": {
"message": "Preset"
},
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
"optionsFontSizeOfTranslatePanel": {
"message": "翻訳パネルのフォントサイズ"
},
"optionsStwAutoRespondDescription": {
"message": "スタンドアロン翻訳ウィンドウは、拡張機能が利用できないページのコンテキストメニューの「翻訳」項目をクリックした翻訳リクエストに自動的に応答します。"
},
"themePreset": {
"message": "プリセット"
},
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
"optionsFontSizeOfTranslatePanel": {
"message": "翻译面板的字体大小"
},
"optionsStwAutoRespondDescription": {
"message": "独立翻译窗口会自动响应你在扩展不可用的页面中点击右键菜单中的“翻译”项后产生的翻译请求。"
},
"themePreset": {
"message": "预设"
},
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
"optionsFontSizeOfTranslatePanel": {
"message": "翻譯面板的字體大小"
},
"optionsStwAutoRespondDescription": {
"message": "獨立翻譯視窗會自動回應你在擴充功能不可用的頁面中點擊右鍵功能表中的「翻譯」項後產生的翻譯請求。"
},
"themePreset": {
"message": "預設"
},
Expand Down
3 changes: 2 additions & 1 deletion src/constants/chromeSendMessageTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const SCTS_TRANSLATE_COMMAND_KEY_PRESSED = 'SCTS_TRANSLATE_COMMAND_KEY_PR
export const SCTS_AUDIO_COMMAND_KEY_PRESSED = 'SCTS_AUDIO_COMMAND_KEY_PRESSED';
export const SCTS_CALL_OUT_COMMAND_KEY_PRESSED = 'SCTS_CALL_OUT_COMMAND_KEY_PRESSED';
export const SCTS_SEND_TEXT_TO_SEPARATE_WINDOW = 'SCTS_SEND_TEXT_TO_SEPARATE_WINDOW';
export const SCTS_CLOSE_COMMAND_KEY_PRESSED = 'SCTS_CLOSE_COMMAND_KEY_PRESSED';
export const SCTS_CLOSE_COMMAND_KEY_PRESSED = 'SCTS_CLOSE_COMMAND_KEY_PRESSED';
export const SCTS_SEPARATE_WINDOW_SET_TEXT = 'SCTS_SEPARATE_TRANSLATE_SET_TEXT';
13 changes: 11 additions & 2 deletions src/entry/background/context-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { listenOptionsChange } from '../../public/options';
import { SCTS_CONTEXT_MENUS_CLICKED } from '../../constants/chromeSendMessageTypes';
import { createNewTab, getI18nMessage, getLocalStorage } from '../../public/chrome-call';
import { createSeparateWindow } from './separate-window';
import { getIsContentScriptEnabled } from '../../public/utils';

const createContextMenus = () => {
chrome.contextMenus.create({
id: 'sc-translator-context-menu',
title: `${getI18nMessage('wordTranslate')} "%s"`,
contexts: ['selection'],
onclick: ({ selectionText }, tab) => {
tab?.id >= 0 && chrome.tabs.sendMessage(tab.id, { type: SCTS_CONTEXT_MENUS_CLICKED, payload: { selectionText } });
onclick: async ({ selectionText }, tab) => {
if (tab?.id >= 0) {
const enabled = await getIsContentScriptEnabled(tab.id);
enabled
? chrome.tabs.sendMessage(tab.id, { type: SCTS_CONTEXT_MENUS_CLICKED, payload: { selectionText } })
: createSeparateWindow(selectionText);
}
else {
createSeparateWindow(selectionText);
}
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion src/entry/background/separate-window.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SCTS_CALL_OUT_COMMAND_KEY_PRESSED } from "../../constants/chromeSendMessageTypes";
import { SCTS_CALL_OUT_COMMAND_KEY_PRESSED, SCTS_SEPARATE_WINDOW_SET_TEXT } from "../../constants/chromeSendMessageTypes";
import { getLocalStorage, sendMessageToTab } from "../../public/chrome-call";
import { listenOptionsChange } from "../../public/options";
import { getQueryString } from "../../public/translate/utils";
Expand All @@ -20,6 +20,8 @@ export const createSeparateWindow = async (text) => {
chrome.windows.update(windowId, { focused: true });

sendMessageToTab(tabId, { type: SCTS_CALL_OUT_COMMAND_KEY_PRESSED });

text && sendMessageToTab(tabId, { type: SCTS_SEPARATE_WINDOW_SET_TEXT, payload: { text } });
}
else {
let query = '';
Expand Down
3 changes: 3 additions & 0 deletions src/entry/options/Options/sections/SeparateWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const SeparateWindow = ({ updateStorage, rememberStwSizeAndPosition }) => {
<div className='opt-section-row'>
<div className='item-description'>{getMessage('optionsStwDescription')}</div>
</div>
<div className='opt-section-row'>
<div className='item-description'>{getMessage('optionsStwAutoRespondDescription')}</div>
</div>
<div className='opt-section-row'>
<OptionToggle
id='remember-separate-window-size-and-position'
Expand Down
4 changes: 2 additions & 2 deletions src/entry/separate/HandleCommands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SCTS_CALL_OUT_COMMAND_KEY_PRESSED,
SCTS_CLOSE_COMMAND_KEY_PRESSED,
SCTS_CONTEXT_MENUS_CLICKED,
SCTS_SEND_TEXT_TO_SEPARATE_WINDOW,
SCTS_SEPARATE_WINDOW_SET_TEXT,
SCTS_TRANSLATE_COMMAND_KEY_PRESSED
} from '../../../constants/chromeSendMessageTypes';
import { useOnExtensionMessage } from '../../../public/react-use';
Expand Down Expand Up @@ -39,7 +39,7 @@ const HandleCommands = () => {
case SCTS_CALL_OUT_COMMAND_KEY_PRESSED:
dispatch(callOutResultBox());
break;
case SCTS_SEND_TEXT_TO_SEPARATE_WINDOW:
case SCTS_SEPARATE_WINDOW_SET_TEXT:
payload.text && dispatch(mtSetText({ text: payload.text }));
break;
case SCTS_CLOSE_COMMAND_KEY_PRESSED:
Expand Down

0 comments on commit 99f170f

Please sign in to comment.