Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F43315569
smaller_gerritbot_comments.js
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
JMeybohm
Mar 25 2024, 10:18 AM
2024-03-25 10:18:06 (UTC+0)
Size
3 KB
Referenced Files
None
Subscribers
None
smaller_gerritbot_comments.js
View Options
// ==UserScript==
// @name Smaller gerritbot comments
// @version 5
// @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."
;
const
gitlabUrl
=
"https://gitlab.wikimedia.org/repos/"
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
}
// Remove irrelevant stuff from page title
// ⚓ T307943
document
.
title
=
document
.
title
.
replace
(
/⚓ T\d+ /
,
""
);
majorEvents
.
forEach
(
me
=>
{
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"
)
}
});
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15072450
Default Alt Text
smaller_gerritbot_comments.js (3 KB)
Attached To
Mode
P11638 smaller_gerritbot_comments.js
Attached
Detach File
Event Timeline
Log In to Comment