Skip to content

Commit f4ec75f

Browse files
committed
Update favicon after loading page
1 parent 7ef6ee5 commit f4ec75f

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/background.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import Browser from "webextension-polyfill";
22
import { initTracker } from "./tracker";
33
import { logger } from "./compositions/logger";
4+
import { useFavicon } from "./compositions/update-favicon";
45

56
logger.log("Start background script");
67

78
Browser.runtime.onInstalled.addListener((details) => {
89
logger.log("Extension installed:", details);
910
});
1011

12+
Browser.webNavigation.onCompleted.addListener(async function(details) {
13+
await useFavicon(details.tabId);
14+
});
15+
1116
initTracker();

src/compositions/update-favicon.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Browser from 'webextension-polyfill';
2+
import { isValidPage } from './valid-page';
3+
import { extractHostname } from './extract-hostname';
4+
import { injectTabsRepository } from '../repository/inject-tabs-repository';
5+
6+
export async function useFavicon(tabId: number): Promise<void>{
7+
const repo = await injectTabsRepository();
8+
const tab = await Browser.tabs.get(tabId);
9+
if (isValidPage(tab)) {
10+
const activeDomain = extractHostname(tab?.url);
11+
let tabFromStoage = repo.getTab(activeDomain);
12+
if (tabFromStoage != undefined && tabFromStoage.favicon != null){
13+
tabFromStoage.setFavicon(tab?.favIconUrl);
14+
}
15+
}
16+
}

src/entity/tab.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ export class Tab implements ISerializable<Tab> {
88
counter: number = 0
99
days: TabDay[] = [];
1010

11-
init(url: string, favicon: string){
11+
init(url: string){
1212
this.url = url;
13-
this.favicon = favicon;
1413
}
1514

1615
incSummaryTime() :void {
@@ -48,6 +47,13 @@ export class Tab implements ISerializable<Tab> {
4847

4948
return this;
5049
}
50+
51+
setFavicon(favicon: string | undefined){
52+
if (favicon != undefined)
53+
this.favicon = favicon;
54+
else
55+
this.favicon = "chrome://favicon/" + this.url;
56+
}
5157
}
5258

5359
export class TabDay implements ISerializable<TabDay> {

src/repository/tabs-repository.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,11 @@ export class TabsRepository implements ITabsRepository {
2929
const tabFromStorage = this.getTab(domain);
3030
const isInBlackListFlag = await isInBlackList(domain);
3131

32-
if (!isInBlackListFlag) {
33-
if (!tabFromStorage) {
34-
if (!favicon) {
35-
favicon = "chrome://favicon/" + domain;
36-
}
32+
if (!isInBlackListFlag && !tabFromStorage) {
3733
const newTab = new Tab();
38-
newTab.init(domain, favicon);
34+
newTab.init(domain);
3935
this.tabs.push(newTab);
4036
return newTab;
41-
}
4237
}
4338

4439
return undefined;

0 commit comments

Comments
 (0)