By default, this script is going to grab the first 5 pages of the On Video forum and sort them by their creation date. Then it will replace all existing topics on the front page with the ordered topics going up to page 5. You can change the number of pages this script will get by updating the numPages variable in the script. The higher this value is set, the longer it will take to load the page.
NOTE: This only works on the first page of On Video, going to page 2 will cause issues.
This tool runs in a chrome extension called Tampermonkey that will run javascript code when a webpage loads. Currently this tool only works for the On Video section of the forum since that's all I use, but it will probably work in other areas with some small modifications.
Installation instructions:
1. Install the Tampermonkey extension for chrome
2. Open up the Tampermonkey dashboard
3. Click the + button next to the Install Userscripts tab
4. Delete all of the existing code in this editor box
5. Paste code from below into the editor box and save the script
Code: Select all
// ==UserScript==
// @name Milovana Sorter
// @version 0.3
// @description Sort Milovana topics by creation date
// @author Expression
// @match https://milovana.com/forum/viewforum.php?f=25
// @grant none
// ==/UserScript==
(function() {
'use strict';
function requestUrl(url) { /* send request to url and sends each post element to buildList processing */
const http = new XMLHttpRequest();
http.open("GET", url);
http.send();
http.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
var temp_element = document.createElement("html");
temp_element.innerHTML = http.responseText;
var elementList = temp_element.getElementsByClassName("topiclist topics")[1].childNodes;
elementList.forEach(buildList);
}
}
}
var post_arr = [];
var display_arr = [];
function getDate(el) { /* get date from element */
for(var i = 0; i < el.length; i++) {
for(var j = 0; j < el[i].children.length; j++) {
if(el[i].children[j].nodeName == "TIME") {
return el[i].children[j].dateTime;
}
}
}
return "ERROR";
}
var count = 0;
function buildList(el, idx, arr) { /* scrapes data from topics and stores it in post_arr or display_arr for sticky topics, immediately storing stickies retains their order at the top*/
let match = /(?<=t\=)\d+(?=$)/;
if(el.nodeType == 1) {
try {
count++;
var className = el.className;
var indexOffset = ((el.children[0].childNodes[1].children[0].className !== "list-inner") ? 1 : 0);
var data = el.children[0].childNodes[1].children[0 + indexOffset].children;
var url = data[0 + indexOffset].href;
var postId = url.match(match)[0];
var postDate = Date.parse(getDate(data));
if(className.includes("sticky")) {
display_arr.push(el);
} else {
post_arr.push([postDate, postId, el, className]);
}
} catch {
return;
}
}
}
async function loop_requests() { /* requests first 5 pages of forum topics */
var loopIncrement = 50;
var numPages = 5;
var count = 0;
var req_url = "https://milovana.com/forum/viewforum.php?f=25&start=";
for(var i = 0; i <= numPages; i++) {
count++;
var postFrom = i * loopIncrement;
if(count > 100) {
break;
}
var url = req_url + postFrom;
await requestUrl(url);
}
}
loop_requests();
function replacePosts() { /* injects posts from an display_arr into the webpage */
document.getElementsByClassName("topiclist topics")[1].innerHTML = "";
for(var i = 0; i <= display_arr.length; i++) {
try {
document.getElementsByClassName("topiclist topics")[1].appendChild(display_arr[i]);
} catch {}
}
}
function getSortedElements() { /* sort elements in descending order by post date in post_arr and push them into display_arr */
var sorted = post_arr.sort(function(a,b) {
return a[0]-b[0]
}).reverse();
sorted.forEach(function(el, idx, arr) {
display_arr.push(el[2]);
});
replacePosts(display_arr);
}
var timeout = setInterval(function() { /* check if post_arr can be sorted by checking its length */
if(post_arr.length > 200) {
getSortedElements();
clearInterval(timeout);
}
}, 100);
})();
Before turning on topic sorter:
After: