blacklist as an util
This commit is contained in:
		
							parent
							
								
									327144a3aa
								
							
						
					
					
						commit
						cf0dcf2522
					
				
					 3 changed files with 65 additions and 21 deletions
				
			
		|  | @ -5,6 +5,7 @@ import * as settingActions from 'content/actions/setting'; | ||||||
| import messages from 'shared/messages'; | import messages from 'shared/messages'; | ||||||
| import * as addonActions from '../../actions/addon'; | import * as addonActions from '../../actions/addon'; | ||||||
| import * as re from 'shared/utils/re'; | import * as re from 'shared/utils/re'; | ||||||
|  | import * as blacklists from 'shared/blacklists'; | ||||||
| 
 | 
 | ||||||
| export default class Common { | export default class Common { | ||||||
|   constructor(win, store) { |   constructor(win, store) { | ||||||
|  | @ -46,29 +47,17 @@ export default class Common { | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     let blacklist = JSON.stringify(this.store.getState().setting.blacklist); |     let blacklist = this.store.getState().setting.blacklist; | ||||||
|     if (blacklist !== this.prevBlacklist) { |     let str = JSON.stringify(blacklist) | ||||||
|       this.prevBlacklist = blacklist; |     if (blacklist !== str) { | ||||||
| 
 |       this.prevBlacklist = str; | ||||||
|       this.disableIfBlack(this.store.getState().setting.blacklist); |       if (blacklists.includes(blacklist, this.win.location)) { | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   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()); |         this.store.dispatch(addonActions.disable()); | ||||||
|       } else { |       } else { | ||||||
|         this.store.dispatch(addonActions.enable()); |         this.store.dispatch(addonActions.enable()); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   reloadSettings() { |   reloadSettings() { | ||||||
|     try { |     try { | ||||||
|  |  | ||||||
							
								
								
									
										13
									
								
								src/shared/blacklists.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/shared/blacklists.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -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 }; | ||||||
							
								
								
									
										42
									
								
								test/shared/blacklists.test.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								test/shared/blacklists.test.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -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; | ||||||
|  |   }) | ||||||
|  | }); | ||||||
		Reference in a new issue