Skip to content

Instantly share code, notes, and snippets.

@Semx11
Created October 22, 2017 22:42
Show Gist options
  • Save Semx11/0478a14db3ddbe519110f0f7b475113c to your computer and use it in GitHub Desktop.
Save Semx11/0478a14db3ddbe519110f0f7b475113c to your computer and use it in GitHub Desktop.
Userscript for Tampermonkey (ECMAScript 5) that allows you to go to the first staff post in a thread by clicking on the "shield". requested by some guy called Fourth
// ==UserScript==
// @name Hypixel Forums Clickable Shields
// @namespace http://semx11.me/
// @version 0.1
// @description Click on the 'staff shield' to take you to the first staff post in a thread.
// @author Semx11
// @match https://hypixel.net/forums/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var running = false;
// praise stackoverflow
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
// pointer cursor for shields
addGlobalStyle("span.hasStaffPost{cursor:pointer;}");
// recursive function, run once per page
function getStaffPost(threadLink, progressSpan, page = 1, maxPages = 0) {
// check page bounds
if (page > maxPages && maxPages > 0) {
progressSpan.html("Not found.");
setTimeout(function() {
progressSpan.remove();
running = false;
}, 2000);
return;
}
// get page
var threadContent = $.ajax({
url: threadLink + "page-" + page,
dataType: "html"
});
// wait for request completion
threadContent.done(function(html) {
var site = $($.parseHTML(html));
// set max pages if not already set
if (maxPages < 1) {
if (site.find("span.pageNavHeader").length !== 0) {
maxPages = parseInt(site.find("span.pageNavHeader").html().toString().replace("Page 1 of ", ""));
} else {
maxPages = 1;
}
}
// check if post author has rank-badge-admin
var staffPosts = site.find(".messageList > li").has("div.player.playerRank > span.rank-badge-admin");
progressSpan.html("Searching (" + page + "/" + maxPages + ")");
// check staff post count
if (staffPosts.length !== 0) {
progressSpan.html("Post found! (" + page + "/" + maxPages + ")");
running = false;
// get first staff post, get post permalink, and click it
staffPosts.find(".datePermalink")[0].click();
return;
}
// recursive, recursive, recursive ...
getStaffPost(threadLink, progressSpan, ++page, maxPages);
});
}
// shorthand for $(document).ready( ... );
$(function() {
// click listener for shields
$("span.hasStaffPost").click(function() {
if (running) {
return;
}
running = true;
// get dom elements used for status and getting thread link
var iconKeyDiv = $(this).parent();
var threadLink = "https://hypixel.net/" + iconKeyDiv.siblings().find("a.PreviewTooltip").attr("href");
// append span for status
iconKeyDiv.append('<span id="loading" style="text-indent: 0; width: auto">Loading</span>');
getStaffPost(threadLink, iconKeyDiv.find("span#loading"), 1);
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment