Page MenuHomePhabricator
Authored By
JMeybohm
Jul 21 2020, 4:56 PM
Size
3 KB
Referenced Files
None
Subscribers
None
// ==UserScript==
// @name Smaller gerritbot comments
// @version 1
// @description Changes gerritbot comments into one-liners.
// @match https://phabricator.wikimedia.org/*
// @grant none
// ==/UserScript==
const majorEvents = document.querySelectorAll(".phui-timeline-major-event");
const uselessMessage = "Your browser timezone setting differs from the timezone setting in your profile, click to reconcile.";
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")
//link = content[1].children[0]
link = content[content.length - 1].children[0]
text = content[0].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)
}
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)
}
// Remove the "Your browser timezone" alert
// The popup is probably created by some JS after this script is executed.
// No idea how to catch that, so I just retry later...
var timezoneNotificationInterval;
var timezoneNotificationIntervalMax = 10;
function removeTimezoneNotification() {
if (timezoneNotificationIntervalMax <= 1) {
clearInterval(timezoneNotificationInterval);
}
timezoneNotificationIntervalMax -= 1;
document.querySelectorAll(".jx-notification-alert").forEach(notification => {
if (notification.textContent == uselessMessage) {
notification.remove();
// Don't run this again if the message was removed
clearInterval(timezoneNotificationInterval);
}
});
}
timezoneNotificationInterval = setInterval(removeTimezoneNotification, 500)
majorEvents.forEach(me => {
modified = false
if (me.querySelector(".phui-handle").text == 'gerritbot') {
handleGerritbot(me)
modified = true
}
if (me.querySelector(".phui-handle").text == 'Stashbot') {
handleStashbot(me)
modified = true
}
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")
}
});

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
8479699
Default Alt Text
raw.txt (3 KB)

Event Timeline