jQuery Twitter 的推文按钮有回调吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4947825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:15:05  来源:igfitidea点击:

Is there a callback for Twitter's Tweet Button?

jqueryapitwittercallback

提问by jbnunn

Is there a way to register a callback on Twitter's Tweet button? I'd like to be able to track which particular users on my site have tweeted a link. I can't add on onClick event because it's a cross-domain iFrame. Any other ideas?

有没有办法在 Twitter 的 Tweet 按钮上注册回调?我希望能够跟踪我网站上的哪些特定用户在推特上发布了链接。我无法添加 onClick 事件,因为它是跨域 iFrame。还有其他想法吗?

I've seen one way to do itbut it seems unreliable. Their documentationdoesn't mention anything so I am looking for help with a work-around.

我见过一种方法,但它似乎不可靠。他们的文档没有提及任何内容,因此我正在寻求解决方法的帮助。

回答by abraham

Twitter has Web Intent eventsfor loaded, rendered, resize, tweet, follow, retweet, like, and click.

Twitter的网站意图的事件loadedrenderedresizetweetfollowretweetlike,和click

twttr.events.bind(
  'tweet',
  function (event) {
    // Do something there
  }
);

The behavior was changedin the fall of 2015 due to the unreliableness of callbacks happening after an event is complete.

由于事件完成后发生的回调不可靠,该行为在 2015 年秋季发生了变化。

They will now be triggered when a user invokes the action in your page, rather than after the action is completed.

它们现在将在用户调用您页面中的操作时触发,而不是在操作完成后触发。

Example loading widgets.js:

加载 widgets.js 的示例

<script>
// Performant asynchronous method of loading widgets.js
window.twttr = (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
  if (d.getElementById(id)) return t;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);

  t._e = [];
  t.ready = function(f) {
    t._e.push(f);
  };

  return t;
}(document, "script", "twitter-wjs"));
</script>

<script>
// Wait until twttr to be ready before adding event listeners
twttr.ready(function (twttr) {
  twttr.events.bind('tweet', function(event) {
    console.log(event);
  });
});
</script>

回答by Jhony Fung

I have just implemented a tweet callback event.

我刚刚实现了一个推文回调事件。

More information on Twitter's callback event
- Twitter Web Intents
- Web Intents Javascript Events

更多关于 Twitter 回调事件的信息
- Twitter Web Intents
- Web Intents Javascript Events

The Javascript callback

Javascript 回调

$.getScript("http://platform.twitter.com/widgets.js", function(){
   function handleTweetEvent(event){
     if (event) {
       alert("This is a callback from a tweet")
     }
   }
   twttr.events.bind('tweet', handleTweetEvent);        
 });

The tweet button:

推文按钮:

 <a href="http://twitter.com/intent/tweet?url=http://test.com;via=stack">twitter</a>

回答by James Lawruk

If you load the Twitter Widgets JS, you can bind a callback function to the tweet event. See the code below:

如果加载 Twitter Widgets JS,则可以将回调函数绑定到推文事件。请参阅下面的代码:

<a href="https://twitter.com/intent/tweet?url=http://yoursite.com/about&text=Message">Twitter</a>

<script>        
//Twitter Widgets JS
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="https://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"));

//Once twttr is ready, bind a callback function to the tweet event
twttr.ready(function(twttr) {       
    twttr.events.bind('tweet', function (event) {
        console.log('tweet, tweet');
    });
});
</script>

回答by michael

yes, there is a callback function. for custom twitter buttons as well. you can find it here: https://dev.twitter.com/discussions/671

是的,有一个回调函数。也用于自定义推特按钮。你可以在这里找到它:https: //dev.twitter.com/discussions/671

回答by Muhammad Tahir

function loguser( 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', loguser);
});

Working Example

工作示例

回答by yessa

<a href="https://twitter.com/IamAnessaReyes" class="twitter-follow-button" data-show-count="false">Follow @IamAnessaReyes</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>