Ecco cosa ho fatto.
Prima ho guardato pinit.js e ho deciso che sostituisce i tag di ancoraggio appositamente contrassegnati con IFRAME. Ho pensato che potevo scrivere la logica javascript per ottenere il nome host usato dall'attributo src sugli iframe generati.
Quindi, ho inserito il markup in base ai normali consigli di pinterest, ma ho inserito il tag di ancoraggio in un div invisibile.
<div id='dummy' style='display:none;'>
<a href="http://pinterest.com/pin/create/button/?
url=http%3A%2F%2Fpage%2Furl
&media=http%3A%2F%2Fimage%2Furl"
class="pin-it-button" count-layout="horizontal"></a>
</div>
<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js">
</script>
Poi, subito dopo, ho inserito uno script per slurp il nome host per il pinterest CDN, dal iframe iniettato.
//
// pint-reverse.js
//
// logic to reverse-engineer pinterest buttons.
//
// The standard javascript module from pinterest replaces links to
// http://pinterest.com/create/button with links to some odd-looking
// url based at cloudfront.net. It also normalizes the URLs.
//
// Not sure why they went through all the trouble. It does not work for
// a dynamic page where new links get inserted. The pint.js code
// assumes a static page, and is designed to run "once" at page creation
// time.
//
// This module spelunks the changes made by that script and
// attempts to replicate it for dynamically-generated buttons.
//
pinterestOptions = {};
(function(obj){
function spelunkPinterestIframe() {
var iframes = document.getElementsByTagName('iframe'),
k = [], iframe, i, L1 = iframes.length, src, split, L2;
for (i=0; i<L1; i++) {
k.push(iframes[i]);
}
do {
iframe = k.pop();
src = iframe.attributes.getNamedItem('src');
if (src !== null) {
split = src.value.split('/');
L2 = split.length;
obj.host = split[L2 - 2];
obj.script = split[L2 - 1].split('?')[0];
//iframe.parentNode.removeChild(iframe);
}
} while (k.length>0);
}
spelunkPinterestIframe();
}(pinterestOptions));
Poi,
function getPinMarkup(photoName, description) {
var loc = document.location,
pathParts = loc.pathname.split('/'),
pageUri = loc.protocol + '//' + loc.hostname + loc.pathname,
href = '/' + pathToImages + photoName,
basePath = (pathParts.length == 3)?'/'+pathParts[1]:'',
mediaUri = loc.protocol+'//'+loc.hostname+basePath+href,
pinMarkup;
description = description || null;
pinMarkup = '<iframe class="pin-it-button" ' + 'scrolling="no" ' +
'src="//' + pinterestOptions.host + '/' + pinterestOptions.script +
'?url=' + encodeURIComponent(pageUri) +
'&media=' + encodeURIComponent(mediaUri);
if (description === null) {
description = 'Insert standard description here';
}
else {
description = 'My site - ' + description;
}
pinMarkup += '&description=' + encodeURIComponent(description);
pinMarkup += '&title=' + encodeURIComponent("Pin this " + tagType);
pinMarkup += '&layout=horizontal&count=1">';
pinMarkup += '</iframe>';
return pinMarkup;
}
E poi usarlo da jQuery in questo modo:
var pinMarkup = getPinMarkup("snap1.jpg", "Something clever here");
$('#pagePin').empty(); // a div...
$('#pagePin').append(pinMarkup);
È fantastico! Come lo sapevi? – Mikhail
Questo è un modo molto più diretto per realizzare ciò che la risposta accettata fa. Ottimo lavoro, Styx! – creativetim
Funziona, ma cose per essere sicuro di: ** 1. ** Lo script di Pinterest deve essere chiamato così prima nella pagina. Se è stato caricato senza di esso, un'altra istanza di caricamento potrebbe non rendere esposta la funzione di creazione. ** 2. ** Lo script Pinterest rimuove questo tag script e inserisce invece pinit_main.js. Durante il processo controlla il ** ** tag di script pinit.js se possiede l'attributo data-pin-build e, in caso affermativo, lo utilizza. ** 3. ** Il pinit_main.js non compresso è [qui] (https://github.com/pinterest/widgets/blob/190f65d11f7b33d7b65fa5a8aafb2c71bd01f4e6/pinit.js#L976) – Firsh