From cf0dcf25226611590d76d263b5a4acdc89510d09 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 7 Jul 2018 17:52:01 +0900 Subject: [PATCH] blacklist as an util --- src/content/components/common/index.js | 31 ++++++------------- src/shared/blacklists.js | 13 ++++++++ test/shared/blacklists.test.js | 42 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 src/shared/blacklists.js create mode 100644 test/shared/blacklists.test.js diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index 3a23956..e81bf1f 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -5,6 +5,7 @@ import * as settingActions from 'content/actions/setting'; import messages from 'shared/messages'; import * as addonActions from '../../actions/addon'; import * as re from 'shared/utils/re'; +import * as blacklists from 'shared/blacklists'; export default class Common { constructor(win, store) { @@ -46,27 +47,15 @@ export default class Common { }); } - let blacklist = JSON.stringify(this.store.getState().setting.blacklist); - if (blacklist !== this.prevBlacklist) { - this.prevBlacklist = blacklist; - - this.disableIfBlack(this.store.getState().setting.blacklist); - } - } - - disableIfBlack(blacklist) { - let loc = this.win.location; - let partial = loc.host + loc.pathname; - let matched = blacklist - .map((item) => { - let pattern = item.includes('/') ? item : item + '/*'; - return re.fromWildcard(pattern); - }) - .some(regex => regex.test(partial)); - if (matched) { - this.store.dispatch(addonActions.disable()); - } else { - this.store.dispatch(addonActions.enable()); + let blacklist = this.store.getState().setting.blacklist; + let str = JSON.stringify(blacklist) + if (blacklist !== str) { + this.prevBlacklist = str; + if (blacklists.includes(blacklist, this.win.location)) { + this.store.dispatch(addonActions.disable()); + } else { + this.store.dispatch(addonActions.enable()); + } } } diff --git a/src/shared/blacklists.js b/src/shared/blacklists.js new file mode 100644 index 0000000..21e3e14 --- /dev/null +++ b/src/shared/blacklists.js @@ -0,0 +1,13 @@ +import * as re from 'shared/utils/re'; + +const includes = (blacklist, url) => { + let u = new URL(url) + return blacklist.some((item) => { + if (!item.includes('/')) { + return re.fromWildcard(item).test(u.hostname); + } + return re.fromWildcard(item).test(u.hostname + u.pathname); + }); +} + +export { includes }; diff --git a/test/shared/blacklists.test.js b/test/shared/blacklists.test.js new file mode 100644 index 0000000..87e89c5 --- /dev/null +++ b/test/shared/blacklists.test.js @@ -0,0 +1,42 @@ +import { includes } from 'shared/blacklists'; + +describe("shared/blacklist", () => { + it('matches by *', () => { + let blacklist = ['*']; + + expect(includes(blacklist, 'https://github.com/abc')).to.be.true; + }) + + it('matches by hostname', () => { + let blacklist = ['github.com']; + + expect(includes(blacklist, 'https://github.com')).to.be.true; + expect(includes(blacklist, 'https://gist.github.com')).to.be.false; + expect(includes(blacklist, 'https://github.com/ueokande')).to.be.true; + expect(includes(blacklist, 'https://github.org')).to.be.false; + expect(includes(blacklist, 'https://google.com/search?q=github.org')).to.be.false; + }) + + it('matches by hostname with wildcard', () => { + let blacklist = ['*.github.com']; + + expect(includes(blacklist, 'https://github.com')).to.be.false; + expect(includes(blacklist, 'https://gist.github.com')).to.be.true; + }) + + it('matches by path', () => { + let blacklist = ['github.com/abc']; + + expect(includes(blacklist, 'https://github.com/abc')).to.be.true; + expect(includes(blacklist, 'https://github.com/abcdef')).to.be.false; + expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; + }) + + it('matches by path with wildcard', () => { + let blacklist = ['github.com/abc*']; + + expect(includes(blacklist, 'https://github.com/abc')).to.be.true; + expect(includes(blacklist, 'https://github.com/abcdef')).to.be.true; + expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; + }) +});