Skip to content

Commit 7ef6ee5

Browse files
committed
Small refact tracker
1 parent 432b2a3 commit 7ef6ee5

File tree

5 files changed

+38
-74
lines changed

5 files changed

+38
-74
lines changed

src/compositions/valid-page.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import Browser from 'webextension-polyfill';
33
export function isValidPage(tab: Browser.Tabs.Tab | undefined): boolean{
44
if (tab == null || tab == undefined || !tab.url || !tab.id) return false;
55

6-
if ((tab.url.indexOf('http:') == -1 && tab.url.indexOf('https:') == -1)
7-
|| tab.url.indexOf('chrome://') !== -1
8-
|| tab.url.indexOf('chrome-extension://') !== -1)
6+
if ((!tab.url.startsWith('http:') && !tab.url.startsWith('https:'))
7+
|| tab.url.startsWith('chrome://')
8+
|| tab.url.startsWith('chrome-extension://'))
99
return false;
1010
return true;
1111
}

src/icons/bmc-new-btn-logo.svg

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/repository/tabs-repository-interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import Browser from 'webextension-polyfill';
44
export interface ITabsRepository {
55
getTabs(): Tab[];
66
getTab(domain:string): Tab | undefined;
7-
addTab(tab:Browser.Tabs.Tab): Promise<Tab | null>;
7+
addTab(domain: string, favicon: string | undefined): Promise<Tab | undefined>;
88
}

src/repository/tabs-repository.ts

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
11
import { ITabsRepository } from "./tabs-repository-interface";
22
import { Tab } from "../entity/tab";
3-
import Browser from 'webextension-polyfill';
43
import { injecStorage } from "../storage/inject-storage";
5-
import { isValidPage } from '../compositions/valid-page';
64
import { isInBlackList } from "../compositions/black-list";
7-
import { extractHostname } from "../compositions/extract-hostname";
85
import { StorageDeserializeParam } from "../storage/storage-params";
96

107
export class TabsRepository implements ITabsRepository {
11-
private tabs: Tab[];
8+
private tabs: Tab[];
129

13-
constructor() {
14-
this.tabs = [];
15-
}
16-
17-
async initAsync(){
18-
this.tabs = await injecStorage().getDeserializeList(StorageDeserializeParam.TABS) as Tab[];
19-
}
10+
constructor() {
11+
this.tabs = [];
12+
}
2013

21-
getTabs(): Tab[] {
22-
return this.tabs;
23-
}
14+
async initAsync() {
15+
this.tabs = (await injecStorage().getDeserializeList(
16+
StorageDeserializeParam.TABS
17+
)) as Tab[];
18+
}
2419

25-
getTab(domain: string): Tab | undefined {
26-
return this.tabs?.find(x => x.url === domain);
27-
}
20+
getTabs(): Tab[] {
21+
return this.tabs;
22+
}
2823

29-
async addTab(tab: Browser.Tabs.Tab): Promise<Tab | undefined> {
30-
if (isValidPage(tab)) {
31-
if (tab.id && (tab.id != 0)) {
32-
const domain = extractHostname(tab.url);
33-
const tabFromStorage = this.getTab(domain);
34-
const isInBlackListFlag = await isInBlackList(domain);
35-
36-
if (!isInBlackListFlag){
37-
if (!tabFromStorage) {
38-
let favicon = tab.favIconUrl;
39-
if (!favicon) {
40-
favicon = 'chrome://favicon/' + domain;
41-
}
42-
const newTab = new Tab();
43-
newTab.init(domain, favicon);
44-
this.tabs.push(newTab);
45-
return newTab;
46-
}
47-
}
48-
}
49-
}
24+
getTab(domain: string): Tab | undefined {
25+
return this.tabs?.find((x) => x.url === domain);
26+
}
27+
28+
async addTab(domain: string, favicon: string | undefined): Promise<Tab | undefined> {
29+
const tabFromStorage = this.getTab(domain);
30+
const isInBlackListFlag = await isInBlackList(domain);
5031

51-
return undefined;
32+
if (!isInBlackListFlag) {
33+
if (!tabFromStorage) {
34+
if (!favicon) {
35+
favicon = "chrome://favicon/" + domain;
36+
}
37+
const newTab = new Tab();
38+
newTab.init(domain, favicon);
39+
this.tabs.push(newTab);
40+
return newTab;
41+
}
5242
}
53-
}
43+
44+
return undefined;
45+
}
46+
}

src/tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function trackTime(){
3939
} else {
4040
let tab = repo.getTab(activeDomain);
4141
if (tab == undefined) {
42-
tab = await repo.addTab(activeTab!);
42+
tab = await repo.addTab(activeDomain, activeTab?.favIconUrl);
4343
}
4444
if (tab != undefined){
4545
const inactivityInterval = (await storage.getValue(

0 commit comments

Comments
 (0)