Skip to content

Instantly share code, notes, and snippets.

@chrisdigital
Last active December 12, 2015 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdigital/4740357 to your computer and use it in GitHub Desktop.
Save chrisdigital/4740357 to your computer and use it in GitHub Desktop.
Grab image description for Pinterest. Extending functionality of "Pinterest Image Pin" Wordpress plugin... http://wordpress.org/extend/plugins/pinterest-image-pin/
/* Now wrapped in an if statement, checking for information to tag onto url even exists in current dom.
It also now encodes parentheses.
It was failing silently on some pages and wasn't sure why but it's fixed now.
I think the pinterest plugin uses multiple links and so I'm only targeting the .pin-it-button class link specfcally now.
I also chained a bunch of .txt stripping (.replace()) events together */
if(jQuery("div.specifications").length > 0){
//target the anchor/image link in the selector we're focused on...
var pinTarget = jQuery(".sdjpip_linkbox a.pin-it-button");
var pinThis = pinTarget.attr("href");
//grab the html of the dom element associated with description/ child ul of .specifications
var nodes = jQuery('.specifications ul')[0].childNodes;
//grab relevant text and strip spaces at the beginning and ending
var nodesText = jQuery.trim(jQuery(nodes).text());
console.log(nodesText);
//Strips unnecessary spaces and tosses any carriage returns, converts to one line//replace parentheses
//strips areas with multiple spaces and replaces with encoded comma and space(targeted between li nodes)
//Strips any left over spaces and replaces with urlencoding for them
//encode colons and adds back a space after colon
var buildResult = nodesText.replace(/\s/g, " ").replace(/\(\s*/g,"%28").replace(/\)\s*/g,"%29").replace(/\s{2,}/g,'%2C%20').replace(/\s/g, "%20").replace(/:\s*/g, "%3a%20");
//Grab what the breadcrumb says, some times page title is less relevant than breadcrumb text
//Strips in itemname witin breadcrumb, any left over spaces and replaces with urlencoding for them
//replace parentheses and anyspaces that slip through
var itemName = (jQuery(".trail-end").text()).replace(/\s/g, "%20").replace(/\(\s*/g,"%28").replace(/\)\s*/g,"%29");
//sticks description url variable, and itemname from breadcrumb at the beginning of string
pinThis = pinThis+ "&description=" + itemName + "%20" + buildResult;
pinThis= pinThis.replace(/\s/g, "%20");
pinTarget.attr("href", pinThis);
//pinTarget.attr("href", "sandwiches");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment