// ==UserScript== // @name Github Notifications Dropdown // @namespace joeytwiddle // @copyright 2014, Paul "Joey" Clark (http://neuralyte.org/~joey) // @version 0.8.3 // @description When clicking the notifications icon, displays notifications in a dropdown pane, without leaving the current page. (Now also makes files in diff views collapsable.) // @include https://github.com/* // @grant none // @downloadURL none // ==/UserScript== // bug: If the notifications list is longer than the page, scroll down to the bottom and then try to click on the white space below the Github document's content. The event does not fire there! var mainNotificationsPath = "/notifications"; var notificationButtonLink = $(".header a.notification-indicator[href]"); var notificationButtonContainer = notificationButtonLink.parent(); var closeClickTargets = $("body, .header a.notification-indicator[href]"); var notificationsDropdown = null; var tabArrow = null; function listenForNotificationClick(){ notificationButtonContainer.on("click", onNotificationButtonClicked); } function onNotificationButtonClicked(evt){ // Act normally, do nothing, if modifier key is pressed. if (evt.ctrlKey || evt.shiftKey || evt.metaKey) { return; } evt.preventDefault(); notificationButtonContainer.off("click", onNotificationButtonClicked); var targetPage = notificationButtonLink.attr('href'); fetchNotifications(targetPage); } function fetchNotifications(targetPage){ notificationButtonContainer.css({ "opacity": "0.3", "background-color": "#ececec", "background-image": "linear-gradient(#d9d9d9, #ececec)" }); $.ajax({ url: targetPage, dataType: "html" }).then(receiveNotificationsPage.bind(null,targetPage)).fail(receiveNotificationsPage); } function receiveNotificationsPage(targetPage, data, textStatus, jqXHR){ notificationButtonContainer.css("opacity", ""); notificationsDropdown = $("
").addClass("notifications-dropdown"); var title = "Notifications"; var extra = null; if (targetPage != mainNotificationsPath) { title += " for " + targetPage.replace(/^\/+|\/notifications$/g,''); } var titleElem = $('

').text(title); if (targetPage != mainNotificationsPath) { var buttonToSeeAll = $('').text('See all'); buttonToSeeAll.on('click', function(evt){ evt.preventDefault(); closeNotificationsDropdown(); fetchNotifications(mainNotificationsPath); }); titleElem.append( textNode(" ("), buttonToSeeAll, textNode(")") ); } notificationsDropdown.append( $("
").append(titleElem) ); var notificationPage = $("
").append( $.parseHTML(data) ); var notificationsList = notificationPage.find(".notifications-list"); // Provide hover text for all links, so if the text is too long to display, it can at least be seen on hover. notificationsList.find("a").each(function(){ $(this).attr("title", $(this).text().trim()); }); var minWidth = Math.min(750, window.innerWidth-48); if (notificationsList.children().length == 0) { notificationsDropdown.append("
No new notifications
"); minWidth = 0; } notificationsDropdown.append(notificationsList); var linkToPage = mainNotificationsPath; //var linkToPage = targetPage; var seeAll = $("
Notifications page
"); notificationsDropdown.append(seeAll); var arrowSize = 10; $("