Social Bookmarking from Flash or Flex

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:

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.

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.

Date: