forked from LudicrousDevelopment/Ludi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavicon.mjs
38 lines (31 loc) · 1.5 KB
/
favicon.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {parse, serialize} from 'parse5';
import walk from 'walk-parse5';
import follow from 'follow-redirects';
const { https } = follow;
function getFavicons(host) {
return new Promise((resolve, reject) => {
var data = []
https.request('https://'+host.replace('https://','').replace('https:/',''), function(response) {
response.on('data',e=>data.push(e)).on('end', function() {
var text = (Buffer.concat(data).toString())
var parsed = parse(text)
var toreturn = []
walk(parsed, function(node) {
if (node.tagName=='link'&&(node.attrs.find(e=>e.name=='rel')||{}).value=='icon') toreturn.push({src: 'https://'+host.replace('https://','').replace('https:/','')+(node.attrs.find(e=>e.name=='href')||{}).value.replace(host,'').replace('https://','')})
//console.log(node.tagName=='meta'&&(node.attrs.find(e=>e.name=='itemprop')||{}).value=='image'))
if (node.tagName=='meta'&&(node.attrs.find(e=>e.name=='itemprop')||{}).value=='image') toreturn.push({src: 'https://'+host.replace('https://','').replace('https:/','')+(node.attrs.find(e=>e.name=='content')||{}).value.replace(host,'').replace('https://','')})
})
return resolve({
icons: [
...toreturn
]
})
})
}).on('error',()=>{}).end()
})
}
export default function(host, req, res) {
getFavicons(host).then(data=>{
res.end((data.icons[0]||{src:req.headers['x-forwarded-proto']+'://'+req.headers['host']+'/ico/dark.ico'}).src)
})
}