Skip to content

Instantly share code, notes, and snippets.

@kentbrew
Created January 31, 2014 22:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentbrew/8744482 to your computer and use it in GitHub Desktop.
Save kentbrew/8744482 to your computer and use it in GitHub Desktop.
Pinterest mega-widget, as seen on http://about.pinterest.com/use
// this snippet requires pinit.js to be loaded with data-pin-build="doBuild"
// turn pin IDs into embedPin links
var render = function (r, el) {
if (r && r.status === 'success' && r.data && r.data.pins) {
var p = r.data.pins;
var parent = el.parentNode;
parent.innerHTML = '';
for (var i = 0; i < p.length; i = i + 1) {
var span = document.createElement('SPAN');
var a = document.createElement('A');
a.setAttribute('data-pin-do', 'embedPin');
a.href = 'http://pinterest.com/pin/' + p[i].id;
span.appendChild(a);
parent.appendChild(span);
}
// fire doBuild, which is onboard pinit.js
doBuild(parent);
}
};
// callback array
var cb = [];
// ping an endpoint
var call = function (slug, el) {
var n = cb.length;
var id = 'cb[' + n + ']';
// create the callback
cb[n] = function (r) {
render(r, el);
};
// make the call
var s = document.createElement('SCRIPT');
s.src = '//widgets.pinterest.com/v3/pidgets/boards/'+ slug + 'pins' + '?callback=' + id;
s.type = 'text/javascript';
s.charset = 'utf-8';
document.body.appendChild(s);
};
// search for links like this:
// <a data-pin-do="embedBigBoard" href="http://pinterest.com/kentbrew/high-five">High Five</a>
var a = document.getElementsByTagName('A');
for (var i = 0; i < a.length; i = i + 1) {
if (a[i].getAttribute('data-pin-do') === 'embedBigBoard') {
var slug = a[i].href.split('.com/')[1];
if (slug) {
// call API
call(slug, a[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment