Skip to content

Instantly share code, notes, and snippets.

@intents
Created March 30, 2011 14:50
Show Gist options
  • Save intents/894540 to your computer and use it in GitHub Desktop.
Save intents/894540 to your computer and use it in GitHub Desktop.
Invoke Intents
<script type="text/javascript">
(function() {
if (window.__twitterIntentHandler) return;
var intentRegex = /twitter\.com(\:\d{2,4})?\/intent\/(\w+)/,
windowOptions = 'scrollbars=yes,resizable=yes,toolbar=no,location=yes',
width = 550,
height = 420,
winHeight = screen.height,
winWidth = screen.width;
function handleIntent(e) {
e = e || window.event;
var target = e.target || e.srcElement,
m, left, top;
while (target && target.nodeName.toLowerCase() !== 'a') {
target = target.parentNode;
}
if (target && target.nodeName.toLowerCase() === 'a' && target.href) {
m = target.href.match(intentRegex);
if (m) {
left = Math.round((winWidth / 2) - (width / 2));
top = 0;
if (winHeight > height) {
top = Math.round((winHeight / 2) - (height / 2));
}
window.open(target.href, 'intent', windowOptions + ',width=' + width +
',height=' + height + ',left=' + left + ',top=' + top);
e.returnValue = false;
e.preventDefault && e.preventDefault();
}
}
}
if (document.addEventListener) {
document.addEventListener('click', handleIntent, false);
} else if (document.attachEvent) {
document.attachEvent('onclick', handleIntent);
}
window.__twitterIntentHandler = true;
}());
</script>
@chenjianjx
Copy link

How to get this script work? I copied it to an html file and show the html in a brower. It turned to be nothing.

@chenjianjx
Copy link

Can I have an out-of-box sample?

@intents
Copy link
Author

intents commented Jun 27, 2011

This snippet of Javascript readies the browser environment to detect anchor tags with hrefs pointing to Twitter Web Intents.

Include the above SCRIPT tag at the end of your HTML page. Somewhere above the SCRIPT tag, include a simple anchor tag pointing to one of the valid Web Intent URLs.

For example:
@twitterapi

When links that match the Web Intents patterns are clicked, this SCRIPT will create a pop-up window for that interaction to complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment