Sort Flags

Allows you to sort flags by on your user flag page

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Sort Flags
// @author         AstroCB
// @description    Allows you to sort flags by on your user flag page
// @version        1.0
// @namespace  https://github.com/AstroCB
// @include        *://*.stackexchange.com/users/flag-summary/*
// @include        *://*stackoverflow.com/users/flag-summary/*
// @include        *://*serverfault.com/users/flag-summary/*
// @include        *://*superuser.com/users/flag-summary/*
// @include        *://*askubuntu.com/users/flag-summary/*
// @include        *://*stackapps.com/users/flag-summary/*
// ==/UserScript==

function showFlagType(type) {
	var pn = parseInt($(".pager.fr > a:nth-last-child(2)").attr("href").replace("?page=", ""));
	$("#mainbar").text("");
	for (var i = 1; i < pn + 2; i++) {
		$("#mainbar").append('<div id="mainbar' + i + '"></div>');
		if (type === "All") {
			$("#mainbar" + i).load(location.href + "?page=" + i + " #mainbar", function () {
				$(".pager.fr").remove()
			});
		} else {
			$("#mainbar" + i).load(location.href + "?page=" + i + " #mainbar > .flagged-post:has(span." + type + ")", function () {
				$(".pager.fr").remove()
			});
		}
	}
}

function showDeclined() {
	showFlagType("Declined");

	// Update button text and hide other buttons
	$("#flag-sort-declined").text("declined");
	hide(["disputed", "helpful", "all"]);
}

function showDisputed() {
	showFlagType("Disputed");

	// Update button text and hide other buttons
	$("#flag-sort-disputed").text("disputed");
	hide(["declined", "helpful", "all"]);
}

function showHelpful() {
	showFlagType("Helpful");

	// Update button text and hide other buttons
	$("#flag-sort-helpful").text("helpful");
	hide(["declined", "disputed", "all"]);
}

function showAll() {
	showFlagType("All");

	// Update button text and hide other buttons
	$("#flag-sort-all").text("all");
	hide(["declined", "disputed", "helpful"]);
}

function hide(buttons) { // Hide other buttons when one is clicked
	for (var i = 0; i < buttons.length; i++) {
		$("#flag-sort-" + buttons[i]).hide();
	}
}

function main() {
	// Button styling
	var style = document.createElement("style");
	style.type = "text/css";
	style.innerText = ".flag-sort { margin-left: 25px; cursor: pointer; }";
	document.getElementsByTagName("head")[0].appendChild(style);

	// Declined
	var declined = document.createElement("span");
	declined.innerText = "show declined";
	declined.setAttribute("id", "flag-sort-declined");
	declined.setAttribute("class", "flag-sort mod-flag-indicator");
	declined.style.backgroundColor = "#f00" // Spam red
	declined.onclick = showDeclined;

	// Disputed
	var disputed = document.createElement("span");
	disputed.innerText = "show disputed";
	disputed.setAttribute("id", "flag-sort-disputed");
	disputed.setAttribute("class", "flag-sort mod-flag-indicator");
	disputed.style.backgroundColor = "#f90" // Supernova orange
	disputed.onclick = showDisputed;

	// Helpful
	var helpful = document.createElement("span");
	helpful.innerText = "show helpful";
	helpful.setAttribute("id", "flag-sort-helpful");
	helpful.setAttribute("class", "flag-sort mod-flag-indicator");
	helpful.style.backgroundColor = "#33a030" // Rep green
	helpful.onclick = showHelpful;

	// All
	var all = document.createElement("span");
	all.innerText = "show all";
	all.setAttribute("id", "flag-sort-all");
	all.setAttribute("class", "flag-sort mod-flag-indicator");
	all.style.backgroundColor = "#07d" // Bounty blue
	all.onclick = showAll;

	// Container to be appended for the buttons
	var container = document.createElement("div");
	container.style.display = "inline-block";
	container.appendChild(declined);
	container.appendChild(disputed);
	container.appendChild(helpful);
	container.appendChild(all);

	document.getElementsByClassName("subheader")[0].children[0].appendChild(container);
}

// Inject helper functions
var functions = showFlagType.toString() + "\n\n" + showDeclined.toString() + "\n\n" + showDisputed.toString() + "\n\n" + showHelpful.toString() + "\n\n" + showAll.toString() + "\n\n" + hide.toString();
var functionScript = document.createElement('script');
functionScript.type = "text/javascript";
functionScript.textContent = functions;
document.body.appendChild(functionScript);

// Inject the main script
var script = document.createElement('script');
script.type = "text/javascript";
script.textContent = "(" + main.toString() + ")();";
document.body.appendChild(script);