Last active
June 18, 2017 12:31
-
-
Save panzi/9978447 to your computer and use it in GitHub Desktop.
Static share links template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// expand {{PATTERNS}} | |
// You can do this serverside. I just did it this way for demonstration purposes. | |
(function () { | |
"use strict"; | |
var shares = document.querySelectorAll("ul.share > li > a"); | |
var keywords = document.querySelector('meta[name=keywords]').getAttribute("content"); | |
var description = document.querySelector('meta[name=description]').getAttribute("content"); | |
var params = { | |
URL: encodeURIComponent(document.querySelector('link[rel=canonical]').getAttribute("href")), | |
TITLE: encodeURIComponent(document.title), | |
HASHTAG: encodeURIComponent(keywords.split(",")[0]), | |
TAGS: encodeURIComponent(keywords), | |
SUMMARY: encodeURIComponent(description) | |
}; | |
for (var i = 0; i < shares.length; ++ i) { | |
var share = shares[i]; | |
var url = share.getAttribute("data-popup-url"); | |
share.setAttribute("href",expand(share.getAttribute("href"),params)); | |
if (url) { | |
share.setAttribute("data-popup-url",expand(url,params)); | |
} | |
} | |
function expand (href,params) { | |
return href.replace(/{{([^{}]+)}}/g, function (match, key) { | |
return params.hasOwnProperty(key) ? params[key] : match; | |
}); | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="javascript:void((function(d){var e=d.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);d.body.appendChild(e)})(document));">Pinterest</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some services provide a share form that can conveniently be displayed in a popup. | |
// This event handler opens such a popup when attached to the click event of a share link: | |
function sharePopup (event) { | |
"use strict"; | |
// only open popup when clicked with left mouse button | |
// (middle click should still open the link in a new tab and | |
// right click should still open the context menu) | |
if ('buttons' in event) { | |
if (!(event.buttons & 1)) { | |
return true; | |
} | |
} | |
else if (event.button !== 0) { | |
return true; | |
} | |
// gather popup parameters | |
var href = this.getAttribute("data-popup-url") || this.href; | |
var params = { | |
menubar: "no", | |
location: "no", | |
toolbar: "no", | |
status: "no", | |
resizable: "yes", | |
width: "640", | |
height: "480" | |
}; | |
for (var name in params) { | |
var value = this.getAttribute("data-popup-"+name); | |
if (value) { | |
params[name] = value; | |
} | |
} | |
// center popup window | |
var width = parseInt(params.width,10); | |
var height = parseInt(params.height,10); | |
params.top = Math.max(0, Math.round((screen.height - height) * 0.5)); | |
params.left = Math.max(0, Math.round((screen.width - width) * 0.5)); | |
var spec = []; | |
for (var name in params) { | |
spec.push(name+"="+params[name]); | |
} | |
// open window | |
window.open(href,'_blank',spec.join(",")); | |
// prevent navigation to the share form | |
if ('preventDefault' in event) { | |
event.preventDefault(); | |
} | |
else { | |
event.returnValue = false; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment