Skip to content

Instantly share code, notes, and snippets.

@jptrsn
Last active December 10, 2015 01:42
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 jptrsn/355510221012442dd8e4 to your computer and use it in GitHub Desktop.
Save jptrsn/355510221012442dd8e4 to your computer and use it in GitHub Desktop.
Google Sites gadget to include a tweet button
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Tweet Button" title_url="http://www.opensourceteacher.ca/apps-script/tweet-button"
description="Add a button to let your website viewers tweet right from your pages."
thumbnail="http://www.gstatic.com/sites-gadgets/common/images/sites-icon-gadget-thumb.png"
screenshot="http://www.gstatic.com/sites-gadgets/common/images/sites-icon-gadget-ss.png"
height="23" width="65" author="Open Source Teacher">
</ModulePrefs>
<UserPref name="handle" display_name="Twitter Handle (no @ symbol)" required="true"/>
<UserPref name="message" display_name="Tweet content text" required="true"/>
<UserPref name="hashtag" display_name="Hashtag (optional)" required="false"/>
<Content type="html">
<![CDATA[
<style>
body, img a,a img {
background-color: transparent;
}
</style>
<a id="tweetButton" href="https://twitter.com/share" class="twitter-share-button" data-text="This tweet button courtesy of Open Source Teacher" data-url="http://www.opensourceteacher.ca" data-via="jpetersen02" data-hashtags="">Tweet</a>
<script type="text/javascript">
gadgets.util.registerOnLoadHandler(doRender);
function doRender() {
!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
if(!d.getElementById(id)){
js=d.createElement(s);
js.id=id;
js.src=p+"://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);
}
var canonical = document.referrer;
document.getElementById("tweetButton").setAttribute("data-url",canonical);
var prefs = new gadgets.Prefs();
var text = prefs.getString("message");
var handle = prefs.getString("handle");
var hashtag = prefs.getString("hashtag");
if (hashtag[0] == "#") {
hashtag = hashtag.substr(1,hashtag.length);
}
document.getElementById("tweetButton").setAttribute("data-text",text);
document.getElementById("tweetButton").setAttribute("data-via",handle);
document.getElementById("tweetButton").setAttribute("data-hashtags",hashtag);
}
(document, 'script', 'twitter-wjs');
}
</script>
]]>
</Content>
</Module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment