Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
zongyz committed Apr 5, 2018
1 parent 23259f8 commit f864be0
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 11 deletions.
10 changes: 6 additions & 4 deletions lib/getdom.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
'use strict'
const request = require('./request')
const cheerio = require('cheerio')

const { parse: urlParse } = require('url')
module.exports = async url=>{
return new Promise(async (resolve, reject)=>{
try {
let response = await request(url),
options = response.request._redirectable._options,
html = response.data,
$ = cheerio.load(html,{
lowerCaseTags: true,
lowerCaseAttributeNames: true,
})
$.baseUrl = `${options.protocol}//${options.hostname}`
$.url = response.request.res.responseUrl
$.baseUrl = urlParse($.url).protocol+'//'+urlParse($.url).hostname
resolve($)
} catch (error) {
// console.log(error)
let $ = cheerio.load("")
$.baseUrl = url
$.url = url
$.baseUrl = urlParse($.url).protocol+'//'+urlParse($.url).hostname
resolve($)
}
})
Expand Down
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = async url => {
let icons = []
return new Promise(async(resolve, reject) => {
if (!url) return reject({})
if (!urlParse(url).protocol) url = `http://${url}/`
if (!urlParse(url).protocol) url = `http://${url}`
let $ = await getdom(url),
tagIcon = await iconByHtml($),
manifestIcon = await iconByManifest($),
Expand All @@ -23,7 +23,9 @@ module.exports = async url => {
for (let i in icons) icons[i].rank = rank(icons[i])
icons.sort((a, b) => (b.rank - a.rank))
resolve({
url: $.baseUrl,
url: $.url,
baseUrl: $.baseUrl,
originUrl: url,
icons
})
})
Expand Down
2 changes: 1 addition & 1 deletion lib/origin/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = async baseurl=>{
})
}
} catch (error) {

// console.log(error);
}
resolve(icons)
})
Expand Down
2 changes: 1 addition & 1 deletion lib/origin/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = async $=>{
}
if(src && src !== '#'){
icons.push({
src:new URL(src, $.baseUrl).href,
src:new URL(src, $.url).href,
sizes,
type,
origin:cheerio.html(el)
Expand Down
4 changes: 2 additions & 2 deletions lib/origin/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ module.exports = async $=>{
let href = $('link[rel="manifest" i]', 'head').attr('href')
if(!href) return resolve(icons)

let url = new URL(href, $.baseUrl).href
let url = new URL(href, $.url).href
try {
let response = await request(url)
if(response.status == 200 && response.data && Array.isArray(response.data.icons)){
icons = response.data.icons.map(
({src = '', sizes = '', type = ''})=>({src:new URL(src, $.baseUrl).href, sizes, type, origin:url})
({src = '', sizes = '', type = ''})=>({src:new URL(src, url).href, sizes, type, origin:url})
) || []
}
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const axios = require('axios').create({
headers: {
// 'Accept': '*/*',
'Accept-Encoding': 'gzip, identity',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
},
// withCredentials: true,
maxRedirects: 5,
timeout: 3000
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "get-website-favicon",
"version": "0.0.6",
"version": "0.0.7",
"description": "get the favicons url from any website",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f864be0

Please sign in to comment.