Skip to content

Commit

Permalink
refactor: Paste in the input box automatically;
Browse files Browse the repository at this point in the history
Remove optional permissions
"clipboardRead";
Due to extension can not pass the test of chrome with "the permission of clipboardRead has not been used";

After my testing, extension don't need this permission to implement this feature;
So I remove the permission and refactor this feature;
  • Loading branch information
chunibyocola committed Nov 14, 2021
1 parent 725b2c3 commit cd9fab9
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 35 deletions.
3 changes: 3 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@
"optionsTranslateToTargetLanguage": {
"message": "Translate to target language"
},
"optionsAutoPasteInTheInputBoxErrorDescription": {
"message": "Error: Failed to get the permission of reading clipboard. You need to allow extension to read your clipboard. You can click on the icon on the right of the URL bar to allow this extension to read the clipboard. You can also allow the requestion in the following link:"
},
"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 @@ -362,6 +362,9 @@
"optionsTranslateToTargetLanguage": {
"message": "目標言語に翻訳します"
},
"optionsAutoPasteInTheInputBoxErrorDescription": {
"message": "エラー:クリップボードの読み取り許可を取得できませんでした。クリップボードを読み取りの許可はこの拡張機能にとっては必要でございます。URLバーの右側のアイコンをクリックして拡張機能のクリップボードへのアクセスを許可できます。次のリンクでも許可を管理できます:"
},
"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 @@ -362,6 +362,9 @@
"optionsTranslateToTargetLanguage": {
"message": "翻译为目标语言"
},
"optionsAutoPasteInTheInputBoxErrorDescription": {
"message": "错误:未能获得读取剪贴板的许可。你需要允许该扩展读取剪贴板。你可以点击 URL 栏右边的图标来允许该扩展访问剪贴板。同样你可以通过以下链接来管理剪贴板权限:"
},
"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 @@ -362,6 +362,9 @@
"optionsTranslateToTargetLanguage": {
"message": "翻譯為目標語言"
},
"optionsAutoPasteInTheInputBoxErrorDescription": {
"message": "錯誤:未能獲得讀取剪貼板的許可。你需要允許該擴充功能讀取剪貼板。你可以點擊 URL 欄右邊的圖標來允許該擴充功能訪問剪貼板。同樣你可以通過以下鏈接來管理剪貼板權限:"
},
"themePreset": {
"message": "預設"
},
Expand Down
3 changes: 0 additions & 3 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@
"host_permissions": [
"<all_urls>"
],
"optional_permissions": [
"clipboardRead"
],
"content_scripts" : [
{
"matches": ["http://*/*", "https://*/*"],
Expand Down
1 change: 0 additions & 1 deletion src/constants/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const defaultOptions: DefaultOptions = {
doNotRespondInTextBox: false,
autoTranslateAfterInput: true,
contextMenus: defaultContextMenus,
clipboardReadPermission: false,
autoPasteInTheInputBox: false,
enableInsertResult: false,
autoInsertResult: false,
Expand Down
2 changes: 0 additions & 2 deletions src/entry/options/Options/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const Options: React.FC = () => {
doNotRespondInTextBox,
autoTranslateAfterInput,
contextMenus,
clipboardReadPermission,
autoPasteInTheInputBox,
enableInsertResult,
autoInsertResult,
Expand Down Expand Up @@ -108,7 +107,6 @@ const Options: React.FC = () => {
<div className='sub-title' id='clipboard'>{getMessage('optionsClipboard')}</div>
<Clipboard
updateStorage={updateStorage}
clipboardReadPermission={clipboardReadPermission}
autoPasteInTheInputBox={autoPasteInTheInputBox}
/>
<div className='sub-title' id='web-page-translating'>{getMessage('optionsWebPageTranslating')}</div>
Expand Down
51 changes: 26 additions & 25 deletions src/entry/options/Options/sections/Clipboard.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import React from 'react';
import React, { useState } from 'react';
import { GenericOptionsProps } from '..';
import { getMessage } from '../../../../public/i18n';
import { DefaultOptions } from '../../../../types';
import OptionToggle from '../../OptionToggle';

type ClipboardProps = GenericOptionsProps<Pick<
DefaultOptions,
'clipboardReadPermission' |
'autoPasteInTheInputBox'
>>;

const Clipboard: React.FC<ClipboardProps> = ({ updateStorage, clipboardReadPermission, autoPasteInTheInputBox }) => {
const updateClipboardReadPermission = () => {
if (clipboardReadPermission) {
chrome.permissions.remove({ permissions: ['clipboardRead'] }, removed => removed && updateStorage('clipboardReadPermission', false));
}
else {
chrome.permissions.request({ permissions: ['clipboardRead'] }, granted => granted && updateStorage('clipboardReadPermission', true));
}
};
const Clipboard: React.FC<ClipboardProps> = ({ updateStorage, autoPasteInTheInputBox }) => {
const [error, setError] = useState(false);

return (
<div className='opt-section'>
<div className='opt-section-row'>
<OptionToggle
id='allow-extension-to-read-clipboard'
message='optionsAllowExtensionToReadClipboard'
checked={clipboardReadPermission}
onClick={updateClipboardReadPermission}
id='read-clipboard-automatically'
message='optionsReadClipboardAutomatically'
checked={autoPasteInTheInputBox}
onClick={() => {
if (autoPasteInTheInputBox) {
updateStorage('autoPasteInTheInputBox', false);
}
else {
navigator.clipboard.readText().then(() => {
error && setError(false);
updateStorage('autoPasteInTheInputBox', true);
}).catch(() => {
setError(true);
});
}
}}
/>
<div className='item-description'>{getMessage('optionsAllowExtensionToReadClipboardDescription')}</div>
<div className='mt10-ml30'>
<OptionToggle
id='read-clipboard-automatically'
message='optionsReadClipboardAutomatically'
checked={autoPasteInTheInputBox}
onClick={() => updateStorage('autoPasteInTheInputBox', !autoPasteInTheInputBox)}
/>
<div className='item-description'>{getMessage('optionsReadClipboardAutomaticallyDescription')}</div>
</div>
<div className='item-description'>{getMessage('optionsReadClipboardAutomaticallyDescription')}</div>
{error && <div className='item-description'>
{getMessage('optionsAutoPasteInTheInputBoxErrorDescription')}
<span onClick={() => chrome.tabs.create({ url: 'chrome://settings/content/clipboard' })} className='span-link'>
settings/content/clipboard
</span>
</div>}
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/entry/popup/MultipleTranslateResult/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const MultipleTranslateResult: React.FC<MultipleTranslateResultProps> = ({ autoT
dispatch(callOutPanel());
};

getOptions().autoPasteInTheInputBox && chrome.permissions.contains({ permissions: ['clipboardRead'] }, result => result && readClipboardText());
getOptions().autoPasteInTheInputBox && readClipboardText();
}, [dispatch]);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/entry/popup/SingleTranslateResult/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const SingleTranslateResult: React.FC<SingleTranslateResultProps> = ({ autoTrans
dispatch(callOutPanel());
};

getOptions().autoPasteInTheInputBox && chrome.permissions.contains({ permissions: ['clipboardRead'] }, result => result && readClipboardText());
getOptions().autoPasteInTheInputBox && readClipboardText();
}, [dispatch]);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/entry/separate/Separate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const Separate: React.FC = () => {
};

if (getOptions().autoPasteInTheInputBox && !text) {
chrome.permissions.contains({ permissions: ['clipboardRead'] }, result => result && readClipboardText());
readClipboardText();
}
else {
dispatch(callOutPanel());
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export type DefaultOptions = {
doNotRespondInTextBox: boolean;
autoTranslateAfterInput: boolean;
contextMenus: OptionsContextMenu[];
clipboardReadPermission: boolean;
autoPasteInTheInputBox: boolean;
enableInsertResult: boolean;
autoInsertResult: boolean;
Expand All @@ -123,4 +122,5 @@ export type DefaultOptions = {
export type DeprecatedOptions = {
showButtonAfterSelect: boolean;
enableContextMenus: boolean;
clipboardReadPermission: boolean;
};

0 comments on commit cd9fab9

Please sign in to comment.