How can I confirm a Twitter web intent was sent?

半世苍凉 提交于 2020-12-29 07:34:10

问题


I'd like to confirm a user tweeted after clicking a Twitter Web Intent. How can I accomplish this?

Example:

<a href="https://twitter.com/intent/tweet?text=Simple+Web+Intent">Tweet</a>

Assuming an anonymous* user clicks this link and tweets, how can I confirm this?

Ideally I'd love to get a link to the tweet.


* anonymous as in not authenticated on my site and without knowing their Twitter username


回答1:


Update

This solution no longer works after Twitter updated the behaviour of the widgets. You can only track if the tweet button was clicked now, and not if the tweet was actually posted.


You can track tweets using the 'twttr.events.bind' event listener form the Twitter API. A working example of tracking tweets can be found here: http://jsfiddle.net/EZ4wu/3/

HTML:

<a href="https://twitter.com/intent/tweet?url=https://www.webniraj.com/2012/09/11/twitter-api-tweet-button-callbacks/&text=Twitter+API:+Tweet+Button+Callbacks"><i class="icon-twitter-sign icon-white"></i> Tweet</a>

JavaScript:

function reward_user( event ) {
    if ( event ) {
        alert( 'Tweeted' );
        console.log( event );
    }
}

window.twttr = (function (d,s,id) {
    var t, js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
    js.src="//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
    return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
    }(document, "script", "twitter-wjs"));

twttr.ready(function (twttr) {
    twttr.events.bind('tweet', reward_user);
});

My previous solution is now outdated and no longer appears to work.




回答2:


Try this:

                        twttr.events.bind('loaded', function(event) {

                            window.location = "http://theredirect_link.com"

                        });



回答3:


@Niroj's answer no longer works. They updated the api, so the event listener fires immediately after user clicks the button - you have no way of checking if he actually tweeted.

If you create the window yourself (without using Twitter widgets), you can at least detect if the window was closed (either user closed it or tweeted).

Don't link the widgets api!

HTML:

<a id="twitter-intent" href="https://twitter.com/intent/tweet?url=https://www.webniraj.com/2012/09/11/twitter-api-tweet-button-callbacks/&text=Twitter+API:+Tweet+Button+Callbacks"><i class="icon-twitter-sign icon-white"></i> Tweet</a>

JS:

document.getElementById('twitter-intent').addEventListener('click', function(e) {

    e.preventDefault();
    e.stopPropagation(); //this should do in case you don't want to unlink twitter widgets api

    var width  = 575,
        height = 400,
        left   = (screen.width  - width)  / 2,
        top    = (screen.height - height) / 2,
        url    = this.href,
        opts   = 'status=1' +
                 ',width='  + width  +
                 ',height=' + height +
                 ',top='    + top    +
                 ',left='   + left;

    var win = window.open(url, 'twitter_share', opts);

    var timer = setInterval(checkWin, 500);

    function checkWin() {

        if (win.closed) {
            //just assume the user tweeted (he could just close the window without tweeting)
            alert("Thank you for tweeting!");   
            clearInterval(timer);

        }

    }

});


来源:https://stackoverflow.com/questions/23278951/how-can-i-confirm-a-twitter-web-intent-was-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!