Allow wildcards in the URL list

This commit is contained in:
sienori 2019-05-03 16:22:29 +09:00
parent bb85f59ef2
commit 6104cd7f55
2 changed files with 11 additions and 7 deletions

View file

@ -89,7 +89,7 @@
"message": "URL list to disable translation"
},
"disableUrlListCaptionLabel": {
"message": "If the URL matches the list, translation on the web page is disabled. Please enter as a regular expression."
"message": "If the page URL matches the list, translation on the web page is disabled. The list allows \"*\" wildcards."
},
"toolbarLabel": {

View file

@ -106,8 +106,7 @@ export default class TranslateContainer extends Component {
};
disableExtensionByUrlList = () => {
const disableUrlList = getSettings("disableUrlList");
const disableUrls = disableUrlList.split("\n");
const disableUrls = getSettings("disableUrlList").split("\n");
let pageUrl;
try {
pageUrl = top.location.href;
@ -115,10 +114,15 @@ export default class TranslateContainer extends Component {
pageUrl = document.referrer;
}
const isMatched = disableUrls.some(urlReg => {
if (urlReg.trim() === "") return false;
return RegExp("^" + urlReg.trim()).test(pageUrl);
});
const matchesPageUrl = urlPattern => {
const pattern = urlPattern
.trim()
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, match => (match === "*" ? ".*" : "\\" + match));
if (pattern === "") return false;
return RegExp("^" + pattern + "$").test(pageUrl);
};
const isMatched = disableUrls.some(matchesPageUrl);
if (isMatched) this.props.removeElement();
};