【问题标题】:Post dynamic twitter updates发布动态推特更新
【发布时间】:2012-02-29 03:40:28
【问题描述】:

我目前在我的页面上有一些脚本,可以解析来自我的在线广播电台的标题/艺术家信息。我使用 html 将其显示为纯文本

<span id="song_title"></span>

如何获取进入 span id 的动态信息并将其用于“发布到 twitter”链接,以便听众可以在 Twitter 上分享当前歌曲标题?

我做了一些研究,发现在 twitter 上发帖有一些变化,但我没有发布这个动态文本。

这是脚本代码:

<!-- Begin Now Playing Script -->
            <script>
            (function () {
                // we need a JSON parser, if it does not exist, load it
                if (typeof JSON == "undefined") {
                    var s = document.createElement("script");
                    // json2.js retrieved from https://github.com/douglascrockford/JSON-js
                    s.src = "json2.js";
                    document.getElementsByTagName("head").appendChild(s);
                }
            })();
            var song_ends = 0;
            function update_song () {
                if ((new Date).getTime() < song_ends) {
                    // use cached result as the song has not ended yet
                    return;
                }
                var req = new XMLHttpRequest();
                // IE compatbility:
                var textContent = 'textContent' in document ? 'textContent' : 'innerText';
                req.onreadystatechange = function () {
                    if (req.readyState == 4) {
                        var song = JSON.parse(req.responseText);
                        if (song.title) {
                            var img = document.getElementById("song_image");
                                if(song.image.src){
                            img.alt = song.image.alt;
                            img.src = song.image.src;
                            img.width = 100;
                            img.height = 100;
                                }else{
                            img.src="images/default_art.png";
                            img.width = 100;
                            img.height = 100;
                                }
                            document.getElementById("song_title")[textContent]  = song.title ;
                            document.getElementById("song_artist")[textContent] = song.artist;
                            document.getElementById("song_next")[textContent]  = song.next ;
                            // store the end date in javascript date format
                            song_ends = (new Date).getTime() + song.wait_ms;
                        }
                    }
                };
                req.open('get', 'php/PlayingNow.php', true);
                req.send(null);
            }
            // poll for changes every 20 seconds
            setInterval(update_song, 20000);
            // and update the song information
            update_song();
            </script>
<!-- End Now Playing Script -->

我希望能够像这样将它发布到 Twitter:目前正在收听 (song_artist) 的 (song_title)

这是上面脚本中引用的 PHP 文件的代码:

<?php // filename: PlayingNow.php
$json = null;
$cache = 'song.json';
// if there is no usuable cache
if (!$json) {
    // retrieve the contents of the URL
    $ch = curl_init('http://bigcountry.streamon.fm/card');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($res);
    // if the title exists, assume the result to be valid
    if ($json && $json->title) {
        // cache it
        $fp = fopen('song.json', 'w');
        fwrite($fp, $res);
        fclose($fp);
    } else {
        $json = null;
    }
}
if ($json) {
    $info = array();
    // contains the time in milliseconds
    $info['wait_ms'] = $json->interval->ends_at - 1000 * microtime(true);
    $info['title']   = $json->title ;
    $info['artist']  = $json->artist;
    $info['album']   = $json->album ;
    $info['next']   = $json->next_song;
    $info['image']   = $json->album_art;
    // display a JSON response for the HTML page
    echo json_encode($info);
}
?>

【问题讨论】:

  • 您需要的不仅仅是 javscript 来发帖。
  • @Ibu:对不起,我不明白你的意思。
  • 我的意思是php部分是你需要调试的部分。你得到什么错误?
  • 我从未说过我遇到了错误。
  • 您是否知道,您发布的两个链接都去了哪里?他们坏了。

标签: javascript twitter html


【解决方案1】:

执行此操作的“正确”方法是使用 Twitter 的 Web Intents,它是专门为这种情况设计的。看看"Tweet or Reply to a Tweet" 部分。在实践中,您只需在页面中包含 Web Intents 脚本 (http://platform.twitter.com/widgets.js),创建一个链接,然后设置其 href,例如:

var link = document.createElement('a');
link.innerHTML = "Link Text";
link.href = 'http://twitter.com/intent/tweet?text=Currently listening to "' + songTitle + '" by ' + songArtist;

var parentElement = document.getElementById('SOME_ELEMENTS_ID');
parentElement.appendChild(link);

如果您还希望推文包含您网站的 URL,您可以添加 url 参数。

【讨论】:

  • 让我确定我有这个权利。我将
  • 您使用 document.createElement 创建一个链接,就像您在上面的 JavaScript 代码中使用 script 元素一样。我已经更新了我的答案以更具说明性。这是基本的 DOM 操作。 MDC 有很好的关于在 DOM 中创建和操作元素的信息:developer.mozilla.org/en/DOM/document.createElement
  • 创建该链接后,如何在我的网站上下文中使用它?
  • 这是一个相当宽泛的问题,Luke,恐怕我无法用你给我的信息和这个回复框给我的 600 个字符来回答这个问题。我希望我已经为您提供了一些有用的信息,但我无法进一步指导您。
猜你喜欢
  • 1970-01-01
  • 2013-10-01
  • 2011-06-19
  • 2011-12-05
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
相关资源
最近更新 更多