Skip to content

Instantly share code, notes, and snippets.

@josue
Created June 30, 2011 20:41
Show Gist options
  • Save josue/1057186 to your computer and use it in GitHub Desktop.
Save josue/1057186 to your computer and use it in GitHub Desktop.
Collects OpenGraph tag data from markup into an object list.
/*
* Author: Josue Rodriguez <josue@josuerodriguez.com>
* Created: 2011-06-30
* License: MIT
* Description: Collects OpenGraph tag data from markup into an object list.
*/
var OpenGraph = function(opts){
var opt = opts || {};
var context = opt.DOM || typeof opts==="string" && opts || document;
var types = opt.types || "album|book|drink|food|game|movie|product|song|tv_show|article|blog|website";
var A = {};
var $ = window.jQuery || null;
A.parse = function(context){
if(!$) { return this; }
A.list = {};
var metas = $("meta[property^='og:']",context);
var og_type = metas.filter("[property='og:type']").attr("content");
var regexp_types = new RegExp(types.toString(),"gim");
if(regexp_types.test(og_type)) {
metas.each(function(i,e){
var k = $(e).attr("property").replace("og:","");
A.list[k] = $(e).attr("content");
});
}
return this;
};
A.parse(context);
return A;
};
// ===== USAGE =====
$(function(){
var Product = {};
// checks OpenGraph support and collects data if type is found (ie: movie, product, etc):
var OG = (new OpenGraph).list;
if(OG.type) {
Product.title = OG.title
Product.image = OG.image;
}
// collect only specific type tag data (ie: game)
var Game = ( new OpenGraph({ types: "game" }) ).list;
//.... etc....
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment