Skip to content

Commit 1ea300d

Browse files
committed
Continuation of tracker changes
1 parent ba8ee29 commit 1ea300d

File tree

5 files changed

+67
-31
lines changed

5 files changed

+67
-31
lines changed

src/compositions/black-list.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { injecStorage } from "../storage/inject-storage";
2+
import { StorageParams } from "../storage/storage-params";
3+
4+
export async function isInBlackList(url: string): Promise<boolean>{
5+
const storage = injecStorage();
6+
const blackList = await storage.getValue(StorageParams.BLACK_LIST) as string[];
7+
return blackList.indexOf(url) != -1 ? true : false;
8+
}

src/compositions/valid-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Browser from 'webextension-polyfill';
22

3-
export function isValidPage(tab: Browser.Tabs.Tab): boolean{
4-
if (tab == null || tab == undefined || !tab.url) return false;
3+
export function isValidPage(tab: Browser.Tabs.Tab | undefined): boolean{
4+
if (tab == null || tab == undefined || !tab.url || !tab.id) return false;
55

66
if ((tab.url.indexOf('http:') == -1 && tab.url.indexOf('https:') == -1)
77
|| tab.url.indexOf('chrome://') !== -1

src/repository/tabs-repository-interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { Tab } from "../storage/tab";
22
import Browser from 'webextension-polyfill';
33

44
export interface ITabsRepository {
5-
getTab(domain:string): Promise<Tab>;
6-
addTab(value:Browser.Tabs.Tab): Promise<void>;
5+
getTab(domain:string): Tab | undefined;
6+
addTab(tab:Browser.Tabs.Tab): Promise<Tab>;
77
}

src/repository/tabs-repository.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,53 @@ import { ITabsRepository } from "./tabs-repository-interface";
22
import { Tab } from "../storage/tab";
33
import Browser from 'webextension-polyfill';
44
import { injecStorage } from "../storage/inject-storage";
5+
import { isValidPage } from '../compositions/valid-page';
6+
import { isInBlackList } from "../compositions/black-list";
7+
import { extractHostname } from "../compositions/extract-hostname";
58

69
export class TabsRepository implements ITabsRepository {
7-
static tabs: Tab[];
10+
private tabs: Tab[];
811

9-
constructor(){}
12+
constructor() {
13+
this.tabs = [];
14+
}
1015

11-
static async Create(): Promise<TabsRepository> {
12-
const instance = new TabsRepository();
16+
async initAsync(){
1317
this.tabs = await injecStorage().getTabs();
14-
return instance;
15-
}
18+
}
1619

17-
getTab(domain: string): Promise<Tab> {
18-
throw new Error("Method not implemented.");
20+
getTab(domain: string): Tab | undefined {
21+
const tab = this.tabs?.find(x => x.url === domain);
22+
return !tab ? tab : undefined;
1923
}
2024

21-
addTab(value: Browser.Tabs.Tab): Promise<void> {
22-
throw new Error("Method not implemented.");
25+
async addTab(tab: Browser.Tabs.Tab): Promise<Tab> {
26+
if (isValidPage(tab)) {
27+
if (tab.id && (tab.id != 0)) {
28+
tabs = tabs || [];
29+
var domain = extractHostname(tab.url);
30+
var isDifferentUrl = false;
31+
if (currentTab !== tab.url) {
32+
isDifferentUrl = true;
33+
}
34+
35+
if (this.isNewUrl(domain) && !await isInBlackList(domain)) {
36+
var favicon = tab.favIconUrl;
37+
if (favicon === undefined) {
38+
favicon = 'chrome://favicon/' + domain;
39+
}
40+
var newTab = new Tab(domain, favicon);
41+
tabs.push(newTab);
42+
}
43+
44+
if (isDifferentUrl && await !isInBlackList(domain)) {
45+
this.setCurrentActiveTab(domain);
46+
var tabUrl = this.getTab(domain);
47+
if (tabUrl !== undefined)
48+
tabUrl.incCounter();
49+
this.addTimeInterval(domain);
50+
}
51+
}
52+
} else this.closeIntervalForCurrentTab();
2353
}
2454
}

src/tracker.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
import Browser from 'webextension-polyfill';
22
import { isValidPage } from './compositions/valid-page';
33
import { extractHostname } from './compositions/extract-hostname';
4+
import { injectTabsRepository } from './repository/inject-tabs-repository';
5+
import { isInBlackList } from './compositions/black-list';
6+
import { useBadge } from './compositions/set-badge';
7+
import { BadgeColor } from './compositions/types';
48

59
export async function initTracker(){
610
const window = await Browser.windows.getLastFocused({ populate: true });
711
if (window.focused){
812
const activeTab = window.tabs?.find(t => t.active === true);
913
if (isValidPage(activeTab)) {
10-
var activeUrl = extractHostname(activeTab?.url);
11-
var tab = activity.getTab(activeUrl);
12-
if (tab === undefined) {
13-
activity.addTab(activeTab);
14-
}
14+
const activeUrl = extractHostname(activeTab!.url);
15+
16+
if (await isInBlackList(activeUrl)) {
17+
useBadge({ tabId: activeTab!.id!, text: 'n/a', color: BadgeColor.green });
18+
} else{
19+
const repo = await injectTabsRepository();
20+
let tab = repo.getTab(activeUrl);
21+
if (tab === undefined)
22+
tab = await repo.addTab(activeTab!);
1523

16-
if (activity.isInBlackList(activeUrl)) {
17-
chrome.action.setBadgeBackgroundColor({ color: '#fdb8b8' })
18-
chrome.action.setBadgeText({
19-
tabId: activeTab.id,
20-
text: 'n/a'
21-
});
22-
} else {
23-
if (tab !== undefined) {
2424
if (currentTab !== tab.url) {
2525
activity.setCurrentActiveTab(tab.url);
2626
}
27-
chrome.idle.queryState(parseInt(setting_interval_inactivity), function(state) {
27+
const state = await Browser.idle.queryState(parseInt(setting_interval_inactivity));
2828
if (state === 'active') {
2929
mainTRacker(activeUrl, tab, activeTab);
3030
} else checkDOM(state, activeUrl, tab, activeTab);
31-
});
32-
}
3331
}
34-
}
32+
}
3533
} else activity.closeIntervalForCurrentTab();
3634
}

0 commit comments

Comments
 (0)