Page MenuHomePhabricator
Paste P11638

smaller_gerritbot_comments.js
ActivePublic

Authored by JMeybohm on Jun 23 2020, 7:55 AM.
Referenced Files
F45167515: smaller_gerritbot_comments.js
Mon, Apr 8, 8:39 AM
F45166635: smaller_gerritbot_comments.js
Mon, Apr 8, 8:33 AM
F45166575: smaller_gerritbot_comments.js
Mon, Apr 8, 8:32 AM
F43315569: smaller_gerritbot_comments.js
Mon, Mar 25, 10:18 AM
F37112872: smaller_gerritbot_comments.js
Jun 22 2023, 4:22 PM
F37112864: smaller_gerritbot_comments.js
Jun 22 2023, 4:02 PM
F34259389: smaller_gerritbot_comments.js
Apr 6 2021, 12:23 PM
F31943340: raw.txt
Jul 21 2020, 4:56 PM
Tokens
"Barnstar" token, awarded by taavi.
// ==UserScript==
// @name Smaller bot comments
// @version 6
// @description Changes bot comments into one-liners.
// @downloadURL https://phabricator.wikimedia.org/paste/raw/11638/
// @match https://phabricator.wikimedia.org/*
// @grant none
// ==/UserScript==
const gitlabUrl = "https://gitlab.wikimedia.org/repos/"
const olderHidden = document.getElementsByClassName("phui-timeline-older-transactions-are-hidden");
// This MutationObserver is used to monitor the removal of the
// "Show Older Changes" banner which will get removed from DOM after
// all older changes have been loaded and added.
function handleMutation(mutationsList, observer) {
mutationsList.forEach(mutation => {
mutation.removedNodes.forEach(node => {
// Check if the removed node is the show-older-block
if (node.getAttribute('data-sigil') === 'show-older-block') {
main();
}
});
});
}
function getCleanTitle(me) {
// Remove the text node: "added a comment."
title = me.querySelector(".phui-timeline-title")
title.childNodes.forEach(n => {
if (n.nodeType == 3) {
n.remove()
}
});
return title
}
function handleGerritbot(me) {
content = me.querySelectorAll(".phabricator-remarkup p")
if (content[0].textContent.includes(gitlabUrl)) {
// gerritbot seems to have handled gitlab changes in the past
return handleCodeReviewBot(me)
} else {
// Comments from gerrit changes
lastIDX = content.length - 1
link = content[lastIDX].children[0]
text = []
content.forEach(function(e, i){
if (i < lastIDX) {
text = text.concat(e.textContent.split('\n'))
}
})
textContent = text[0].trim()
textTooltip = text[1].trim()
changeId = textContent.match(/^Change #?(\d+)/)[1]
newText = textContent.replace(/^Change #?\d+ (.*):$/, '$1')
link.text = changeId
// Add repo name and commit message as title for the changeId link
link.setAttribute('title', textTooltip)
title = getCleanTitle(me)
title.append(": Change ", link, " ", newText)
return true
}
}
function handleCodeReviewBot(me) {
content = me.querySelectorAll(".phabricator-remarkup p")
// Comments from gitlab changes
link = content[0].querySelector(".remarkup-link")
actionLine = content[0].textContent.replace(link.href, "").trim().split(" ")
action = actionLine.pop()
user = actionLine.join(" ")
// Remove the gitlab base URL from link rext
link.text = link.href.replace(gitlabUrl, "")
// Add the commit message as title for the link
link.setAttribute('title', content[1].textContent)
title = getCleanTitle(me)
title.append(": MR ", link, " ", action, " by ", user)
return true
}
function handleStashbot(me) {
content = me.querySelector(".phabricator-remarkup p")
salSpan = content.querySelector("span")
ircChannel = salSpan.textContent.match(/.*\((#\S+)\)/)[1]
salURL = salSpan.querySelector("a").href
salSpan.remove()
salLink = document.createElement("a")
salLink.setAttribute("href", salURL)
salLink.setAttribute("target", "_blank")
salLink.text = ircChannel
title = getCleanTitle(me)
title.append(" ", salLink, ": ")
// remove the timestamp from text node
content.childNodes.forEach(n => {
if (n.nodeType == 3) {
n.textContent = n.textContent.trimLeft().replace(/\[[\d-TZ:]+\] /, '')
}
})
title.append(...content.childNodes)
return true
}
function main() {
const majorEvents = document.querySelectorAll(".phui-timeline-major-event")
majorEvents.forEach(me => {
let modified = false
if (me.querySelector(".phui-handle").text == 'gerritbot') {
modified = handleGerritbot(me)
}
if (me.querySelector(".phui-handle").text == 'Stashbot') {
modified = handleStashbot(me)
}
if (me.querySelector(".phui-handle").text == 'CodeReviewBot') {
modified = handleCodeReviewBot(me)
}
if (modified) {
// remove the "quote" menu on the right
me.querySelector(".phui-timeline-menu").remove()
// remove the original comment content
me.querySelector(".phui-timeline-core-content").remove()
// make this event minor (one-liner)
me.classList.replace("phui-timeline-major-event", "phui-timeline-minor-event")
}
});
}
// Remove irrelevant stuff from page title
// ⚓ T307943
document.title = document.title.replace(/⚓ T\d+ /, "");
// Run the main function
main();
// Register observers to watch for loading of older events
// which might happen on click or when a anchor to an older
// event is in the URL.
if (olderHidden.length > 0) {
const observer = new MutationObserver(handleMutation);
const observerConfig = { childList: true };
for (let ohb of olderHidden) {
observer.observe(ohb.parentNode, observerConfig);
}
}

Event Timeline

JMeybohm edited the content of this paste. (Show Details)

I seem unable to screenshot the tooltip, but it contains the repo name and the commit message.

Screenshot_20200624_082225.png (505×892 px, 88 KB)

JMeybohm edited the content of this paste. (Show Details)