diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index a7ca9b2..77d0d33 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -10,10 +10,10 @@ "message": "Donate with PayPal" }, "enableOnThisPage": { - "message": "Enable on this page" + "message": "Enable working on this page" }, "disableOnThisPage": { - "message": "Disable on this page" + "message": "Disable working on this page" }, "initialTextArea": { "message": "Enter text" @@ -85,6 +85,12 @@ "ifChangeSecondLangOnPageCaptionLabel": { "message": "Detects the language of the selected text, and if it is the same as the default target language, translate it into the second language." }, + "disableUrlListLabel": { + "message": "URL list to disable working" + }, + "disableUrlListCaptionLabel": { + "message": "If the URL matches the list, working on the web page is disabled. Please enter as a regular expression." + }, "toolbarLabel": { "message": "Toolbar popup" diff --git a/src/content/components/TranslateContainer.js b/src/content/components/TranslateContainer.js index babb6b4..47fbdd2 100644 --- a/src/content/components/TranslateContainer.js +++ b/src/content/components/TranslateContainer.js @@ -72,9 +72,7 @@ const matchesTargetLang = async selectedText => { }; const waitTime = time => { - return new Promise(resolve => { - setTimeout(resolve(), time); - }); + return new Promise(resolve => setTimeout(() => resolve(), time)); }; export default class TranslateContainer extends Component { @@ -104,6 +102,19 @@ export default class TranslateContainer extends Component { browser.runtime.onMessage.addListener(this.handleMessage); overWriteLogLevel(); updateLogLevel(); + if (this.props.isFirst) this.disableExtensionByUrlList(); + }; + + disableExtensionByUrlList = () => { + const disableUrlList = getSettings("disableUrlList"); + const disableUrls = disableUrlList.split("\n"); + const pageUrl = top.location.href; + + const isMatched = disableUrls.some(urlReg => { + if (urlReg.trim() === "") return false; + return RegExp("^" + urlReg.trim()).test(pageUrl); + }); + if (isMatched) this.props.removeElement(); }; handleMessage = async request => { diff --git a/src/content/index.js b/src/content/index.js index 5b586a7..9ef62e8 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -15,11 +15,9 @@ const handleMessage = async request => { case "getEnabled": return isEnabled; case "enableExtension": - isEnabled = true; insertElement(); break; case "disableExtension": - isEnabled = false; removeElement(); break; default: @@ -31,19 +29,28 @@ browser.runtime.onMessage.addListener(handleMessage); const removeElement = () => { const element = document.getElementById("simple-translate"); if (!element) return; + ReactDOM.unmountComponentAtNode(element); element.parentNode.removeChild(element); + isEnabled = false; }; +let isFirst = true; const insertElement = () => { const element = document.getElementById("simple-translate"); if (element) return; document.body.insertAdjacentHTML("beforeend", "
"); ReactDOM.render( - , + , document.getElementById("simple-translate") ); + isFirst = false; + isEnabled = true; }; insertElement(); diff --git a/src/options/components/OptionContainer.js b/src/options/components/OptionContainer.js index 8ae8811..fb004ee 100644 --- a/src/options/components/OptionContainer.js +++ b/src/options/components/OptionContainer.js @@ -68,6 +68,18 @@ export default props => { /> ); break; + case "textarea": + formId = id; + optionForm = ( +