Html 带有事件的 Twitter 按钮的自定义设计

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15413159/
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-29 06:27:23  来源:igfitidea点击:

Custom design for Twitter button with events

javascripthtmltwitter

提问by Mateu

I've got a custom tweet button:

我有一个自定义推文按钮:

<a href="http://twitter.com/share?url=http://example.com;text=myText;size=l&amp;count=none" target="_blank">
    <div>
        <img src="/assets/twitter-logo.jpg">
        <span>Twitter</span>
    </div>
</a>

Now I want to achieve some result after the tweet has been published. To do so, I've looked at the Twitter Events API:

现在我想在推文发布后取得一些成果。为此,我查看了 Twitter Events API:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<script type="text/javascript">
    twttr.events.bind('tweet', function (event) {
        alert("Tweet Successful");
    });
</script>

However, this API only works on non-custom buttons (twitter-share-buttonclass).

但是,此 API 仅适用于非自定义按钮(twitter-share-button类)。

Anyone has managed to create a custom button which listens to events?

任何人都设法创建了一个监听事件的自定义按钮?

回答by nmoliveira

You can have a custom link and still have the web intents!

您可以拥有自定义链接,但仍然拥有 Web 意图

The keyis, instead of your link pointing to "https://twitter.com/share" it should point to "https://twitter.com/intent/tweet"

关键的是,而不是你的链接指向的,以“ https://twitter.com/share”它应该指向“ https://twitter.com/intent/tweet

like this:

像这样:

<a href="https://twitter.com/intent/tweet">Tweet</a>

This way you can use the web intents like you were trying:

通过这种方式,您可以像尝试一样使用网络意图:

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

Check this jsFiddle

检查这个jsFiddle

回答by Adan Luna

I got some one with a share action

我有一个分享行动

<a href="https://twitter.com/share" class="twitter-share-button" data-via="pincheregio" data-size="large" data-count="none">Tweet</a>

<script>
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

      twttr.ready(function (twttr) {
        twttr.events.bind('tweet', function (event) {
        alert("paso");
      });

    });
</script>

http://jsfiddle.net/aXCNX/139/

http://jsfiddle.net/aXCNX/139/

回答by Developer

DO NOT WASTE your time with "tweet" event. Unlike other social networks, twitter will fire this even regardless was the tweet successfully completed or user just closed the window. So instead use code bellow:

不要在“推特”事件上浪费你的时间。与其他社交网络不同,即使推文成功完成或用户刚刚关闭了窗口,twitter 也会触发它。所以改用下面的代码:

jQuery('#twitter').off('click').on('click', function() {
  var field_list = {
    url: 'https://example.com/',
    text: 'Some random text',
    hashtags: 'myhashtag,anothertag',
  }
  window.open('https://twitter.com/share?'+jQuery.param(field_list), '_blank', 'width=550,height=420').focus();
  alert('Is it successful tweet or not? That is the question!');
});