Github News Feed Filter

Add filters for Github homepage news feed items

目前为 2014-03-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Github News Feed Filter
  3. // @namespace https://github.com/jerone/UserScripts
  4. // @description Add filters for Github homepage news feed items
  5. // @author jerone
  6. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_News_Feed_Filter
  7. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_News_Feed_Filter
  8. // @include http*://github.com/
  9. // @version 1
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15.  
  16. function proxy(fn) {
  17. return function () {
  18. var that = this;
  19. return function (e) {
  20. var args = that.slice(0); // clone;
  21. args.unshift(e); // prepend event;
  22. fn.apply(this, args);
  23. };
  24. }.call([].slice.call(arguments, 1));
  25. }
  26.  
  27. function addFilters() {
  28. var container;
  29. if (!(container = document.querySelector(".news"))) return;
  30.  
  31. var ul = document.createElement("ul");
  32. ul.classList.add("dashboard-tabs");
  33. [{ text: "", icon: "octicon-comment-discussion", filter: ["*"] },
  34. { text: "Comments", icon: "octicon-comment", filter: ["issues_comment"] },
  35. { text: "Commits", icon: "octicon-git-commit", filter: ["push"] },
  36. { text: "Issue actions", icon: "octicon-issue-opened", filter: ["issues_opened", "issues_closed"] }
  37. ].forEach(function (item) {
  38. var li = document.createElement("li");
  39. var a = document.createElement("a");
  40. a.classList.add("js-selected-navigation-item");
  41. a.setAttribute("href", "/");
  42. a.setAttribute("title", item.filter.join(" & "));
  43. var s = document.createElement("span");
  44. s.classList.add("octicon", item.icon);
  45. if (item.filter == "*") {
  46. li.style.cssFloat = "left";
  47. li.style.width = "49px";
  48. a.classList.add("selected");
  49. } else {
  50. s.style.marginRight = "6px";
  51. }
  52. a.appendChild(s);
  53. a.appendChild(document.createTextNode(item.text));
  54. a.addEventListener("click", proxy(function (e, filter) {
  55. e.preventDefault();
  56. Array.forEach(container.querySelectorAll(".selected"), function (m) {
  57. m.classList.remove("selected");
  58. });
  59. this.classList.add("selected");
  60. var alerts = container.querySelectorAll(".alert");
  61. Array.filter(alerts, function (alert) {
  62. alert.style.display = filter == "*" || filter.some(function (c) {
  63. return alert.classList.contains(c);
  64. }) ? "block" : "none";
  65. });
  66. return false;
  67. }, item.filter));
  68. li.appendChild(a);
  69. ul.appendChild(li);
  70. });
  71.  
  72. container.insertBefore(ul, container.firstChild);
  73.  
  74. // update on clicking "More"-button;
  75. var event = new Event("click");
  76. $.pageUpdate(function () {
  77. window.setTimeout(function () {
  78. container.querySelector(".selected").dispatchEvent(event);
  79. }, 1);
  80. });
  81. }
  82.  
  83. // init;
  84. addFilters();
  85.  
  86. // on pjax;
  87. $(document).on('pjax:success', addFilters);
  88.  
  89. })();