Skip to main content

Google Plus One button callback - Any way to "subscribe" to the +1 action?


I'm looking for a way to subscribe to the plus one button.



According to the documentation here: https://developers.google.com/+/plugins/+1button/#plusonetag-parameters I could add a callback attribute to the tag, but in my case I'm not allowed to interfere. I'm building a tool ontop of the site, an embedded JS triggered on document ready. I want to add the callback live, and it mustn't interfere the original callback if one was declared.



I don't have this problem with Facebook or Twitter (like and tweet, for instance) . In these cases there's the FB and twttr global variables, registered like so once they are available:




FB.Event.subscribe("edge.create", function(e) {
console.log(e);
})



or twitter's twttr.events.bind ...



Am I missing anything or is Google choosing a very awkward way to do things? What's their interest in this method and what can be done around it?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You can use the JavaScript API to retrieve the +1 callback.

    gapi.plusone.render(
    myDomNode,
    { "callback": myCallbackFunction });


    Or you alternatively can specify the "callback" attribute if you're using the DOM version.

    In either case, the callback will be invoked with an object which has two properties: href returns the URL which was +1'd, and state is either "off" or "on".

    ReplyDelete
  2. You could go one further than Daves answer, and indeed inject your own callback - but take the extra step of retrieving the existing callback value beforehand and dispatching it yourself within your own handler (if there is an existing callback value) with the same values as your callback received.

    That way both your handler and the original handler will be called, and hopefully no one would be any the wiser :)

    ReplyDelete
  3. An aggressive but usually workable solution would be to replace the callback attribute of the G:PLUSONE tag with your own function, which can call the original callback (if one was defined) and do its own stuff, too. Google's plusone.js script replaces the G:PLUSONE tag with an iframe, so this has to be done before this script executes (probably with a DOM-ready hook). Here's a naive example (which you can see working on jsfiddle - open up a debug console and click the +1 button).

    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
    var originalCallback = function(o) {
    console.log('original callback - ' + o.state);
    };
    // on DOM ready
    $(function() {
    var plusoneTag = $('G\\:PLUSONE');
    var originalCallbackName = $(plusoneTag).attr('callback');
    // global function
    hijackerCallback = function(o) {
    console.log('hijacking callback - ' + o.state);
    window[originalCallbackName](o);
    };
    plusoneTag.attr('callback', 'hijackerCallback');
    });
    </script>
    <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>




    ...persume that somewhere in the host page you have the +1 tag, like so:

    <g:plusone annotation="inline" callback="originalCallback"></g:plusone>


    As a side note, I've tried to listen for the removal of the G:PLUSONE tag using DOMNodeRemoved and replace the callback then - but that's too late and the plusone.js script is already bound to the original callback at this stage. In the real world, you should probably try injecting your script just before plusone.js (we're probably talking about a Chrome or Firefox extension here).

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.