Last active
August 19, 2024 00:55
-
-
Save jpbochi/ac88177a0a4741300c2f24c4e33a9c90 to your computer and use it in GitHub Desktop.
On GitHub PR inline comments, changes default button to "Add single comment"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Only Single Comments | |
// @namespace https://gist.githubusercontent.com/jpbochi/ac88177a0a4741300c2f24c4e33a9c90 | |
// @version 1.0.5 | |
// @description On GitHub PR inline comments, changes the default button from "Start a review" to "Add single comment" | |
// @author JP Bochi | |
// @match https://github.com/*/*/pull/* | |
// @icon https://www.google.com/s2/favicons?domain=github.com | |
// @downloadURL https://gist.githubusercontent.com/jpbochi/ac88177a0a4741300c2f24c4e33a9c90/raw/only-single-comments.js | |
// @updateURL https://gist.githubusercontent.com/jpbochi/ac88177a0a4741300c2f24c4e33a9c90/raw/only-single-comments.js | |
// @grant none | |
// ==/UserScript== | |
/** | |
* Made for https://www.tampermonkey.net | |
*/ | |
(function () { | |
'use strict'; | |
const defaultToSingleComment = (form) => { | |
console.info('=>> Fixing form…', form); | |
const existing = form.querySelector('input[type="hidden"][name="single_comment"]'); | |
if (existing) return; | |
const newInput = document.createElement('input'); | |
newInput.setAttribute('type', 'hidden'); | |
newInput.setAttribute('name', 'single_comment'); | |
newInput.setAttribute('value', '1'); | |
form.insertBefore(newInput, form.firstChild) | |
form.querySelector('.form-actions .btn-primary')?.classList.remove('btn-primary'); | |
form.querySelector('.form-actions button[name="single_comment"')?.classList.add('btn-primary') | |
console.info('=>> Fixed inline comment form.'); | |
}; | |
const observe = (mutations, _observer) => { | |
const addedNodes = Array.from(mutations) | |
.filter(mutation => (mutation.type === 'childList')) | |
.map(mutation => mutation.addedNodes) | |
.flatMap(addedNodes => Array.from(addedNodes || [])); | |
console.info(`=>> Running only-single-comments script on ${addedNodes.length} new nodes…`); | |
addedNodes | |
.filter(added => added.querySelectorAll) | |
.flatMap(added => Array.from(added.querySelectorAll('form.js-inline-comment-form'))) | |
.forEach(defaultToSingleComment); | |
}; | |
const config = { attributes: false, childList: true, subtree: true }; | |
const targetNode = document.getElementById('repo-content-pjax-container'); | |
// Fix any pre-existing forms | |
Array.from(targetNode.querySelectorAll('form.js-inline-comment-form')) | |
.forEach(defaultToSingleComment); | |
// Watch for new forms | |
new MutationObserver(observe).observe(targetNode, config); | |
})(); |
If you are using this on a private corporate github website, add its url to your "User matches" after you install the script.
This is awesome! Thank you for this. I had to make one change since it looks like GitHub has updated their CSS:
- form.querySelector('.form-actions .btn-primary')?.classList.remove('btn-primary');
- form.querySelector('.form-actions button[name="single_comment"')?.classList.add('btn-primary')
+ form.querySelector('.form-actions .Button--primary')?.classList.add('Button--secondary');
+ form.querySelector('.form-actions .Button--primary')?.classList.remove('Button--primary');
+ form.querySelector('.form-actions button[name="single_comment"')?.classList.remove('Button--secondary')
+ form.querySelector('.form-actions button[name="single_comment"')?.classList.add('Button--primary')
I actually moved this to a repo. I should have updated the gist earlier.
https://github.com/jpbochi/user-scripts/blob/master/only-single-comments.js
Oh, awesome! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview of what this gist will do: