Github News Feed Filter

Add filters for Github homepage news feed items

As of 2014-03-06. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Github News Feed Filter
// @namespace   https://github.com/jerone/UserScripts
// @description Add filters for Github homepage news feed items
// @author      jerone
// @homepage    https://github.com/jerone/UserScripts/tree/master/Github_News_Feed_Filter
// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_News_Feed_Filter
// @include     http*://github.com/
// @version     1
// @grant       none
// ==/UserScript==


(function () {

	function proxy(fn) {
		return function () {
			var that = this;
			return function (e) {
				var args = that.slice(0);  // clone;
				args.unshift(e);  // prepend event;
				fn.apply(this, args);
			};
		}.call([].slice.call(arguments, 1));
	}

	function addFilters() {
		var container;
		if (!(container = document.querySelector(".news"))) return;

		var ul = document.createElement("ul");
		ul.classList.add("dashboard-tabs");
		[{ text: "", icon: "octicon-comment-discussion", filter: ["*"] },
		 { text: "Comments", icon: "octicon-comment", filter: ["issues_comment"] },
		 { text: "Commits", icon: "octicon-git-commit", filter: ["push"] },
		 { text: "Issue actions", icon: "octicon-issue-opened", filter: ["issues_opened", "issues_closed"] }
		].forEach(function (item) {
			var li = document.createElement("li");
			var a = document.createElement("a");
			a.classList.add("js-selected-navigation-item");
			a.setAttribute("href", "/");
			a.setAttribute("title", item.filter.join(" & "));
			var s = document.createElement("span");
			s.classList.add("octicon", item.icon);
			if (item.filter == "*") {
				li.style.cssFloat = "left";
				li.style.width = "49px";
				a.classList.add("selected");
			} else {
				s.style.marginRight = "6px";
			}
			a.appendChild(s);
			a.appendChild(document.createTextNode(item.text));
			a.addEventListener("click", proxy(function (e, filter) {
				e.preventDefault();
				Array.forEach(container.querySelectorAll(".selected"), function (m) {
					m.classList.remove("selected");
				});
				this.classList.add("selected");
				var alerts = container.querySelectorAll(".alert");
				Array.filter(alerts, function (alert) {
					alert.style.display = filter == "*" || filter.some(function (c) {
						return alert.classList.contains(c);
					}) ? "block" : "none";
				});
				return false;
			}, item.filter));
			li.appendChild(a);
			ul.appendChild(li);
		});

		container.insertBefore(ul, container.firstChild);

		// update on clicking "More"-button;
		var event = new Event("click");
		$.pageUpdate(function () {
			window.setTimeout(function () {
				container.querySelector(".selected").dispatchEvent(event);
			}, 1);
		});
	}

	// init;
	addFilters();

	// on pjax;
	$(document).on('pjax:success', addFilters);

})();