本文介绍了Twitter 嵌入式时间线回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对 Twitter 的嵌入式时间线进行大量研究.我一直试图弄清楚是否有可能知道时间线何时完成加载并显示在页面上.此问题已被请求,但尚未实施.我走到了死胡同,希望有人能够帮助我找到解决方案.这是我目前发现的:

I've been doing a lot of research into Twitter's Embedded Timelines. I've been trying to figure out if it is possible to know when the timeline is finished loading and displays on the page. This issue has been requested, but has yet to be implemented. I have come to a dead end an am hoping someone is able to help me find the solution. Here is what I have found so far:

添加嵌入式时间轴的代码:

The code to add an Embedded Timeline:

<a class="twitter-timeline" href="https://twitter.com/twitterapi" data-widget-id="YOUR-    WIDGET-ID-HERE">Tweets by @twitterapi</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>

运行脚本时会发生什么:

What happens when the script is run:

  1. 脚本将 标记替换为 元素.

  1. The script replaces the <a> tag with an <iframe> element.

iframe 的内容已准备就绪,但头像和关注@username"按钮尚未下载.iframeheight 属性设置为 0,这样您就看不到没有图像的内容.

The contents of the iframe are ready, but the avatar image and the 'follow @username' button are not done downloading. The iframe's height attribute is set to 0 so that you don't see the content without the images.

头像已下载,但关注@用户名"按钮仍在下载.内容仍然不可见.

The avatar image is downloaded, but the 'follow @username' button is still downloading. Content is still not visible.

'follow @username' 按钮下载完成.iframeheight 属性设置为匹配 iframe 的内容的高度.时间线现在可见.

The 'follow @username' button is finished downloading. The iframe's height attribute is set to match the height of the contents of the iframe. The timeline is now visible.

现在,我已经尝试了所有我能想到的方法来确定时间线何时可见(没有使用 settimeout/interval).目标是有一个非轮询事件/函数/回调.这是我迄今为止尝试过但没有成功的方法:

Now, I've tried everything I can think of to figure out when the timeline is visible (without using settimeout/interval). The goal is to have a non-polling event/function/callback. Here is what I have tried so far without success:

此时我可能忘记了更多组合.我知道存在一个使用 Twitter widget.jsgithub gist,但会为其添加回调.我无法让它工作.

There's probably a few more combinations I forgot at this point. I know there exists a github gist that uses the Twitter widget.js, but adds callbacks to it. I was unable to get this to work.

对于日志问题很抱歉,但我希望有人能够提供帮助.谢谢.

Sorry for the log question, but I hope someone is able to help. Thanks.

推荐答案

当你复制和粘贴从 Twitter 获得的代码时,你必须替换这个:

When you copy and paste the code you get from Twitter, you have to replace this:

<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>

有了这个:

<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";js.setAttribute('onload', "twttr.events.bind('rendered',function(e) {doSomething()});");fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

当然,上面调用的 doSomething 函数是您的回调,它会在您的嵌入式时间轴加载和呈现时运行.

Of course, the doSomething function called above is your callback that runs when your embedded timeline loads and renders.

另外,我猜这个解决方案在 Internet Explorer 中不起作用,因为它不支持脚本元素的 onLoad 事件.不过有解决方案.

Also, I guess this solution doesn't work in Internet Explorer since it doesn't support onLoad events for script elements. Yet there are solutions for that.

最后,您可以在我编写的 Twitter 嵌入式时间线抓取工具中看到我对此问题的处理方法.

这篇关于Twitter 嵌入式时间线回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 22:05