Skip to content

Instantly share code, notes, and snippets.

@artnc
Last active November 1, 2017 20:56
Show Gist options
  • Save artnc/2b65fbd5ab12d6ea0bbef08e749d1b42 to your computer and use it in GitHub Desktop.
Save artnc/2b65fbd5ab12d6ea0bbef08e749d1b42 to your computer and use it in GitHub Desktop.
GitHub pull request self-unassignment bookmarklet

Create a new browser bookmark and set its URL to this:

javascript:function _asyncToGenerator(b){return function(){var c=b.apply(this,arguments);return new Promise(function(e,f){function g(h,i){try{var j=c[h](i),k=j.value}catch(l){return void f(l)}return j.done?void e(k):Promise.resolve(k).then(function(l){g('next',l)},function(l){g('throw',l)})}return g('next')})}}_asyncToGenerator(regeneratorRuntime.mark(function b(){var c,e,f,g,h,i,j,k,l,m,n;return regeneratorRuntime.wrap(function(p){for(;;)switch(p.prev=p.next){case 0:if(c=function(q){return Array.prototype.slice.call(q)},e=function(q){return new Promise(function(r){return setTimeout(r,q)})},f=document.documentElement.scrollTop,g=document.querySelectorAll('.sidebar-assignee')[1],h=c(g.querySelectorAll('p .assignee')),i=document.querySelector('meta[name=user-login]').content,h.some(function(q){return q.textContent===i})){p.next=9;break}return alert('You\'re not assigned!'),p.abrupt('return');case 9:j=g.querySelector('.discussion-sidebar-toggle'),j.click(),k=[];case 12:if(k.length){p.next=18;break}return k=c(g.querySelectorAll('.selected .js-username')),p.next=16,e(200);case 16:p.next=12;break;case 18:return l=k.find(function(q){return q.textContent===i}),m=l.closest('.select-menu-item'),m.click(),p.next=23,e(100);case 23:j.click(),n=document.querySelector('.sidebar-notifications button'),/Unsubscribe/.test(n.textContent)&&n.click(),document.documentElement.scrollTop=f;case 27:case'end':return p.stop();}},b,void 0)}))();

Clicking the bookmarklet while viewing a PR's "Conversation" tab will unassign and unsubscribe you.

Tested in Chrome only.

// To generate a bookmarklet out of this, run the code through
// https://babeljs.io/repl and then prepend `javascript:`
(async () => {
// Helper functions
const asArray = arrayLike => Array.prototype.slice.call(arrayLike);
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
// Remember current scroll position of page
const scrollPosition = document.documentElement.scrollTop;
// Check that self is assigned
const sidebar = document.querySelectorAll('.sidebar-assignee')[1];
const assigneeLinks = asArray(sidebar.querySelectorAll('p .assignee'));
const myUsername = document.querySelector('meta[name=user-login]').content;
if (!assigneeLinks.some(a => a.textContent === myUsername)) {
alert('You\'re not assigned!');
return;
}
// Open "Assignees" dropdown
const assigneeButton = sidebar.querySelector('.discussion-sidebar-toggle');
assigneeButton.click();
// Wait until dropdown contents become available
let userSpans = [];
while (!userSpans.length) {
userSpans = asArray(sidebar.querySelectorAll('.selected .js-username'));
await sleep(200);
}
// Unassign self
const mySpan = userSpans.find(d => d.textContent === myUsername);
const myDiv = mySpan.closest('.select-menu-item');
myDiv.click();
await sleep(100);
// Close "Assignees" dropdown
assigneeButton.click();
// Unsubscribe from notifications
const followButton = document.querySelector('.sidebar-notifications button');
/Unsubscribe/.test(followButton.textContent) && followButton.click();
// Restore original scroll position
document.documentElement.scrollTop = scrollPosition;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment