1 /** 2 * jquery.sharelink.js 3 * @version 1.0 4 * @author mach3 5 * @requires jQuery http://jquery.com/ 6 */ 7 8 $.fn.extend({ 9 /** 10 * Function to return information about URLs for social bookmark ( network ) services as object. 11 * @class 12 * @property {string} twitter http://twitter.com/share?url={{url}}&text={{title}} 13 * @property {string} facebook http://www.facebook.com/sharer.php?u={{url}} 14 * @property {string} livedoor http://clip.livedoor.com/clip/add?link={{url}}&title={{title}} 15 * @property {string} hatena http://b.hatena.ne.jp/add?url={{url}} 16 * @property {string} delicious http://b.hatena.ne.jp/add?url={{url}} 17 * @property {string} google https://www.google.com/bookmarks/mark?op=add&bkmk={{url}}&title={{title}} 18 * @property {string} yahoo http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t={{title}}&u={{url}}&ei=UTF-8 19 */ 20 sharelinkURL:function(){ 21 return { 22 "twitter" : "http://twitter.com/share?url={{url}}&text={{title}}", 23 "facebook" : "http://www.facebook.com/sharer.php?u={{url}}", 24 "livedoor" : "http://clip.livedoor.com/clip/add?link={{url}}&title={{title}}", 25 "hatena" : "http://b.hatena.ne.jp/add?url={{url}}", 26 "delicious" : "http://www.delicious.com/save?url={{url}}&title={{title}}", 27 "google" : "https://www.google.com/bookmarks/mark?op=add&bkmk={{url}}&title={{title}}", 28 "yahoo" : "http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t={{title}}&u={{url}}&ei=UTF-8" 29 }; 30 }, 31 /** 32 * Function to return default configuration for sharelink as object. 33 * @class 34 * @property {string} url URL of web page you want to share. 35 * @property {string} title Title of web page you want to share. 36 * @property {string} target Target window string. 37 * @property {boolean} encode Boolean if encode URL and title or not. 38 */ 39 sharelinkDefaultOption:function(){ 40 return { 41 url:location.href, 42 title:document.title, 43 target:"_blank", 44 encode:true 45 }; 46 }, 47 /** 48 * Function to attach links for social bookmark ( network ) services to anchor elements. 49 * @class 50 * @param {string} type The name of service. (see keys of sharelinkURL) 51 * @param {object} option The configurations for sharelink. (see sharelinkDefaultOption) 52 * @returns {object} jQuery object itself. 53 */ 54 sharelink:function( type, option ){ 55 var op = $.extend( $().sharelinkDefaultOption(), option ), 56 urls = $().sharelinkURL(), 57 makelink = function(){ 58 return urls[type] 59 .replace( /{{url}}/g, (op.encode) ? encodeURI( op.url ) : op.url ) 60 .replace( /{{title}}/g, (op.encode) ? encodeURI( op.title ) : op.title ); 61 }; 62 $(this).attr("href", makelink()); 63 $(this).attr("target", op.target); 64 return this; 65 } 66 }); 67