Seems one of the things I’m asked to do fairly often is provide links to the social apps that all the kids are crazy about these days. Tired of constantly having to rewrite script or look up an api call, I figured I’d sit down and begin accumulating all the basic calls in one handy dandy file. And, hence, came Bookmark.as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.onebyonedesign.social { import flash.net.navigateToURL; import flash.net.URLRequest; import flash.net.URLRequestMethod; /** * Posts links to various social networks * @author Devon O. Wolfgang */ public class Bookmark { public static const DIGG:String = "digg"; public static const FACEBOOK:String = "faceBook"; public static const GOOGLE_BOOKMARK:String = "googleBookmark"; public static const MYSPACE:String = "myspace"; public static const STUMBLE_UPON:String = "stumbleUpon"; public static const TECHNORATI:String = "technorati"; public static const TWITTER:String = "twitter"; public static const YAHOO_BOOKMARK:String = "yahooBookmark"; public function Bookmark() { /* do not instantiate - use static method post() */ } public static function post(to:String, link:String, title:String = "") { var request:URLRequest = new URLRequest(); request.method = URLRequestMethod.GET; link = encodeURI(link); title = encodeURI(title); switch (to) { case DIGG : request.url = "http://digg.com/submit?phase=2&url=" + link + "&title=" + title; break; case FACEBOOK : request.url = "http://www.facebook.com/sharer.php?u=" + link; break; case GOOGLE_BOOKMARK : request.url = "http://www.google.com/bookmarks/mark?op=add&bkmk=" + link + "&title=" + title; break; case MYSPACE : request.url = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + link + "&t=" + title; break; case STUMBLE_UPON : request.url = "http://www.stumbleupon.com/submit?url=" + link; break; case TECHNORATI : request.url = "http://technorati.com/faves/?add=" + link; break; case TWITTER : request.url = "http://twitter.com/home?status=" + link; break; case YAHOO_BOOKMARK : request.url = "http://myweb2.search.yahoo.com/myresults/bookmarklet?u=" + link + "&t=" + title; break; default : break; } navigateToURL(request, "_blank"); } } } |
Usage is a piece of cake.
1 2 3 4 5 6 7 8 |
var url:String = "http://www.onebyonedesign.com/"; var title:String = "Bitchin' Website!"; facebookButton.addEventListener(MouseEvent.CLICK, postToFacebook); private function postToFacebook(event:MouseEvent):void { Bookmark.post(Bookmark.FACEBOOK, url, title); } |
That scrap of code will open up Facebook in a new browser window to let folks login if necessary or add comments or whatever. While many of the networks don’t require a title to be sent, it doesn’t hurt to pass one to the static post() method – it just won’t be used. On the other hand, if you know the network you’re posting to doesn’t need one, you can leave the title parameter out. I just find it easiest to always send one and not worry about it.
Obviously, this script ain’t rocket science, but sometimes it’s the simplest things that prove most useful. If you have any social site html api’s you’re holding out on and want to add them to the list, post them in a comment. I’d like to make this class fairly well inclusive.
++Love for Warbler
In other news, warbler has now been officially recognized by Twitter, so while surfing the Twitter page you may just see tweets ending in something like: “5 minutes ago from warbler”. Pretty cool. Kind of the nerdy web app developer equivalent of seeing your name in lights.
Great! This looks useful. I’ve written a similar one of these just as included functions, but it’s a good idea to make it a class like you have. I never take it to that level of abstraction, so props to you!
Rock n’ roll my friend! Nice job on the app!
Kids will love you more.
Excellent class – simplicity is the key and this has got me out of a hole – great job!
hey… where i put the code? i use blogger.. please help me….
This was excellent help. if only we could some how add messages to facebook too. currently only links can be posted like this.
1013: The private attribute may be used only on class property definitions.
Hey Sean, that usage example is just a snippet from a larger class file (obviously you would also need a facebookButton instance defined elsewhere as well).
While I’m revisiting this old post though, I should say that, rather than using the encodeURI() function in the static post() method, you should use encodeURIComponent().
Hope that helps out.
d.