From 6ef5a6338fd4edc3d9e438f6119046bd4616df9c Mon Sep 17 00:00:00 2001 From: yizhi <946185759@qq.com> Date: Sun, 13 Jan 2019 08:29:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- dist/favicon.js | 114 ------------------------------------------------ dist/index.js | 5 --- package.json | 2 +- src/favicon.ts | 4 +- 5 files changed, 5 insertions(+), 123 deletions(-) delete mode 100644 dist/favicon.js delete mode 100644 dist/index.js diff --git a/.gitignore b/.gitignore index 572406bf..87a7db15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules -package-lock.json \ No newline at end of file +package-lock.json +/dist \ No newline at end of file diff --git a/dist/favicon.js b/dist/favicon.js deleted file mode 100644 index 155711c4..00000000 --- a/dist/favicon.js +++ /dev/null @@ -1,114 +0,0 @@ -"use strict"; -/** - * favicon的获取顺序为: - * 1、 拉取指定网址,并检测html代码中header->link标签是否存在favicon,如果存在则使用它们。 - * 2、当第一步不满足的时候回进行猜测,一般猜测 favicon.ico、 favicon.png、 favicon.svg、favicon.gif,如果存在并且mime类型是图片则返回。 - * 3、如果上面两步都不满足,则表示未找到favicon - */ -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const url_1 = __importDefault(require("url")); -const request_1 = __importDefault(require("request")); -//处理link标签 -function parseLinkTag(uri, link, types) { - //内容读取 - link = ' ' + link.trim().replace(/(^<\s*link\s*)|(\s*\/?>$)/g, ''); - let match = link.match(/\srel\s*=\s*"([\s\S]+?)"/); - if (!match) - return null; - if (!match[1].trim().split(/\s+/).filter(t => types.indexOf(t) >= 0).length) - return null; - match = link.match(/\shref\s*=\s*"([\s\S]*?)"/); - if (!match) - return null; - let href = match[1]; - match = link.match(/\ssizes\s*=\s*"(\d+)[xX](\d+)"/); - const size = match ? { width: parseInt(match[1]), height: parseInt(match[2]) } : undefined; - //连接处理 - if (!(/https?:\/\//.test(href))) { - let { protocol, host, pathname } = url_1.default.parse(uri); - protocol = protocol || 'https:'; - if (/\/\//.test(href)) - href = protocol + href; - else { - if (!host) - return null; - if (href[0] == '/') - href = protocol + '//' + host + href; - else - href = protocol + '//' + host + (pathname || '').replace(/\/$/, '') + '/' + href; - } - } - return { url: href, size }; -} -//从HTML中获取faviconnpm install favicon -async function getFavIconFromHtml(uri, option) { - return new Promise(resolve => { - request_1.default.get(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body) => { - if (err || !body || !body.match) - resolve([]); - //获取所有link标签 - const match = body.match(//g); - if (!match) - return resolve([]); - const favicons = match.map(link => parseLinkTag(uri, link, option.types)).filter(res => !!res); - resolve(favicons); - }); - }); -} -//猜测favicon -async function guessFavIcon(uri, option) { - //用于测试请求一个地址,请求成功返回地址否则返回空 - const fetch = (uri) => new Promise(resolve => { - request_1.default(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body) => { - if (err || !body || !res.headers['content-type']) - resolve(null); - if (/^image\//.test(res.headers['content-type'])) - resolve(uri); - else - resolve(null); - }); - }); - //猜测某个网址 - const guessUri = async (uri) => { - const urls = await Promise.all(['ico', 'png', 'gif', 'svg'].map(suf => fetch(uri + '/favicon.' + suf))); - return urls.filter(s => !!s).map(s => ({ url: s })); - }; - const { protocol, port, host } = url_1.default.parse(uri); - if (!host) - return []; - //如果没有提供协议,则猜测http和https - if (!protocol) { - const [f1, f2] = await Promise.all([ - guessUri(`https://${host}`), - guessUri(`http://${host}`), - ]); - return [...f1, ...f2]; - } - //否则猜测给定的地址 - else - return guessUri(`${protocol}//${host}`); -} -/** - * get favicon from given url - * @param uri URL - * @param option Optional - */ -async function favicon(uri, option) { - //选项初始化 - option = option || {}; - option.types = option.types || ['icon']; - option.timeout = option.timeout || 5000; - option.headers = { - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36', - ...option.headers || {}, - }; - //从网页获取数据 - let icons = await getFavIconFromHtml(uri, option); - if (!icons || !icons.length) - icons = await guessFavIcon(uri, option); - return icons; -} -exports.favicon = favicon; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 47d29117..00000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const favicon_1 = require("./favicon"); -exports.favicon = favicon_1.favicon; -module.exports = { favicon: favicon_1.favicon }; diff --git a/package.json b/package.json index 52910a2d..a3196036 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "favicon-trap", - "version": "1.0.2", + "version": "1.0.3", "description": "", "main": "dist/index.js", "types": "typing/index.d.ts", diff --git a/src/favicon.ts b/src/favicon.ts index 029ca815..51be48d5 100644 --- a/src/favicon.ts +++ b/src/favicon.ts @@ -60,7 +60,7 @@ function parseLinkTag(uri: string, link: string, types: Array): FavIcon async function getFavIconFromHtml(uri: string, option: FaviconOption): Promise> { return new Promise>(resolve => { request.get(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body: string) => { - if (err || !body || !body.match) resolve([]) + if (err || !body || !body.match) return resolve([]) //获取所有link标签 const match = body.match(//g) if (!match) return resolve([]) @@ -75,7 +75,7 @@ async function guessFavIcon(uri: string, option: FaviconOption): Promise new Promise(resolve => { request(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body) => { - if (err || !body || !res.headers['content-type']) resolve(null) + if (err || !body || !res.headers['content-type']) return resolve(null) if (/^image\//.test(res.headers['content-type']!)) resolve(uri) else resolve(null) })