Skip to content

Instantly share code, notes, and snippets.

@jpbochi
Last active July 28, 2022 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpbochi/ac88177a0a4741300c2f24c4e33a9c90 to your computer and use it in GitHub Desktop.
Save jpbochi/ac88177a0a4741300c2f24c4e33a9c90 to your computer and use it in GitHub Desktop.
On GitHub PR inline comments, changes default button to "Add single comment"
// ==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);
})();
@jpbochi
Copy link
Author

jpbochi commented Jul 28, 2022

Preview of what this gist will do:

image

@jpbochi
Copy link
Author

jpbochi commented Jul 28, 2022

If you are using this on a private corporate github website, add its url to your "User matches" after you install the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment