Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Soundcloud: Sort comments by timestamp
- // @description Sort comments by timestamp on Soundcloud
- // @include *soundcloud.com/*
- // @grant none
- // @run-at document-idle
- // @namespace https://greasyfork.org/users/4252
- // @version 0.0.3
- // ==/UserScript==
- var SortButton = document.createElement("button");
- SortButton.type = "button";
- SortButton.className = "sort-button sc-button sc-button-medium sc-button-responsive";
- SortButton.style = "float: right; margin-bottom: 6px;";
- SortButton.innerHTML = "Sort by timestamp";
- SortButton.onclick = sortcomments;
- // window.onload doesn't work for soundcloud which is AJAX
- // popstate event doesn't work reliably either.
- // So let's use XHR as a proxy for page load event
- // Basic AJAX interceptor by hijacking XMLHttpRequest.prototype.open
- (function() {
- XMLHttpRequest.prototype.__open = XMLHttpRequest.prototype.open;
- XMLHttpRequest.prototype.open = function() {
- this.addEventListener('load', function() {
- if(this.readyState !== 4) return;
- if(this.status !== 200) return;
- if(/\/(tracks)/.test(this.responseURL)) {
- appendSortButton();
- }
- });
- XMLHttpRequest.prototype.__open.apply(this, arguments);
- };
- })();
- function appendSortButton() {
- if (document.getElementsByClassName("commentsList__title").length && !document.getElementsByClassName("sort-button")[0]) {
- console.info("Added sort button!");
- 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]);
- }
- console.info("Comments sorted successfully");
- }
- 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__timestamp").length !== 0) {
- return hmsToSecondsOnly(licomment.getElementsByClassName("commentItem__timestamp")[0].innerHTML.replace("says at ", "").slice(0, -1)); //we slice of the last character to change "1:32:25:" to "1:32:25"
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement