// ==UserScript== // @name GitHub Pulse Sort By // @namespace faleij // @description adds sorting option to repo pulse // @include https://github.com/* // @version 1.1.1 // @grant none // @run-at document-end // @downloadURL https://update.greasyfork.icu/scripts/16467/GitHub%20Pulse%20Sort%20By.user.js // @updateURL https://update.greasyfork.icu/scripts/16467/GitHub%20Pulse%20Sort%20By.meta.js // ==/UserScript== /* jshint esnext:true, node:true, browser:true */ /* globals $ */ 'use strict'; const menuItems = [{ selected: true, id: 'sort_changed', value: 'time', text: 'Last Changed', sort: (a, b) => new Date($('time:first', a).attr('datetime')) > new Date($('time:first', b).attr('datetime')) },{ id: 'sort_assignee', value: 'assignee', text: 'Assignee', sort: (a, b) => $('.assignee:first', a).text().localeCompare($('.assignee:first', b).text()) },{ id: 'sort_created', value: 'num', text: 'Created', sort: (a, b) => parseInt($('.num:first', a).text().substr(1)) > parseInt($('.num:first', b).text().substr(1)) }]; let getMenuItemHTML = (args) => `
${args.text}
`; const menuHTML = `
`; const renderProgressHTML = (total, at) => ` ${at} of ${total} `; const target = document.querySelector('#js-repo-pjax-container'); const mutationHandler = () => { console.log('event handler', location.pathname.endsWith('/pulse')); if (/\/pulse\/|$/.test(location.pathname) && !$('.faleijs-sort-by-menu', target).length) create(); }; const observer = new MutationObserver(mutationHandler); observer.observe(target, { childList: true }); mutationHandler(); function create() { // Load assignees and progress $('li>.title').each((index, title) => $.get($(title).attr('href')).then(data => { data = $(data); let total = $('.comment-body:first input[type=checkbox]', data); if (total.length) { $(title).parent().append(renderProgressHTML(total.length, total.filter('[checked]').length)); } $(title).parent().append($('.assignee', data).parent().css('float', 'right')); })); $('input:radio[name="sortBy"]', $(menuHTML).prependTo('.header-with-actions')).change(MenuChangeHandler); } function MenuChangeHandler() { let menuItem = menuItems.find(el => el.value === this.value); $('.repository-content ul').each((index, ul) => $('li', ul).sort(menuItem.sort).appendTo(ul)); }