适用于soundcloud的Jquery Quick Hack

时间:2014-04-09 09:46:17

标签: javascript jquery soundcloud code-snippets

我是一个箱子挖掘机,soundcloud是一个很好的工具,可以找到一些新的酷道。 找到ID的最好方法是查看注释,但是有一个问题,它们都有时间戳,但无法用这个时间戳对它们进行排序。

有没有一种快速方法可以直接在chrome js控制台中使用jquery做到这一点?

在评论页面中,每条评论的结构都是

<li class="commentsList__item">
  <div class="commentItem sc-media large">
    <a class="sc-media-image commentItem__avatar" href="/username">
        <img src="[...]" width="40" height="40" alt="Users’s avatar">
    </a>
    <div class="commentItem__content sc-media-content">
      <span class="commentItem__createdAt sc-text-light">
        <time class="relativeTime" title="Posted on 9 April 2014 11:15" datetime="2014-04-09T09:15:44.000Z">
          <span class="sc-visuallyhidden">Posted 21 mins ago</span>
          <span aria-hidden="true">21 mins</span>
        </time>
      </span>
      <div class="commentItem__controls"> <a href="/platform/[nameofthetrack]/comments/[commentID]" class="sc-button sc-button-reply sc-button-medium commentItem__replyButton" title="Open this comment">Reply</a> 
      </div>
      <div class="commentItem__body sc-font-light"> <a href="/[username]" title="Visit [Username]’s profile" class="commentItem__username sc-link-dark">[Username]</a> 
        <span>[Comment]</span>
        <span class="sc-text-light commentItem__timestamp">at 10.08</span>
      </div>
    </div>
  </div>
</li>

我想要排序的数据是:

<span class="sc-text-light commentItem__timestamp">at 10.08</span>

通过这个小js片段,我将能够在歌曲中快速找到trackrequest和trackid。 你明白我的意思吗?

1 个答案:

答案 0 :(得分:0)

我会尝试这样的事情:

var comments = [];
// iterate on every comment item
$(".commentItem").each(function() {
    // get the time under the YYYY-MM-DDTHH:MM:SS format
    var datestr = $(this).find(".commentItem__timestamp").text();
    var time = parseInt(datestr.replace("at ","").replace(".",""));
    // key is the corresponding integer (10.08 => 1008)
    comments[time] = $(this);
});

var keys = Object.keys(comments);
keys.sort();

var result = [];

len = keys.length;

for (i = 0; i < len; i++)
{
    result.push(comments[keys[i]]);
}

// now you have a sorted array (from the oldest to the most recent)
// you can use the reverse method to reverse the sorting.