Skip to content

Commit 171febe

Browse files
committed
Feature: Use icon url of browser (#11)
1 parent 48b69e7 commit 171febe

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

src/background/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ setInterval(() => {
2727
const url = tab.url
2828
if (!url) return
2929
if (isBrowserUrl(url)) return
30-
const host = extractHostname(url)
30+
const host = extractHostname(url).host
3131
if (host) {
3232
hostSet.add(host)
3333
isFocusWindow && tab.active && (focusHost = host)
@@ -63,9 +63,13 @@ chrome.webNavigation.onCompleted.addListener((detail) => {
6363
return
6464
}
6565
chrome.tabs.get(detail.tabId, tab => {
66-
const domain = extractHostname(tab.url)
67-
const iconUrl = tab.favIconUrl
68-
if (!domain || !iconUrl) return
66+
const url = tab.url
67+
if (!url) return
68+
const hostInfo = extractHostname(url)
69+
const domain = hostInfo.host
70+
const protocol = hostInfo.protocol
71+
if (!domain) return
72+
const iconUrl = tab.favIconUrl || `chrome://favicon/${protocol ? protocol + '://' : ''}${domain}`
6973
iconUrlDatabase.put(domain, iconUrl)
7074
})
7175
})

src/manifest.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module.exports = {
3737
permissions: [
3838
'storage',
3939
'tabs',
40-
'webNavigation'
40+
'webNavigation',
41+
'chrome://favicon/*'
4142
],
4243
browser_action: {
4344
default_popup: "static/popup.html",

src/util/constant/url.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { IS_FIREFOX, IS_CHROME, IS_EDGE } from "./environment"
33
/**
44
* @param domain domain
55
* @return Url of domain's favicon
6+
* @deprecated 0.1.7
67
*/
78
export function FAVICON(domain: string): string {
8-
return `https://favicon-1256916044.cos.ap-guangzhou.myqcloud.com/${domain}`
9+
return `chrome://favicon/https://${domain}`
910
}
1011

1112
let homePage = undefined

src/util/pattern.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,30 @@ export function isValidMergeOriginHost(host: string) {
5151
return reg.test(host)
5252
}
5353

54-
export function extractHostname(url: string) {
55-
let hostname: string;
54+
export type HostInfo = {
55+
/**
56+
* Including port
57+
*/
58+
host: string
59+
protocol: string
60+
}
61+
62+
export function extractHostname(url: string): HostInfo {
63+
let host: string
64+
let protocol: string
5665

57-
if (url.indexOf("//") > -1) {
58-
hostname = url.split('/')[2];
66+
const indexOfDoubleSlashes = url.indexOf("//")
67+
if (indexOfDoubleSlashes > -1) {
68+
const splited = url.split('/')
69+
host = splited[2]
70+
protocol = splited[0]
71+
protocol = protocol.substr(0, protocol.length - 1)
5972
} else {
60-
hostname = url.split('/')[0];
73+
host = url.split('/')[0]
74+
protocol = ''
6175
}
62-
hostname = hostname.split('?')[0];
76+
host = host.split('?')[0]
77+
78+
return { host, protocol }
79+
}
6380

64-
return hostname;
65-
}

test/util/pattern.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ test('merge domain origin', () => {
3535
})
3636

3737
test("url", () => {
38-
expect(extractHostname('https://www.baidu.com?1231=123')).toEqual('www.baidu.com')
39-
expect(extractHostname('http://localhost:8087?1231=123')).toEqual('localhost:8087')
40-
expect(extractHostname('http://localhost:8087/')).toEqual('localhost:8087')
41-
expect(extractHostname('http://localhost:8087/?123=123')).toEqual('localhost:8087')
38+
expect(extractHostname('https://www.baidu.com?1231=123')).toEqual({ host: 'www.baidu.com', protocol: 'https' })
39+
expect(extractHostname('http://localhost:8087?1231=123')).toEqual({ host: 'localhost:8087', protocol: 'http' })
40+
expect(extractHostname('http://localhost:8087/')).toEqual({ host: 'localhost:8087', protocol: 'http' })
41+
expect(extractHostname('http://localhost:8087/?123=123')).toEqual({ host: 'localhost:8087', protocol: 'http' })
42+
expect(extractHostname('localhost:8087/?123=123')).toEqual({ host: 'localhost:8087', protocol: '' })
4243
})

0 commit comments

Comments
 (0)