@@ -17,41 +17,61 @@ export type AppThunk<ReturnType = void> = ThunkAction<
17
17
export type AppDispatch = ThunkDispatch < RootState , undefined , AnyAction >
18
18
19
19
import Parser = require( "@yang991178/rss-parser" )
20
- const customFields = {
21
- item : [ "thumb" , "image" , [ "content:encoded" , "fullContent" ] ] as Parser . CustomFieldItem [ ]
22
- }
23
-
24
- import ElectronProxyAgent = require( "@yang991178/electron-proxy-agent" )
25
- import { ViewType } from "./models/page"
26
- import { IPartialTheme } from "@fluentui/react"
27
- import { SourceGroup } from "./models/group"
28
- let agent = new ElectronProxyAgent ( remote . getCurrentWebContents ( ) . session )
29
20
export const rssParser = new Parser ( {
30
- customFields : customFields ,
31
- requestOptions : {
32
- agent : agent
21
+ customFields : {
22
+ item : [ "thumb" , "image" , [ "content:encoded" , "fullContent" ] ] as Parser . CustomFieldItem [ ]
33
23
}
34
24
} )
35
25
26
+ export async function parseRSS ( url : string ) {
27
+ try {
28
+ let result = await fetch ( url , { credentials : "omit" } )
29
+ if ( result . ok ) {
30
+ return await rssParser . parseString ( await result . text ( ) )
31
+ } else {
32
+ throw new Error ( result . statusText )
33
+ }
34
+ } catch {
35
+ throw new Error ( "A network error has occured." )
36
+ }
37
+ }
38
+
36
39
export const domParser = new DOMParser ( )
37
40
38
- const favicon = require ( "favicon" )
39
- export function faviconPromise ( url : string ) : Promise < string > {
40
- return new Promise < string > ( ( resolve , reject ) => {
41
- favicon ( url , ( err , icon : string ) => {
42
- if ( err ) reject ( err )
43
- else if ( ! icon ) resolve ( icon )
44
- else {
45
- let parts = icon . split ( "//" )
46
- resolve ( parts [ 0 ] + "//" + parts [ parts . length - 1 ] )
41
+ import Url = require( "url" )
42
+ export async function fetchFavicon ( url : string ) {
43
+ try {
44
+ let result = await fetch ( url , { credentials : "omit" } )
45
+ if ( result . ok ) {
46
+ let html = await result . text ( )
47
+ let dom = domParser . parseFromString ( html , "text/html" )
48
+ let links = dom . getElementsByTagName ( "link" )
49
+ for ( let link of links ) {
50
+ let rel = link . getAttribute ( "rel" )
51
+ if ( ( rel === "icon" || rel === "shortcut icon" ) && link . hasAttribute ( "href" ) ) {
52
+ let href = link . getAttribute ( "href" )
53
+ let parsedUrl = Url . parse ( url )
54
+ if ( href . startsWith ( "//" ) ) return parsedUrl . protocol + href
55
+ else if ( href . startsWith ( "/" ) ) return url + href
56
+ else return href
57
+ }
47
58
}
48
- } )
49
- } )
59
+ }
60
+ url = url + "/favicon.ico"
61
+ result = await fetch ( url , { credentials : "omit" } )
62
+ if ( result . status == 200 && result . headers . has ( "Content-Type" )
63
+ && result . headers . get ( "Content-Type" ) . startsWith ( "image" ) ) {
64
+ return url
65
+ }
66
+ return null
67
+ } catch {
68
+ return null
69
+ }
50
70
}
51
71
52
72
export function htmlDecode ( input : string ) {
53
- var doc = domParser . parseFromString ( input , "text/html" ) ;
54
- return doc . documentElement . textContent ;
73
+ var doc = domParser . parseFromString ( input , "text/html" )
74
+ return doc . documentElement . textContent
55
75
}
56
76
57
77
export function openExternal ( url : string ) {
0 commit comments