How can i rerender Pinterest's Pin It button?
学习当地常用口语可以快速融入当地文化,如问路:'Excuse me, how can I get to...?' #生活知识# #旅游生活# #旅游语言学习#
Here's what I did.
First I looked at pinit.js, and determined that it replaces specially-marked anchor tags with IFRAMEs. I figured that I could write javascript logic to get the hostname used by the src attribute on the generated iframes.
So, I inserted markup according to the normal recommendations by pinterest, but I put the anchor tag into an invisible div.
<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>
Then, immediately after that, I inserted a script to slurp up the hostname for the pinterest CDN, from the injected iframe.
// // 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));
Then,
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; }
And then use it from jQuery like this:
var pinMarkup = getPinMarkup("snap1.jpg", "Something clever here"); $('#pagePin').empty(); // a div... $('#pagePin').append(pinMarkup);
网址:How can i rerender Pinterest's Pin It button? https://klqsh.com/news/view/205434
相关内容
How can I install clangPizza Cornicione – What It Is and How to Make It
How can I locate a product on Costco.com?
How to extend the task bar to multiple screens.
How do i set it up to have sound out of multiple monitors on windows 11?
How can I see what products are available on Costco.com?
How can I access the /android
How to share on Pinterest
How do I track my order on Costco.com?
How to install Firefox directly from Mozilla with `apt`?