当用户从我的网页发布帖子时,如何运行活动?

时间:2020-12-07 21:15:08

I need to know when a user tweets my blog's content because I want to register that activity on the database, is there any way to do that? Using a simple button like this one?

我需要知道用户何时推特我的博客内容,因为我想在数据库上注册该活动,有没有办法做到这一点?使用像这样的简单按钮?

<a href="https://twitter.com/share" class="twitter-share-button" data-show-count="false">Tweet</a><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

Or how can I do it? Maybe using API's callback?

或者我该怎么办?也许使用API​​的回调?

1 个解决方案

#1


0  

At first I thinked about adding an event listener to the anchor, but that wouldn't work, because Twitter's API converts it to an iframe.

起初我想到将一个事件监听器添加到锚点,但这不起作用,因为Twitter的API将其转换为iframe。

Reading a bit the Twitter's JavaScript Widget API Docs I came up with this:

读一下Twitter的JavaScript Widget API文档,我想出了这个:

  twttr.ready(
  function(twttr) {
      twttr.events.bind('click', function(event) {
          //Here you can place your code for an AJAX call
          console.log('click on share button')
      });
  });

Codepen: http://codepen.io/anon/pen/grEBvr

Note: it triggers on click, so not when the user actually shared, didn't find an event for shared.

注意:它在点击时触发,因此当用户实际共享时,没有找到共享事件。

Note 2: had to remove the async attribute in the <script>, was giving issues like "twttr is undefined".

注意2:必须删除

#1


0  

At first I thinked about adding an event listener to the anchor, but that wouldn't work, because Twitter's API converts it to an iframe.

起初我想到将一个事件监听器添加到锚点,但这不起作用,因为Twitter的API将其转换为iframe。

Reading a bit the Twitter's JavaScript Widget API Docs I came up with this:

读一下Twitter的JavaScript Widget API文档,我想出了这个:

  twttr.ready(
  function(twttr) {
      twttr.events.bind('click', function(event) {
          //Here you can place your code for an AJAX call
          console.log('click on share button')
      });
  });

Codepen: http://codepen.io/anon/pen/grEBvr

Note: it triggers on click, so not when the user actually shared, didn't find an event for shared.

注意:它在点击时触发,因此当用户实际共享时,没有找到共享事件。

Note 2: had to remove the async attribute in the <script>, was giving issues like "twttr is undefined".

注意2:必须删除