@@ -6,58 +6,69 @@ import { fetchAsBrowser } from "./fetch-page-html";
6
6
import { isElement } from "./is-element" ;
7
7
8
8
export async function fetchPageIcon ( src : URL , srcHast : Root ) : Promise < URL > {
9
- // <link rel="manifest" href="/manifest.json">
10
- const manifestPath : Element | undefined = find ( srcHast , ( node ) => {
11
- return isElement ( node ) && node . tagName === "link" && String ( node . properties . rel ) . includes ( "manifest" ) ;
12
- } ) ;
13
-
14
9
let iconHref : URL | undefined ;
15
10
16
- if ( manifestPath ?. properties ?. href ) {
17
- console . log ( "Found manifest" , manifestPath . properties . href , "for" , src . href ) ;
11
+ // <link rel="shortcut icon" type="image/png" href="https://example.com/img.png">
12
+ // <link rel="apple-touch-icon" sizes="96x96" href="/icons/icon-96x96.png?v=2a">
13
+ const iconExtensions = [ ".svg" , ".png" , ".jpg" , ".jpeg" ] ;
14
+ const favicon : Element | undefined = find ( srcHast , ( node ) => {
15
+ if ( ! isElement ( node ) || node . tagName !== "link" ) return false ;
18
16
19
- // `/manifest.json`
20
- const manifestRelativeURL = String ( manifestPath . properties . href ) ;
21
- const fullManifestURL = new URL ( manifestRelativeURL , src ) ;
17
+ const rel = ( node as Element ) . properties ?. rel ?. toString ( ) ?? "" ;
18
+ const href = ( node as Element ) . properties ?. href ?. toString ( ) ?? "" ;
19
+ let hrefUrl : URL | undefined ;
20
+ try {
21
+ // Need to check the file extension through URL.pathname to omit query params
22
+ hrefUrl = new URL ( href , src ) ;
23
+ } catch ( e ) {
24
+ // do nothing
25
+ }
22
26
23
- const manifest = await fetchAsBrowser ( fullManifestURL )
24
- . then ( ( r ) => r . json ( ) )
25
- . catch ( ( ) => null ) ;
27
+ const extname = path . extname ( hrefUrl ?. pathname ?? "" ) ;
28
+ return rel . includes ( "icon" ) && iconExtensions . includes ( extname ) ;
29
+ } ) ;
26
30
27
- if ( manifest ) {
28
- const largestIcon = getLargestManifestIcon ( manifest ) ;
29
- if ( largestIcon ?. icon ) {
30
- console . log ( "Found iconHref from manifest.json:" , largestIcon . icon . src ) ;
31
- iconHref = new URL ( largestIcon . icon . src , src . origin ) ;
32
- }
33
- }
31
+ if ( favicon ?. properties ?. href ) {
32
+ console . log ( "Found iconHref from <link> tags:" , favicon . properties . href ) ;
33
+ iconHref = new URL ( favicon . properties . href . toString ( ) , src ) ;
34
34
}
35
35
36
36
if ( ! iconHref ) {
37
- // <link rel="shortcut icon" type="image/png" href="https://example.com/img.png">
38
- // <link rel="apple-touch-icon" sizes="96x96" href="/icons/icon-96x96.png?v=2a">
39
- const iconExtensions = [ ".svg" , ".png" , ".jpg" , ".jpeg" ] ;
40
- const favicon : Element | undefined = find ( srcHast , ( node ) => {
41
- if ( ! isElement ( node ) || node . tagName !== "link" )
42
- return false ;
37
+ // <link rel="manifest" href="/manifest.json">
38
+ const manifestPath : Element | undefined = find ( srcHast , ( node ) => {
39
+ return (
40
+ isElement ( node ) &&
41
+ node . tagName === "link" &&
42
+ String ( node . properties . rel ) . includes ( "manifest" )
43
+ ) ;
44
+ } ) ;
43
45
44
- const rel = ( node as Element ) . properties ?. rel ?. toString ( ) ?? "" ;
45
- const href = ( node as Element ) . properties ?. href ?. toString ( ) ?? "" ;
46
- let hrefUrl : URL | undefined ;
47
- try {
48
- // Need to check the file extension through URL.pathname to omit query params
49
- hrefUrl = new URL ( href , src ) ;
50
- } catch ( e ) {
51
- // do nothing
52
- }
46
+ if ( manifestPath ?. properties ?. href ) {
47
+ console . log (
48
+ "Found manifest" ,
49
+ manifestPath . properties . href ,
50
+ "for" ,
51
+ src . href ,
52
+ ) ;
53
53
54
- const extname = path . extname ( hrefUrl ?. pathname ?? "" ) ;
55
- return rel . includes ( "icon" ) && iconExtensions . includes ( extname ) ;
56
- } ) ;
54
+ // `/manifest.json`
55
+ const manifestRelativeURL = String ( manifestPath . properties . href ) ;
56
+ const fullManifestURL = new URL ( manifestRelativeURL , src ) ;
57
57
58
- if ( favicon ?. properties ?. href ) {
59
- console . log ( "Found iconHref from <link> tags:" , favicon . properties . href ) ;
60
- iconHref = new URL ( favicon . properties . href . toString ( ) , src ) ;
58
+ const manifest = await fetchAsBrowser ( fullManifestURL )
59
+ . then ( ( r ) => r . json ( ) )
60
+ . catch ( ( ) => null ) ;
61
+
62
+ if ( manifest ) {
63
+ const largestIcon = getLargestManifestIcon ( manifest ) ;
64
+ if ( largestIcon ?. icon ) {
65
+ console . log (
66
+ "Found iconHref from manifest.json:" ,
67
+ largestIcon . icon . src ,
68
+ ) ;
69
+ iconHref = new URL ( largestIcon . icon . src , src . origin ) ;
70
+ }
71
+ }
61
72
}
62
73
}
63
74
0 commit comments