Costumize Facebook, Twitter ad Google Plus Sharing

Facebook : beyond the great api they give for extra stuff they are the only ones still allowing title, description and image via href so the code is like this

function OpenFacebookShare(title, summary, url, imageUrl) {
  var url = '[title]=' +
        encodeURIComponent(title) +
    '&p[summary]=' + encodeURIComponent(summary) +
    '&p[url]=' + encodeURIComponent(url) +
    '&p[images][0]=' + encodeURIComponent(imageUrl);, 'mywindow', 'menubar=1,resizable=1,width=650,height=450');

as simple as that.

Twitter: twitter only allows url and text (only 140 chars) by href.
but they do request the url for their own meta tags, i.e. "twitter:title" ect.

to do this you must create a twitter account and create a "card" for your domain and allow that domain. its really not that much of a horror, just create an account, go here to the part of the tool validator and put in your domain and it will ask you to approve it. as simple as that.

about the tags you have a complete list of tags there, they all look like this:
<meta property="twitter:card" content="summary" />
<meta property="twitter:title" content="Bresleveloper's Blog" />
dont forget the card :)

all that leaves you with that simple code:
function OpenTwitterShare(url, text) {
    var url = '' + 
         encodeURIComponent(url) + '&text=' +     
         encodeURIComponent(text);, 'mywindow''menubar=1,resizable=1,width=650,height=450');

Google Plus: is even meaner - they allow only url.
so you, must, again, create an account and make some steps:
1. go here
2. go to "Service" and turn on "Google+ API"
3. go to "API access" and create there a clientID

the trick with google+ is that you dont "send" a request of something but instead you give the script the button (or other element) you want to be your button and he creates it alone.
your code will be like this

(function () {
    var po = document.createElement('script'); 
    po.type = 'text/javascript'
    po.async = true;
       po.src = '';//?onload=onLoadCallback';
    //?onload=onLoadCallback' this allows a callback when the google scripts loads if you need it
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(po, s);

function SetGoogleShareButton(url, comment, buttonID) {
   var options = { contenturl : url,
                   clientid : 'your clientID here',
                   cookiepolicy : 'single_host_origin',
                   prefilltext : comment,
                   recipients: null,
                   calltoactionlabel : 'VIEW',
                   calltoactionurl : url
   gapi.interactivepost.render(buttonID, options);
   return false;

//callback for google - u cant give it up
function signinCallback(authResult) {
   if (authResult['access_token']) {
      // User successfully authorized the G+ App!
   } else if (authResult['error']) {
      // User has not authorized the G+ App!

and its almost the end. you need to call SetGoogleShareButton somewhere AFTER the google scripts loads, so in my case the buttons where inside a popup so i called it in the "LoadPopup" func, or you can just make a callback that will call it and send it as the ?onload= value.



Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

Asp.Net Ending Response options, Response.End() vs CompleteRequest()