AddThis是目前比较流行的一个收藏分享网站.
通过定制,我们可以支持一个页面响应多个分享信息.
以下信息来自AddThis API文档:
Overview
After gathering your feedback for a year or so, we’ve completely redesigned our API, with the following goals in mind:
- If you’re not a JavaScript wizard, the most common customizations should still be easy
- In fact, most use cases should be covered with as little JavaScript as possible
- But even the most complicated configurations should be possible somehow (for example, multiple buttons on a page behaving differently)
- UI management code in JavaScript shouldn’t be required to actually use the tools
The new API supports global configuration (as with our original API) as well as per-element configuration–every button on a page can behave differently in almost every regard. You might not need to ever go that far. But you could.
The new API also offers a few new ways of rendering buttons. In the past, we distributed code with inline JavaScript. Not only did this lock us in to whatever code we distributed, but often we found users had trouble managing that much code, or even just pasting inline JavaScript into their CMSs. Now we take care of all that for you. You tell us which elements you want to be AddThis buttons (and how you’d like them configured), and we take care of the rest.
Why? We’re big fans of modern JavaScript toolkits like jQuery that separate markup and interactive behavior like this. (If you’re not familiar with the concept of Unobtrusive JavaScript, here’s a few articles.) It’s easier for everyone to manage, and, because it lets us drastically simplify the code we distribute, should even improve load times.
An Illustrative Comparison
API 1.0
<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, '', '[URL]', '[TITLE]');" onmouseout="addthis_close();" onclick="return addthis_sendto();"><img src= "http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" border="0" alt="Share"/></a>
API 2.0
<a class="addthis_button"></a>
Configuration
We support two types of configuration objects: one for configuring our UI tools, and one for specifying what you’re sharing.
UI Configuration
The same object format can be used for specifying global or instance configuration. To specify global configuration, the special name addthis_config must be used.
For example:
var addthis_config = { // ... members go here }
UI Configuration Options
| Name | Description | Type | Default value |
|---|---|---|---|
| username | Your AddThis username. Always global to a page. | string | none |
| services_exclude | Services to exclude from all menus. For example, setting this to 'facebook,myspace' would hide Facebook and MySpace on all our menus. Always global. |
string (csv) | none |
| services_compact | Services to use in the compact menu. For example, setting this to 'print,email,favorites' would result in only those three services appearing. Always global. |
string (csv) | We regularly optimize the default list based on our data. |
| services_expanded | Services to use in the expanded menu. Useful if very few services are desired — specifying a long list via services_exclude could be tiresome, and wouldn’t catch a new service added later. For example, setting this to 'bebo,misterwong,netvibes' would result in only those three services appearing in the expanded menu. Always global. |
string (csv) | all the services AddThis offers |
| services_custom | Specify your own AddThis bookmarking service like so:{name: "My Service",. All three fields must be present for each custom service. Always global. Sample usage.
Analytics will appear in your account under the service’s base domain name (no subdomains–”example.com” in this case), and the first service specified will appear in the compact menu. Only one service per domain is accepted. All custom services specified automatically get added to the expanded menu. To include a custom service with |
array of custom service objects | none |
| ui_click | If true, the compact menu will never appear upon mousing over the regular button. Instead, it will be revealed upon clicking the button. |
boolean | false |
| ui_delay | Delay, in milliseconds, before compact menu appears when mousing over a regular button. Capped at 500 ms. | integer | 0 |
| ui_hover_direction | Normally, we show the compact menu in the direction of the user’s browser that has the most space (i.e., a button at the bottom of the page will pop up, and vice versa). You can override this behavior with this setting: 1 to force the menu to appear “up”, -1 to force the menu to appear “down”. |
integer | 0 |
| ui_language | For forcing the menu to use a particular language, specified via ISO code. For example, setting this to "sv" would show the menu in Swedish. Note: Regardless of the number of times it’s specified, only one language is supported per page. |
string | User’s browser |
| ui_offset_top | Number of pixels to offset the top of the compact menu from its parent element | integer | 0 |
| ui_offset_left | Number of pixels to offset the left of the compact menu from its parent element | integer | 0 |
| ui_header_color | Color of the compact and extended menus’ header foregrounds. For example, "#FFF" would make the text white. |
hex string | n/a |
| ui_header_background | Color of the compact and extended menus’ header backgrounds. For example, "#000" would make the text appear on a black background. |
hex string | n/a |
| ui_cobrand | Additional branding message to be rendered in the upper-right-hand corner of the menus. Should be less than 15 characters in most cases to render properly. | string | none |
| ui_use_addressbook | If true, the user will be able import their contacts from popular webmail services when using AddThis’s email sharing. |
boolean | false |
| ui_508_compliant | If true, clicking the AddThis button will open a new window to a page supporting sharing without JavaScript. |
boolean | false |
| data_track_linkback | Set to true to allow us to append a variable to your URLs upon sharing. We’ll use this to track how many people come back to your content via links shared with AddThis (report coming soon). Highly recommended. Always global. | boolean | false |
| data_ga_tracker | Google Analytics tracking object. If set, we’ll send AddThis tracking events to Google, so you can have integrated reporting. Sample usage. | object | null |
| data_use_flash | If not true, the AddThis button won’t load our Flash-based analytics engine. Some future reports will be affected. Always global. | boolean | true |
| data_use_cookies | If not true, we won’t set any cookies at all. Some future reports will be affected. Always global. | boolean | true |
Supported Services
For use in service_* configuration options.
| code | description |
|---|---|
| a1webmarks | A1-Webmarks, a social bookmarking tool |
| aim | AIM, the ubiquitous instant messaing platform |
| amazonwishlist | Amazon.com’s Wishlist |
| amenme | Amen Me!, a social bookmarking site with a Christian slant |
| aolmail | AOL Mail, AOL’s webmail platform |
| arto | Arto, a picture sharing site |
| ask | Ask.com |
| backflip | Backflip, a social bookmarking tool |
| baidu | Baidu, a social bookmarking tool |
| ballhype | BallHype, a social sports news aggregator |
| bebo | Bebo, a social network |
| bitly | Bit.ly, a URL shortener |
| bizsugar | bizSugar, a social media aggregator |
| bleetbox | Bleetbox, a social chat network |
| blinklist | Blinklist, a social bookmarking tool |
| blogger | Blogger, a blogging platform |
| bloggy | Bloggy, a Swedish blogging platform |
| blogmarks | Blogmarks, a social bookmarking tool |
| bobrdobr | Bobrdobr, a Russian social bookmarking tool |
| bordom | Bordom, a link sharing site |
| brainify | Brainify, a social bookmarking tool for academics and researchers |
| bryderi | Bryderi.se, a Swedish social shopping platform |
| buddymarks | BuddyMarks, a social bookmarking tool |
| buzz | Buzz, a social news aggregator |
| buzz_fr | Buzz.fr, the French version of Buzz |
| care2 | Care2, a health-focused social bookmarking tool |
| citeulike | CiteULike, a social citation tool for academics and researchers |
| connotea | Connotea, a social bookmarking tool for academics and researchers |
| delicious | Delicious (Delicious is a social bookmarking service that allows users to tag) |
| designmoo | Designmoo, a social design news aggregator |
| digg | Digg, a social news aggregator |
| diglog | diglog, a Chinese social bookmarking tool |
| diigo | Diigo, a social bookmarking and annotation tool |
| domelhor | DoMelhor, a Portuguese social news aggregator |
| doower | Doower.com |
| dosti | Dosti, an Indian-language news aggregator |
| dotnetkicks | DotNetKicks, a social .NET news aggregator |
| dzone | dzone, a social bookmarking tool for developers |
| edelight | edelight, a German social shopping platform |
| ekudos | eKudos, a Dutch social news aggregator |
| The AddThis email interface | |
| eucliquei | euCliquei, a Latin American social news aggregator |
| evernote | Evernote, a social note-taking tool |
| fabulously40 | Fabulously40, a social network for women “of age” |
| Facebook, a social network | |
| fark | Fark, a social news aggregator |
| faves | Faves, a social bookmarking tool |
| favorites | Adds the page to the browser’s bookmarks |
| favoritus | Favoritus, a social bookmarking tool |
| fnews | Fnews, an Azerbaijani social news aggregator |
| folkd | Folkd, a social bookmarking tool |
| forgetfoo | forgetfoo.com |
| fresqui | Fresqui, a Spanish social news aggregator |
| friendfeed | FriendFeed, a real-time social feed aggregator |
| funp | funP, a Taiwanese social bookmarking tool |
| gabbr | Gabbr, a social news aggregator |
| gacetilla | Gacetilla, a Spanish social news aggregator |
| globalgrind | GlobalGrind, a social content aggregator |
| gluvsnap | GluvSnapPin, a social health news aggregator |
| gmail | Gmail, Google’s email platform |
| Google Bookmarks, a social bookmarking tool | |
| googlereader | Google Reader, a feed reader |
| googletranslate | Google Translate, a translation service |
| gravee | Gravee, a social recommendation engine |
| grumper | Grumper, for complaining about anything |
| habergentr | Haber.gen.tr, a Turkish social news aggregator |
| hackernews | Hacker News, a social technology news aggregator |
| hadashhot | Hadash Hot, an Israeli social news aggregator |
| hatena | Hatena, a Japanese social bookmarking tool |
| hellotxt | HelloTxt, a link-sharing tool |
| hemidemi | HEMiDEMi, a Taiwanese social bookmarking tool |
| hipstr | hipstr, a social bookmarking tool |
| hitmarks | Hitmarks, a social bookmarking tool |
| hotklix | Hotklix, an social entertainment news aggregator |
| hotmail | Hotmail, Microsoft’s webmail platform |
| hyves | Hyves, a Dutch social network |
| jamespot | Jamespot, a social sharing platform |
| jumptags | Jumptags, a social bookmarking tool |
| kaboodle | Kaboodle, a social shopping platform |
| kaevur | Kaevur, a link sharing tool |
| kirtsy | KiRTSY, a social sharing platform |
| kudos | Kudos, a Norwegian social bookmarking tool |
| laaikit | Laaikit, a social news aggregator |
| librerio | Librerio, a social bookmarking tool |
| linkagogo | Link-a-Gogo, a social bookmarking tool |
| LinkedIn, a social network for business | |
| live | Windows Live Favorites, a social bookmarking tool |
| livejournal | LiveJournal, a blogging platform |
| lunch | Lunch.com, a social reviews platform |
| lynki | Lynki, a social sharing platform |
| memori | Memori.ru, a Russian social bookmarking tool |
| meneame | Menéame, a Spanish social content aggregator |
| mindbodygreen | Mindbodygreen, a health-focused social bookmarking tool |
| misterwong | Mister Wong, a social bookmarking tool |
| misterwong_de | Mister Wong.de, the German-language version of Mister Wong |
| mixx | Mixx, a social news aggregator |
| multiply | Multiply, a media sharing tool |
| myaol | myAOL Favorites, a social bookmarking tool |
| mynasa | My NASA, NASA’s bookmarking tool |
| mylinkvault | MyLinkVault, a social bookmarking tool |
| myspace | MySpace, a social network |
| n4g | N4G, a social game news aggregator |
| netlog | NetLog, a social network |
| netvibes | Netvibes, a personal news aggregator |
| netvouz | Netvouz, a social bookmarking tool |
| newstrust | NewsTrust, a social network for journalists |
| newsvine | Newsvine, a social news aggregator |
| nujij | Nujij, a Dutch social bookmarking tool |
| oknotizie | OKNOtizie, an Italian social news aggregator |
| oneview | Oneview, a social news aggregator |
| oyyla | Oyyla, a Turkish social news and media aggregator |
| phonefavs | PhoneFavs, a mobile-friendly social bookmarking tool |
| pingfm | Ping.fm, a social bookmarking and micro-blogging tool |
| planypus | Planypus, a social wiki |
| plaxo | Plaxo, an online address book and social network |
| plurk | Plurk, a blogging platform |
| polladium | Polladium, an online polling system |
| posterous | Posterous, a blogging platform |
| Print the current page | |
| printfriendly | PrintFriendly, a service for printing online content better |
| propeller | Propeller, a social news aggregator |
| pusha | Pusha, a social news aggregator |
| Reddit, a social news aggregator | |
| segnalo | Segnalo, an Italian social bookmarking tool |
| shetoldme | She Told Me, a social news aggregator |
| simpy | Simpy, a social bookmarking tool |
| slashdot | Slashdot, a social technology news aggregator |
| smaknews | Smak News, a social news aggregator for women |
| sodahead | SodaHead, a social discussion platform |
| sonico | Sonico, a photo sharing platform |
| sphinn | Sphinn, a social network for marketers |
| squidoo | Squidoo, a content aggregator |
| startaid | Startaid, a social bookmarking tool |
| strands | Strands, a social network for runners |
| studivz | studiVZ, a German social network |
| stumbleupon | StumbleUpon, a social content discovery platform |
| stylehive | Stylehive, a social network for the stylish |
| svejo | Svejo, a Bulgarian social news aggregator |
| symbaloo | Symbaloo, a personal web portal |
| tagza | Tagza, a social bookmarking tool |
| technet | Technet, Microsoft’s TechNet Library |
| technorati | Technorati, a blog indexing and tracking tool by tag or keyword. Also provides popularity indexes.) |
| tellmypolitician | TellMyPolitician, a tool for contacting your local politician |
| thisnext | ThisNext, a social shopping platform |
| tipd | Tip’d, a social financial news aggregator |
| transferr | Transferr, a personal portal |
| tulinq | Tulinq, a Spanish social news aggregator |
| tumblr | Tumblr, a blogging platform |
| tusul | Tusul, a Turkish social news aggregator |
| Twitter, a micro-blogging platform | |
| typepad | Typepad, a blogging platform |
| viadeo | Viadeo, a social networking tool for business |
| virb | Virb, a social media sharing tool |
| webnews | Webnews, a German social news aggregator |
| wordpress | Wordpress, a blogging platform |
| worio | Worio, a social content discovery engine |
| wovre | Wovre, a social network |
| wykop | Wykop, a social news aggregator |
| yahoobkm | Yahoo! Bookmarks, a social bookmarking tool |
| yahoomail | Yahoo! Mail, Yahoo’s email platform |
| yammer | Yammer, a micro-blogging platform |
| yardbarker | Yardbarker, a social sports news aggregator |
| yigg | Yigg, a German social news aggregator |
| youmob | YouMob, a social discussion platform |
Sharing Configuration
The same object format can be used for specifying global or instance configuration. To specify global configuration, the special name addthis_share must be used.
For example:
var addthis_share = { // ... members go here }
Sharing Configuration Options
| Name | Description | Type | Default value |
|---|---|---|---|
| url | URL to share (tutorial here) | url | window URL |
| title | title of shared object (tutorial here) | string | window title |
| content | additional HTML content to share with link (tutorial here) | string | none |
| email_template | name of template to use for emailed shares (tutorial here) | string | none |
| email_vars | associative array mapping custom email variables to values (tutorial here) | object | none |
| templates | associative array mapping services to post templates (see below) | object | none |
Some destinations support more than just posting a link and a link title. Twitter, for example, lets the user post a bunch of arbitrary content. By using a template, you can customize the default post.
Sample template:
twitter: 'check out {{url}} (from @addthis)'
We’ll fill in a few different tokens for you — you can use all, some or none of them:
- {{url}} – the URL being shared; on destinations that have a length limit (i.e., Twitter), we’ll automatically shorten the URL for you
- {{lurl}} – the URL being shared; even on destinations that have a length limit, this token (short for “long URL”) will never shorten your URL
- {{title}} – the title being shared
- {{html}} – the arbitrary HTML shared (the “content” parameter above)
You can provide one template per destination that supports them (currently just Twitter):
var addthis_share = { // ... templates: { twitter: 'check out {{url}} (from @example_dot_com)', } }
Section 508 Compliance
You can make your AddThis button Section 508 compliant by using ui_508_compliant.
<script type="text/javascript"> var addthis_config = { ui_508_compliant: true } </script> <a class="addthis_button"></a>
Rendering with JavaScript
You can render the AddThis button anywhere on your page using JavaScript. All of our JavaScript rendering functions take the form:
addthis.function(target(s), [configurationObject], [sharingObject]);
where target(s) can be
- a classname, specified with a dot: ‘.sharing-button’
- an id, specified with a hash: ‘#addthis_button_1′
- an actual HTML element reference
- an array of HTML element references
If configurationObject is unspecified, global defaults (i.e., the addthis_config object) are used. If sharingObject is unspecified, global defaults (i.e., the addthis_share object) are used. At present, we only support rendering of buttons via JavaScript, but we plan to support inline menus as well in the future.
The Button
Renders a normal AddThis button at an anchor tag. If the tag has no image in it, we load our default image. If the tag has an image in it, that image is used as for the button graphic.
addthis.button(target(s), [configurationObject], [sharingObject]);
Examples
Render a button at a tag:
<a id="atbutton"></a> <script type="text/javascript"> addthis.button("#atbutton"); </script>
Render a button at every anchor with a certain class:
<a class="sharing-button"></a> . . . <a class="sharing-button"></a> . . . <a class="sharing-button"></a> <script type="text/javascript"> addthis.button('.sharing-button'); </script>
Each “sharing-button”-classed anchor would become a regular AddThis button.
Share many different blog posts:
<a id="blog-post-24"></a> <script type="text/javascript"> addthis.button('#blog-post-24', {}, {url: "http://example.com/blog/24", title: "The 24th post"}); </script> . . . <!-- blog content --> . . . <a id="blog-post-25"></a> <script type="text/javascript"> addthis.button('#blog-post-25', {}, {url: "http://example.com/blog/25", title: "The 25th post"}); </script> . . .
Render a button at an HTML element passed by reference:
<a id="button-1"></a> <script type="text/javascript"> addthis.button(document.getElementById('button-1')); </script>
Render a button at an array of HTML elements:
<a id="button-1"></a> <a id="button-2"></a> <a id="button-3"></a> <a id="button-4"></a> <script type="text/javascript"> addthis.button([document.getElementById('button-1'), document.getElementById('button-2'), document.getElementById('button-3'), document.getElementById('button-4')]); </script>
Rendering with Link Decoration
While the JavaScript rendering methods are powerful, in most cases we believe they’re more powerful than what’s actually necessary. Following the principles of Unobtrusive JavaScript as discussed above, you can now create and configure an AddThis button in three simple steps:
- Add plain old anchor tags where you want your buttons to appear
- Add the appropriate CSS classes for the kinds of buttons you want them to be
- Add our JavaScript
Upon loading, our script will look for all our standard classes and render buttons as specified. Here’s some simple examples to demonstrate:
The code used to generate it: <a></a>
An email button:
The code used to generate it: <a></a>
See where we’re going with this? The links aren’t processed until after the DOM’s ready, so it won’t even slow down your page. (If you don’t want your layout to shift when the images are loaded, however, you should make sure to provide static images or content for all the links.)
Decoration Class Reference
| Name | Description | Live example |
|---|---|---|
| addthis_button | Standard AddThis button; loads our regular button image if no image is present. Mousing over this button reveals the compact menu. Clicking it opens the expanded menu. | |
| addthis_button_email | Email-only button | |
| addthis_button_service | One-click sharing to a particular service, where service could be the service code of any service we support |
|
| addthis_toolbox | Standard AddThis Toolbox (requires child elements) | |
| addthis_default_style | When set, toolboxes and other buttons will be rendered floating left, per our standard distribution | see above |
| addthis_button_compact | Same as addthis_button, except only a small + logo is used as a default image. | Share |
| addthis_button_expanded | Same as addthis_button_compact, except mousing over the button has no effect. | More Services |
| addthis_separator | Adds a few pixels of padding around a floating separator, for use in toolboxes. Only intended for spans. |
LEFT|RIGHT
|
Note: Class-based configuration trumps global JavaScript configuration objects. For example:
<script type="text/javascript"> var addthis_config = { ui_click: true /* normally would disable mouseover behavior */ } </script> <div class="addthis_toolbox"></div> <a class="addthis_button_compact">Share</a>
The compact menu button rendered by this code will have mouseover behavior, as the class-based configuration is considered authoritative.
Attribute-based Configuration
So you’ve probably noticed that the JavaScript-based configuration and the decoration-based rendering only work together for global configurations. If you want different configurations for each button, you can’t pass a JavaScript configuration object in to the default renderer. You can, however, specify parameters as custom attributes on the anchors themselves. All the parameters described above are available, with the prefix addthis:. Let’s look at some examples:
To specify a custom URL and title:
<a class="addthis_button" addthis:url="http://example.com" addthis:title="An excellent website"></a>
To specify a hover delay:
<a class="addthis_button" addthis:ui_hover_delay="200"></a>
Note: Attribute-based configuration trumps global JavaScript configuration objects.
Configuration Inheritance
Configuration is inherited by the elements nested within toolbox divs. This way, you could easily customize an entire toolbox without having to add configuration attributes to every single button. For example:
<div class="addthis_toolbox" addthis:url="http://example.com/blog/post/2009/05/01" addthis:title="Hooray!"> <a class="addthis_button_compact">Share</a> <a class="addthis_button_email"></a> <a class="addthis_button_facebook"></a> <a class="addthis_button_buzz"></a> </div>
Each of the buttons in this toolbox would share the URL specified at the toolbox level.
There are two exceptions.
1. Configuration isn’t inherited by children of arbitrary elements.
In this example, neither button would share “http://example.com”. The parent div must be an addthis_toolbox.
<div addthis:url="http://example.com"> <a class="addthis_button_email"></a> <a class="addthis_button_print"></a> </div>
2. Configuration isn’t inherited by regular addthis_buttons.
Because addthis_button is intended to stand alone, it does not inherit from a toolbox parent.
<div class="addthis_toolbox" addthis:url="http://example.com"> <a class="addthis_button"></a> </div>
Valid XHTML
If you’re using XHTML, the custom attributes will not pass validation. To make your XHTML technically valid, you’ll need to add a custom namspace declaration to your opening <html> tag. (Note that the W3C validator does not take custom namespaces into account.)
Change
<html xmlns="http://www.w3.org/1999/xhtml">
to
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:addthis="http://www.addthis.com/help/api-spec">
