[wip] content

This commit is contained in:
Shin'ya Ueoka 2018-07-07 16:58:09 +09:00
parent 85b4bd5b07
commit 327144a3aa
6 changed files with 86 additions and 58 deletions

View file

@ -4,6 +4,7 @@ import FollowComponent from './follow';
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';
export default class Common {
constructor(win, store) {
@ -14,8 +15,10 @@ export default class Common {
input.onKey(key => follow.key(key));
input.onKey(key => keymapper.key(key));
this.win = win;
this.store = store;
this.prevEnabled = undefined;
this.prevBlacklist = undefined;
this.reloadSettings();
@ -42,14 +45,34 @@ export default class Common {
enabled,
});
}
let blacklist = JSON.stringify(this.store.getState().setting.blacklist);
if (blacklist !== this.prevBlacklist) {
this.prevBlacklist = blacklist;
this.disableIfBlack(this.store.getState().setting.blacklist);
}
}
async reloadSettings() {
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());
}
}
reloadSettings() {
try {
let settings = await browser.runtime.sendMessage({
type: messages.SETTINGS_QUERY,
});
this.store.dispatch(settingActions.set(settings));
this.store.dispatch(settingActions.load());
} catch (e) {
// Sometime sendMessage fails when background script is not ready.
console.warn(e);