favicon-server/api/grabber/index.js

57 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-02-13 08:49:10 +00:00
const page = require('./page');
const URL = require('url').URL;
const normalizeUrl = require('normalize-url');
module.exports = {
default: (domain, options, done) => {
const url = `http://${domain}/`;
page(url, (err, icons, baseUrl) => {
if (err) return done(err);
icons.forEach((icon) => {
const url = new URL(icon.src, baseUrl);
if (options.normalizeUrl) {
icon.src = normalizeUrl(url.href, {
removeQueryParameters: [/.+/],
stripWWW: false,
});
} else {
icon.src = url.href;
}
});
const data = {
domain,
icons,
};
return done(null, data);
});
},
allurl: (url, options, done) => {
page(url, (err, icons, baseUrl) => {
if (err) return done(err);
icons.forEach((icon) => {
const url = new URL(icon.src, baseUrl);
if (options.normalizeUrl) {
icon.src = normalizeUrl(url.href, {
removeQueryParameters: [/.+/],
stripWWW: false,
});
} else {
icon.src = url.href;
}
});
const data = {
url,
icons,
};
return done(null, data);
});
}
}