The Tweet Button is usually:
<a href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="someone">Tweet</a>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
and here I can get a callback when the user tweets:
<script>
twttr.events.bind('tweet', function(event) {
console.log(event);
});
</script>
What I don’t understand: how does twitter give me this callback? The tweet is done in another window and from another domain. How is this possible?
From what I can gather this is Twitters own implementation of Web Intents which is a Web API loosely based on Androids Intents functionality. It looks like that Twitter are using a JavaScript implementation of Web Intents (possibly similar to this).
In this scenario, the Twitter JavaScript registers an
intentwith the browser. Theintentbeing you sharing a URL. When the user clicks the tweet button theintentactivity is started and a new pop-up windows is displayed.The user clicks the tweet button from the pop-up window and the browser posts back the event data to a callback specified when starting the activity. The callback is specified in the
widget.jsand you hook up to this event using thetwttr.events.bindmethod.There is a really good example of how this works on the JavaScript github implementation of WebIntents.
The example is obviously slightly different to the Twitter implementation but the general process is the same.
Here are few other references for the WebIntents API: