Skip to content

Instantly share code, notes, and snippets.

@jamesmartin
Last active April 28, 2020 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmartin/22605878fa4c1e30002d49fe56fdfff5 to your computer and use it in GitHub Desktop.
Save jamesmartin/22605878fa4c1e30002d49fe56fdfff5 to your computer and use it in GitHub Desktop.
Safari Userscript for GitHub Notifications v2
// For use with https://github.com/quoid/userscripts
// and https://github.com/notifications
function removeNotificationShelf() {
if (window.location.href.match(/github\.com\/.*/)) {
const notificationShelf = document.querySelector('.js-notification-shelf')
if (notificationShelf) {
console.log("Removing the notifications shelf!")
notificationShelf.parentNode.removeChild(notificationShelf)
}
}
}
// Wait a bit because the notification shelf pops in after a second or so
setTimeout(removeNotificationShelf, 1500)
// There are SSO prompt boxes in different formats on the main /notifications
// page and the /notifications?query= pages :shrug:
function removeSsoWarnings() {
const inlineSsoWarnings = document.querySelectorAll(".notifications-list-item svg.octicon-shield-lock")
if (inlineSsoWarnings) {
inlineSsoWarnings.forEach(function(el) {
const li = el.parentNode
li.parentNode.removeChild(li)
})
}
const floatingSsoWarnings = document.querySelectorAll(".js-notifications-container .note svg.octicon-shield-lock")
if (floatingSsoWarnings) {
floatingSsoWarnings.forEach(function(el) {
const box = el.parentNode.parentNode
box.parentNode.removeChild(box)
})
}
}
function bindOpenInTabKeyboardShortCut() {
console.log('(re) binding keyboard shortcuts')
const notifications = document.querySelectorAll('.js-notifications-list-item .js-navigation-open')
notifications.forEach(function(node) {
node.setAttribute('target', '_blank')
})
document.addEventListener('keydown', function(event) {
// The `t` keyboard shortcut opens a notification in a new browser tab
if (event.which == 84) {
const focusedNotification = document.querySelector('.js-notifications-list-item.navigation-focus')
if (focusedNotification) {
// Pretend this notification has been read to account for the UI not
// updating right away when something is marked as "done".
focusedNotification.classList.remove('notification-unread')
focusedNotification.classList.add('notification-read')
const notificationUrl = focusedNotification.querySelector('.js-navigation-open')
if (notificationUrl) {
console.log("Opening...")
const clicked = notificationUrl.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true
})
)
if (clicked) {
notificationUrl.click()
window.focus()
}
} else {
console.log("This notification doesn't have a URL?")
}
} else {
console.log("No notification selected")
}
}
})
}
if (window.location.href.match(/github\.com\/notifications.*/)) {
removeSsoWarnings()
bindOpenInTabKeyboardShortCut()
setInterval(function() {
removeSsoWarnings()
bindOpenInTabKeyboardShortCut()
}, 3000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment