Skip to content

Instantly share code, notes, and snippets.

@yorickvP
Last active June 28, 2018 11:56
Show Gist options
  • Save yorickvP/e69775e5b8997054103b855aea4103aa to your computer and use it in GitHub Desktop.
Save yorickvP/e69775e5b8997054103b855aea4103aa to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GH-PR-Approvals
// @namespace http://serokell.io
// @version 0.5
// @updateURL https://gist.githubusercontent.com/yorickvP/e69775e5b8997054103b855aea4103aa/raw/
// @description adds approval count to github PRs
// @author You
// @match https://github.com/*
// @run-at document-start
// @grant unsafeWindow
// ==/UserScript==
/* jshint esnext: false, asi: true */
/* jshint esversion: 6 */
const matchers = []
function checkShouldRun(location=window.location.href) {
matchers.forEach(({regex, on, off}) => {
if(!!regex.exec(location))
on(location)
else off(location)
})
}
matchers.push({
regex: /^https:\/\/github.com\/serokell\/[a-zA-Z0-9-]+(\/pull.*|\/commit.*|)/,
on: ()=> observer.observe(document, { subtree: true, childList: true, attributes: false }),
off: ()=> observer.disconnect()
})
const oldPushState = unsafeWindow.history.pushState
unsafeWindow.history.pushState = function(state, name, url) {
checkShouldRun(url)
oldPushState.apply(this, arguments)
}
const issue_regex = /([\s\[\(]|^)((OPS|AD|SD)-\d+)([\s$\]\)])/gi
const selector = "a.muted-link.tooltipped[aria-label$=\"approval\"],a.muted-link.tooltipped[aria-label$=\"approvals\"]"
const observer = new MutationObserver(unbounce(visitall))
function visitall() {
//const t0 = performance.now()
visitall_selectorlist([".js-issue-row", selector], elem => {
if (elem.textContent.trim() == "Approved") {
elem.textContent = "Approved by " + elem.getAttribute("aria-label").match(/(\d+) review/)[1]
}
})
visitall_selectorlist([".js-issue-row", "a.h4"], replace_issues)
visitall_selectorlist([".js-issue-title"], replace_issues)
visitall_selectorlist([".markdown-body", "p"], replace_issues)
visitall_selectorlist(["a.message"], replace_issues)
visitall_selectorlist(["p.commit-title"], replace_issues)
visitall_selectorlist([".ghpr-stop-click"], elem => {
// stop github's onclick handler
elem.onclick = event => event.stopPropagation()
})
//const t1 = performance.now()
//console.log("took", (t1-t0), "ms")
}
function replace_issues(elem) {
if (elem.className == "message") console.log(elem)
if (issue_regex.exec(elem.innerHTML) && elem.children.length == 0) {
// onclick in html gets blocked by CSP, add a classname
var nw = elem.innerHTML.replace(issue_regex, "$1<a href=https://issues.serokell.io/issue/$2 target=_blank class=ghpr-stop-click style=color:blue>$2</a>$4")
elem.innerHTML = nw
}
}
function unbounce(fun, delay=100) {
// only call function every x milliseconds
let timeout
return function() {
if (timeout) return
timeout = setTimeout(x=> {
timeout = null
fun.apply(this, arguments)
}, delay)
}
}
function qsa_optim(n, x) {
// queryselectorall is way slower than getElementsByClassName and getElementById in trivial cases
if (x.match(/^\.[a-zA-Z-]$/)) return n.getElementsByClassName(x.slice(1))
else if (x.match(/^#[a-zA-Z-]$/)) return [n.getElementById(x.slice(1))]
else return n.querySelectorAll(x)
}
function visitall_selectorlist([s, ...sels], fun, root=document) {
// DFS on selector list
if (!s) fun(root)
else for (const node of qsa_optim(root, s)) {
visitall_selectorlist(sels, fun, node)
}
}
checkShouldRun()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment