Comparing version 1.0.4 to 1.0.5
@@ -14,11 +14,10 @@ /** | ||
* @param url the url (where "it" lives) | ||
* @param options the options. {force: Boolean} | ||
* | ||
* @returns {Promise.<{}>} | ||
*/ | ||
exports.grabIt = async (url, options) => { | ||
exports.grabIt = async (url) => { | ||
if(!url) throw new Error("Missing url!"); | ||
try{ | ||
let {og, twitter} = await utils.grabInfo(url, options); | ||
let {og, twitter, favicon, defaults} = await utils.grabInfo(url); | ||
let props = ["title", "description", "image"]; | ||
@@ -29,3 +28,3 @@ let res = {}; | ||
for(let prop of props){ | ||
val = og[prop] || twitter[prop]; | ||
val = og[prop] || twitter[prop] || defaults[prop]; | ||
@@ -35,2 +34,6 @@ if(val) res[prop] = val; | ||
if (favicon) { | ||
res.favicon = favicon; | ||
} | ||
return res; | ||
@@ -37,0 +40,0 @@ } |
{ | ||
"name": "grabity", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Get preview data from a link. Just grab it.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,8 +6,8 @@ ## [![Travis](https://img.shields.io/travis/e-oj/grabity.svg?style=flat-square)](https://travis-ci.org/e-oj/grabity) [![npm](https://img.shields.io/npm/l/grabity.svg?style=flat-square)](https://www.npmjs.com/package/grabity) [![npm](https://img.shields.io/npm/v/grabity.svg?style=flat-square)](https://www.npmjs.com/package/grabity) | ||
[og]: <https://docs.mongodb.com/manual/core/gridfs/> | ||
[og]: <http://ogp.me/> | ||
[twitter]: <https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup> | ||
Grabity looks through [Open Graph](http://ogp.me/) and [Twitter Cards](https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup) markup to get Information about a link. It's functions will return as much data as they can from the markup. If no [og] or [twitter] tags are found, the returned objects will be empty. | ||
Grabity looks through [Open Graph](http://ogp.me/) and [Twitter Cards](https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup) markup to get Information about a link. Its functions will return as much data as they can from the markup. If no [og] or [twitter] tags are found, it will default to the content of the <title> tag and meta description, and if either the title tag or meta description is missing, the returned property will be empty. | ||
## Getting Started: | ||
## Getting Started: | ||
``` | ||
@@ -21,16 +21,17 @@ npm install grabity | ||
let grabity = require("grabity"); | ||
(async () => { | ||
let it = await grabity.grabIt("https://github.com/e-oj/grabity"); | ||
console.log(it); | ||
})(); | ||
``` | ||
``` | ||
Should produce: | ||
``` | ||
{ | ||
{ | ||
title: 'e-oj/grabity', | ||
description: 'grabity - Get preview data from a link. Just grab it! 🎣', | ||
image: 'https://avatars0.githubusercontent.com/u/9700116?s=400&v=4' | ||
image: 'https://avatars0.githubusercontent.com/u/9700116?s=400&v=4', | ||
favicon: 'https://assets-cdn.github.com/favicon.ico' | ||
} | ||
@@ -41,48 +42,49 @@ ``` | ||
### grabity.grabIt(url): Gets a title, description and image from a url | ||
### grabity.grabIt(url): Gets a title, description, image, and favicon from a url | ||
> url (required): url to be used | ||
> returns: object containing title, description and image if found | ||
Gets the [og] or [twitter] title, description and image from a url and returns them in an object. If [og] and [twitter] tags exist for a property, the [og] tag is given preference. The [twitter] tag is selected if an [og] tag does not exist for a property. If there is no tag ([og] or [twitter]) for a property, that property is not included in the returned object. | ||
> returns: object containing title, description, image, and favicon if found | ||
Gets the [og] or [twitter] title, description and image from a url, as well as the favicon, and returns them in an object. If [og] and [twitter] tags exist for a property, the [og] tag is given preference. The [twitter] tag is selected if an [og] tag does not exist for a property. If there is no tag ([og] or [twitter]) for a property, that property is not included in the returned object. | ||
```javascript | ||
let grabity = require("grabity"); | ||
(async () => { | ||
let it = await grabity.grabIt("https://www.flickr.com"); | ||
console.log(it); | ||
})(); | ||
``` | ||
``` | ||
result: | ||
``` | ||
{ | ||
{ | ||
title: 'Flickr, a Yahoo company', | ||
description: 'Flickr is almost certainly the best online photo management and sharing application in the world. Show off your favorite photos and videos to the world, securely and privately show content to your friends and family, or blog the photos and videos you take with a cameraphone.', | ||
image: 'https://farm4.staticflickr.com/3914/15118079089_489aa62638_b.jpg' | ||
image: 'https://farm4.staticflickr.com/3914/15118079089_489aa62638_b.jpg', | ||
favicon: 'https://s.yimg.com/pw/favicon.ico' | ||
} | ||
``` | ||
### grabity.grab(url): Gets all [og] + [twitter] tags and their values | ||
### grabity.grab(url): Gets all [og] + [twitter] tags and their values, as well as the favicon, | ||
> url (required): url to be used | ||
> returns: object containing all found [og] + [twitter] tags and values | ||
Gets all existing [og] and [twitter] tags from the markup and returns them in an object. | ||
> returns: object containing all found [og] + [twitter] tags and values, and favicon | ||
Gets all existing [og] and [twitter] tags, as well as the favicon, from the markup and returns them in an object. | ||
```javascript | ||
let grabity = require("grabity"); | ||
(async () => { | ||
let tags = await grabity.grab("https://www.flickr.com"); | ||
console.log(tags); | ||
})(); | ||
``` | ||
``` | ||
result: | ||
``` | ||
{ | ||
{ | ||
'og:site_name': 'Flickr', | ||
@@ -98,3 +100,4 @@ 'og:updated_time': '2017-11-19T21:29:36.577Z', | ||
'twitter:description': 'Flickr is almost certainly the best online photo management and sharing application in the world. Show off your favorite photos and videos to the world, securely and privately show content to your friends and family, or blog the photos and videos you take with a cameraphone.', | ||
'twitter:image:src': 'https://farm4.staticflickr.com/3914/15118079089_489aa62638_b.jpg' | ||
'twitter:image:src': 'https://farm4.staticflickr.com/3914/15118079089_489aa62638_b.jpg', | ||
favicon: 'https://s.yimg.com/pw/favicon.ico' | ||
} | ||
@@ -101,0 +104,0 @@ |
@@ -15,2 +15,5 @@ /** | ||
exports["twitter:description"] = "View the album on Flickr."; | ||
exports["twitter:image"] = "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg"; | ||
exports["twitter:image"] = "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg"; | ||
exports["title"] = "YE OLDE TEST FILE"; | ||
exports["description"] = "This is a meta description"; |
@@ -17,3 +17,3 @@ /** | ||
describe("#grabIt", () => { | ||
it("should return title, description and image", async () => { | ||
it("should return title, description, image, and favicon", async () => { | ||
let it = await grabity.grabIt(URL); | ||
@@ -24,2 +24,3 @@ | ||
expect(it.image).to.equal(tags["twitter:image"]); | ||
expect(it.favicon).to.equal("https://assets-cdn.github.com/favicon.ico"); | ||
}); | ||
@@ -29,3 +30,3 @@ }); | ||
describe("#grab", () => { | ||
it("should return all 'twitter:' and 'og:' tags", async () => { | ||
it("should return all 'twitter:' and 'og:' tags, favicon, default title tag and meta description", async () => { | ||
let it = await grabity.grab(URL); | ||
@@ -37,4 +38,6 @@ let keys = Object.keys(tags); | ||
} | ||
expect(it.favicon).to.equal("https://assets-cdn.github.com/favicon.ico"); | ||
}); | ||
}); | ||
}); |
@@ -27,2 +27,3 @@ /** | ||
let twitter = {}; | ||
let defaults = {}; | ||
@@ -32,5 +33,16 @@ try{ | ||
let doc = dom.window.document; | ||
let elems = doc.getElementsByTagName("meta"); | ||
let metaEls = doc.getElementsByTagName("meta"); | ||
let linkEls = doc.getElementsByTagName("link"); | ||
let titleTag = doc.getElementsByTagName("title"); | ||
let metaDescTag = doc.querySelector("meta[name='description']"); | ||
for(let meta of elems){ | ||
if(titleTag.length){ | ||
defaults.title = titleTag[0].textContent; | ||
} | ||
if(metaDescTag){ | ||
defaults.description = metaDescTag.content; | ||
} | ||
for(let meta of metaEls){ | ||
filterInfo(meta, OG_PROP, og); | ||
@@ -40,3 +52,5 @@ filterInfo(meta, TWITTER_PROP, twitter); | ||
return {og, twitter}; | ||
let favicon = findFavicon(linkEls); | ||
return {og, twitter, favicon, defaults}; | ||
} | ||
@@ -62,8 +76,21 @@ catch(err){ | ||
let doc = dom.window.document; | ||
let elems = doc.getElementsByTagName("meta"); | ||
let metaEls = doc.getElementsByTagName("meta"); | ||
let linkEls = doc.getElementsByTagName("link"); | ||
let titleTag = doc.getElementsByTagName("title"); | ||
let metaDescTag = doc.querySelector("meta[name='description']"); | ||
for(let meta of elems){ | ||
if(titleTag.length){ | ||
res.title = titleTag[0].textContent; | ||
} | ||
if(metaDescTag){ | ||
res.description = metaDescTag.content; | ||
} | ||
for(let meta of metaEls){ | ||
filterAll(meta, "og:", res); | ||
filterAll(meta, "twitter:", res); | ||
} | ||
res.favicon = findFavicon(linkEls); | ||
@@ -78,2 +105,39 @@ return res; | ||
/** | ||
* Try to find a valid favicon from all of the dom's links | ||
* | ||
* @param links an array of dom 'link' elements | ||
* @returns the href of the favicon, if found | ||
*/ | ||
function findFavicon(links){ | ||
let favicon = ''; | ||
// Prioritise links with rel of 'icon' | ||
for(let link of links){ | ||
if (link.rel === 'icon' && link.href){ | ||
favicon = link.href; | ||
} | ||
} | ||
// Check links with rel including 'icon' | ||
if (!favicon) { | ||
for(let link of links){ | ||
if (link.rel.includes('icon') && link.href){ | ||
favicon = link.href; | ||
} | ||
} | ||
} | ||
// Check links with href containing 'favicon' | ||
if (!favicon) { | ||
for(let link of links){ | ||
if (link.href.includes('favicon')){ | ||
favicon = link.href; | ||
} | ||
} | ||
} | ||
return favicon | ||
} | ||
/** | ||
* Filter for all og and twitter properties | ||
@@ -80,0 +144,0 @@ * in a meta tag |
Sorry, the diff of this file is not supported yet
14773
271
115