Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kubuxu/eb873d14a4626a54fa34a52229c80ee2 to your computer and use it in GitHub Desktop.
Save Kubuxu/eb873d14a4626a54fa34a52229c80ee2 to your computer and use it in GitHub Desktop.
Userscrip which adds a button to GitHub Beta Notifications Dashboard for unsubscribing from repos
// ==UserScript==
// @name GitHub Notifications Beta Automatic Unsubscribe (Local)
// @namespace https://kubuxu.com/
// @version 0.2
// @description Adds a button to GitHub Beta Notifications Dashboard for unsubscribing from repos
// @author @Kubuxu
// @match https://github.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
(function() {
'use strict';
window.gnbau = {};
window.gnbau.logging = false;
function log(...args) {
if (window.gnbau.logging) {
console.log(...args)
}
}
var $ = window.jQuery;
const hashUnsubSpecial = '#gnbau--unsubscribe'
let unsubIcon = $('<svg class="octicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path d="m4.182 4.31.016.011 10.104 7.316.013.01 1.375.996a.75.75 0 1 1-.88 1.214L13.626 13H2.518a1.516 1.516 0 0 1-1.263-2.36l1.703-2.554A.255.255 0 0 0 3 7.947V5.305L.31 3.357a.75.75 0 1 1 .88-1.214Zm7.373 7.19L4.5 6.391v1.556c0 .346-.102.683-.294.97l-1.703 2.556a.017.017 0 0 0-.003.01c0 .005.002.009.005.012l.006.004.007.001ZM8 1.5c-.997 0-1.895.416-2.534 1.086A.75.75 0 1 1 4.38 1.55 5 5 0 0 1 13 5v2.373a.75.75 0 0 1-1.5 0V5A3.5 3.5 0 0 0 8 1.5ZM8 16a2 2 0 0 1-1.985-1.75c-.017-.137.097-.25.235-.25h3.5c.138 0 .252.113.235.25A2 2 0 0 1 8 16Z"></path></svg>')
//unsubIcon = $('<span class="text-center d-inline-block"></span>').append(unsubIcon)
function attachUnsubButtons() {
log("attaching buttons")
$('.js-notifications-group').has(':contains("Mark as done")').each(function() {
const group = $(this)
if (group.find('.unsub-button').length != 0) {
return
}
const usubLocation = group.find('a').attr('href') + hashUnsubSpecial;
let unsubButton = $('<div>', {
class: 'btn btn-sm mr-2 unsub-button',
text: ' Usubscribe',
}).on('click', function() {
window.open(usubLocation)
})
unsubButton.prepend(unsubIcon.clone())
group.find('form.js-grouped-notifications-mark-all-read-button').before(unsubButton)
})
return
}
function unsubscribe() {
if ($('button[aria-label="Watching a repository"][disabled="disabled"]').length != 0) {
log('waiting to click unsub')
setTimeout(unsubscribe, 100)
return
}
$('.notifications-component-menu-modal button').has(':contains("Participating")').trigger('click')
log('clicked unsub')
waitUnsub()
}
function waitUnsub() {
if($('span[data-target="notifications-list-subscription-form.watchButtonCopy"]:visible').length == 0) {
log('waiting for unsub to happen')
setTimeout(waitUnsub, 100)
return
}
window.close();
}
function work() {
if (window.location == 'https://github.com/notifications') {
attachUnsubButtons()
setTimeout(attachUnsubButtons, 1500)
} else if (window.location.hash == hashUnsubSpecial) {
unsubscribe()
}
}
{
let lastUrl = location.href
new MutationObserver(() => {
const url = location.href
if (url !== lastUrl) {
lastUrl = url
work()
}
}).observe(document, {subtree: true, childList: true})
}
work()
})();
/*
<button aria-label="Watching a repository" type="button" disabled="disabled" data-view-component="true" class="tooltipped tooltipped-n Button--secondary Button--small Button"> <span class="Button-content">
<span class="Button-visual Button-leadingVisual">
<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-eye">
<path d="M8 2c1.981 0 3.671.992 4.933 2.078 1.27 1.091 2.187 2.345 2.637 3.023a1.62 1.62 0 0 1 0 1.798c-.45.678-1.367 1.932-2.637 3.023C11.67 13.008 9.981 14 8 14c-1.981 0-3.671-.992-4.933-2.078C1.797 10.83.88 9.576.43 8.898a1.62 1.62 0 0 1 0-1.798c.45-.677 1.367-1.931 2.637-3.022C4.33 2.992 6.019 2 8 2ZM1.679 7.932a.12.12 0 0 0 0 .136c.411.622 1.241 1.75 2.366 2.717C5.176 11.758 6.527 12.5 8 12.5c1.473 0 2.825-.742 3.955-1.715 1.124-.967 1.954-2.096 2.366-2.717a.12.12 0 0 0 0-.136c-.412-.621-1.242-1.75-2.366-2.717C10.824 4.242 9.473 3.5 8 3.5c-1.473 0-2.825.742-3.955 1.715-1.124.967-1.954 2.096-2.366 2.717ZM8 10a2 2 0 1 1-.001-3.999A2 2 0 0 1 8 10Z"></path>
</svg>
</span>
<span class="Button-label">Watch</span>
</span>
</button>
button.tooltipped
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment