Skip to content

Instantly share code, notes, and snippets.

@chuckhoffmann
Last active May 11, 2017 11:59
Show Gist options
  • Save chuckhoffmann/7f6b3e027f20ebcc9feb49493967201f to your computer and use it in GitHub Desktop.
Save chuckhoffmann/7f6b3e027f20ebcc9feb49493967201f to your computer and use it in GitHub Desktop.
A userscript to add functionality to the Fairfax Underground forum.
// ==UserScript==
// @name FFXU Format Fixer
// @namespace http://tampermonkey.net/
// @version 0.9
// @description Add functionality to Fairfax Underground forum.
// @author Chuck Hoffmann
// @match http://www.fairfaxunderground.com/forum/list/2/*
// @match http://www.fairfaxunderground.com/forum/list/2.html
// @match http://www.fairfaxunderground.com/forum/list.php?2
// @grant none
// ==/UserScript==
(function() {
'use strict';
//**
//Get a list of all the table row elements, loop through them,
//getting the fourth td element in each row, and append our custom
//HTML:
//<br><span class="PhorumListSubText>Started <<date>> </span>
//to those elements
//**
let rowList = document.getElementsByTagName("tr");
addCustomStyles();
addBranding();
checkForDb();
for(let rowCtr = 1, len = rowList.length; rowCtr < len; rowCtr++){
let rowCells = rowList[rowCtr].getElementsByTagName("td");
if (rowCells.length == 5){
processTableRow(rowCells);
}
}
function checkForDb(){
//Check if the database exists and if not, ask if user wants to load it
if (localStorage.length === 0) {
let loadDBCheck = confirm("No values in database! Do you want to load it now?");
if(loadDBCheck){
loadDb();
}
}
}
function loadDb(){
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200){
let db = xhttp.responseText;
let dbObj = JSON.parse(db);
for(let x in dbObj){localStorage.setItem(x,dbObj[x]);}
}
};
xhttp.open("GET","http://www.fairfaxunderground.com/forum/file.php?2,file=283889,filename=ffxuformatfixerjson.txt");
xhttp.send();
}
function addCustomStyles(){
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = ".highlightyellow {background-color: #ffe;}" +
".highlightred {background-color: #fee;}";
document.getElementsByTagName("head")[0].appendChild(style);
}
function addBranding(){
let brandDiv = document.createElement("div");
brandDiv.className = "PhorumNavBlock";
let brandSpan = document.createElement("span");
brandSpan.className = "PhorumHeadingLeft";
let brandText = document.createTextNode("FFXU Format Fixer");
brandSpan.appendChild(brandText);
brandDiv.appendChild(brandSpan);
brandDiv.appendChild(document.createTextNode(" by Chuck Hoffmann"));
document.body.insertBefore(brandDiv, document.body.firstChild);
}
function makeKeyFromURL(threadURL){
//Given a URL of the form http://www.fairfaxunderground.com/forum/read/2/1279153.html
//extract the thread number and return it as a string
return (threadURL.split("/")[6]).replace(".html","");
}
function processTableRow(rowCells){
//Expects a array-like collection of five td elements
let threadURL = rowCells[0].getElementsByTagName("a")[0].getAttribute("href");
let dbKey = makeKeyFromURL(threadURL);
let createdDate = localStorage.getItem(dbKey);
if (createdDate !== null){
addDatetoStartedByCell(rowCells[3],createdDate);
} else {
//The created date for this thread is not in localStorage
//Test if thread URL is a sticky
let subjectPrefix = rowCells[0].getElementsByClassName("PhorumListSubjPrefix");
if (subjectPrefix.length === 0 || subjectPrefix[0].innerText == "Sticky:"){
for(let cellCtr=0;cellCtr<5;cellCtr++){
//Highlight all cells in a table row for which a URL needs to be retrieved
rowCells[cellCtr].classList.add("highlightyellow");
}
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
let threadPage = this.responseXML;
let startDate = threadPage.getElementsByClassName("PhorumReadBodyHead")[1].textContent.replace("Date: ","");
let lsKey = makeKeyFromURL(threadURL);
localStorage.setItem(lsKey,startDate);
addDatetoStartedByCell(rowCells[3],startDate);
//Remove highlighting
for(let cellCtr=0;cellCtr<5;cellCtr++){
rowCells[cellCtr].classList.remove("highlightyellow");
}
}
};
xhttp.open("GET",threadURL,true);
xhttp.responseType = "document";
xhttp.send();
}
}
return;
}
function addDatetoStartedByCell(startedByCell, threadStartedDate){
//insert customHTML
let startedElem = document.createElement("span");
let startedText = document.createTextNode("Started " + threadStartedDate);
startedElem.className = "PhorumListSubText";
startedElem.appendChild(startedText);
startedByCell.classList.add("PhorumSmallFont");
startedByCell.appendChild(document.createElement("br"));
startedByCell.appendChild(startedElem);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment