Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
chore: remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
sentialx committed Aug 4, 2019
1 parent c8f1ab7 commit c8046a2
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 775 deletions.
46 changes: 25 additions & 21 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import './chrome';
import {FilterConfig as Theme, DynamicThemeFix} from '../definitions';
import ThemeEngines from '../generators/theme-engines';
import {createOrUpdateDynamicTheme, removeDynamicTheme} from '../inject/dynamic-theme';
import "./chrome";
import { FilterConfig as Theme, DynamicThemeFix } from "../definitions";
import ThemeEngines from "../generators/theme-engines";
import {
createOrUpdateDynamicTheme,
removeDynamicTheme
} from "../inject/dynamic-theme";

const defaultTheme: Theme = {
mode: 1,
brightness: 100,
contrast: 100,
grayscale: 0,
sepia: 0,
useFont: false,
fontFamily: '',
textStroke: 0,
engine: ThemeEngines.dynamicTheme,
stylesheet: '',
mode: 1,
brightness: 100,
contrast: 100,
grayscale: 0,
sepia: 0,
engine: ThemeEngines.dynamicTheme,
stylesheet: ""
};

export function enable(themeOptions: Partial<Theme>, fixes: DynamicThemeFix = null, isIFrame = false) {
const theme = {...defaultTheme, ...themeOptions};
export function enable(
themeOptions: Partial<Theme>,
fixes: DynamicThemeFix = null,
isIFrame = false
) {
const theme = { ...defaultTheme, ...themeOptions };

if (theme.engine !== ThemeEngines.dynamicTheme) {
throw new Error('Theme engine is not supported');
}
if (theme.engine !== ThemeEngines.dynamicTheme) {
throw new Error("Theme engine is not supported");
}

createOrUpdateDynamicTheme(theme, fixes, isIFrame);
createOrUpdateDynamicTheme(theme, fixes, isIFrame);
}

export function disable() {
removeDynamicTheme();
removeDynamicTheme();
}
40 changes: 1 addition & 39 deletions src/background/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import Messenger from "./messenger";
import TabManager from "./tab-manager";
import UserStorage from "./user-storage";
import {
getFontList,
getCommands,
setShortcut,
canInjectScript
} from "./utils/extension-api";
import { isInTimeInterval, getDuration } from "../utils/time";
import { isURLInList, getURLHost, isURLEnabled } from "../utils/url";
import ThemeEngines from "../generators/theme-engines";
import createCSSFilterStylesheet from "../generators/css-filter";
Expand All @@ -27,13 +25,10 @@ import {
TabInfo
} from "../definitions";

const AUTO_TIME_CHECK_INTERVAL = getDuration({ seconds: 10 });

export class Extension {
ready: boolean;

config: ConfigManager;
fonts: string[];
messenger: Messenger;
tabs: TabManager;
user: UserStorage;
Expand All @@ -52,24 +47,14 @@ export class Extension {
}

isEnabled() {
if (this.user.settings.automation === "time") {
const now = new Date();
return isInTimeInterval(
now,
this.user.settings.time.activation,
this.user.settings.time.deactivation
);
}
return this.user.settings.enabled;
}

private awaiting: (() => void)[];

async start() {
await this.config.load({ local: true });
this.fonts = await getFontList();

await this.user.loadSettings();
this.changeSettings(this.user.settings);
console.log("loaded", this.user.settings);

Expand All @@ -81,7 +66,6 @@ export class Extension {
this.awaiting.forEach(ready => ready());
this.awaiting = null;

this.startAutoTimeCheck();
this.user.cleanup();
}

Expand Down Expand Up @@ -121,8 +105,7 @@ export class Extension {
if (command === "toggle") {
console.log("Toggle command entered");
this.changeSettings({
enabled: !this.isEnabled(),
automation: ""
enabled: !this.isEnabled()
});
}
if (command === "addSite") {
Expand Down Expand Up @@ -157,7 +140,6 @@ export class Extension {
isEnabled: this.isEnabled(),
isReady: this.ready,
settings: this.user.settings,
fonts: this.fonts,
shortcuts: await this.getShortcuts()
};
}
Expand All @@ -174,25 +156,7 @@ export class Extension {
}
}

private wasEnabledOnLastCheck: boolean;

private startAutoTimeCheck() {
setInterval(() => {
if (!this.ready || this.user.settings.automation !== "time") {
return;
}
const isEnabled = this.isEnabled();
if (this.wasEnabledOnLastCheck !== isEnabled) {
this.wasEnabledOnLastCheck = isEnabled;
this.tabs.sendMessage(this.getTabMessage);
this.reportChanges();
}
}, AUTO_TIME_CHECK_INTERVAL);
}

changeSettings($settings: Partial<UserSettings>) {
const prev = { ...this.user.settings };

this.user.set($settings);
this.onSettingsChanged();
}
Expand Down Expand Up @@ -239,7 +203,6 @@ export class Extension {
return;
}

this.wasEnabledOnLastCheck = this.isEnabled();
this.tabs.sendMessage(this.getTabMessage);
this.saveUserSettings();
this.reportChanges();
Expand Down Expand Up @@ -342,7 +305,6 @@ export class Extension {
// User settings

private async saveUserSettings() {
await this.user.saveSettings();
console.log("saved", this.user.settings);
}
}
3 changes: 1 addition & 2 deletions src/background/messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export default class Messenger {
chrome.runtime.onMessage.addListener(msg => {
if (msg.name === "toggle") {
this.adapter.changeSettings({
enabled: msg.toggle,
automation: ""
enabled: msg.toggle
});
}
});
Expand Down
Loading

0 comments on commit c8046a2

Please sign in to comment.