您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Sort comments by timestamp on Soundcloud
当前为
- // ==UserScript==
- // @name Soundcloud:Sort comments by timestamp
- // @description Sort comments by timestamp on Soundcloud
- // @include *soundcloud.com/*
- // @grant none
- // @namespace https://greasyfork.org/users/4252
- // @version 0.0.1.20170208182105
- // ==/UserScript==
- //Note: Only been tested in firefox
- var SortButton = document.createElement("button");
- SortButton.type = "button";
- SortButton.className = "sc-button sc-button-medium sc-button-responsive";
- SortButton.innerHTML = "Sort by timestamp";
- SortButton.onclick = sortcomments;
- window.addEventListener('load', WindowLoadedSortButton, false);
- function WindowLoadedSortButton() {
- if (document.getElementsByClassName("commentsList__title").length != 0) { //if comment header exists
- document.getElementsByClassName("commentsList__title")[0].appendChild(SortButton);
- }
- }
- function sortcomments() {
- if (document.getElementsByClassName("paging-eof").length == 0) {
- alert("Please scroll all the way to the bottom so that all comments load before running this script");
- return;
- }
- var commentContainer = document.getElementsByClassName("lazyLoadingList__list")[0];
- var allcomments = [].slice.call(commentContainer.children);
- k = 0.01; //decimal to stick at end of timestamp so that threads (replies) stay together
- for (i = 0; i < allcomments.length; i++) {
- if (allcomments[i].firstChild.classList.contains("isReply")) {
- allcomments[i].setAttribute("timestamp4sort", getTimestampInSeconds(allcomments[i]) + k);
- k = k + 0.01; //theoretically correctly sort 100 consecutive replies
- } else {
- allcomments[i].setAttribute("timestamp4sort", getTimestampInSeconds(allcomments[i]));
- k = 0.01; //reset
- }
- }
- allcomments.sort(compare);
- while (commentContainer.lastChild) {
- commentContainer.removeChild(commentContainer.lastChild);
- }
- for (i = 0; i < allcomments.length; i++) {
- commentContainer.appendChild(allcomments[i]);
- }
- alert("sorted");
- }
- function compare(a, b) {
- var avalue = parseFloat(a.getAttribute("timestamp4sort"));
- var bvalue = parseFloat(b.getAttribute("timestamp4sort"));
- if (avalue < bvalue)
- return -1;
- if (avalue > bvalue)
- return 1;
- return 0;
- }
- function hmsToSecondsOnly(str) { //This function handles "HH:MM:SS" as well as "MM:SS" or "SS".
- var p = str.split(':'),
- s = 0,
- m = 1;
- while (p.length > 0) {
- s += m * parseInt(p.pop(), 10);
- m *= 60;
- }
- return s;
- }
- function getTimestampInSeconds(licomment) { //takes the <li> element of a comment. returns an integer
- if (licomment.getElementsByClassName("commentItem__timestampLink").length != 0) {
- return hmsToSecondsOnly(licomment.getElementsByClassName("commentItem__timestampLink")[0].innerHTML);
- } else {
- return 0;
- }
- }