Pull request comment context

When viewing a commit in a PR, automatically add the current URL to newly opened comments, to show the context in which the reviewer made the comment.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Pull request comment context
// @namespace    http://stderr.nl
// @version      2025-09-12
// @description  When viewing a commit in a PR, automatically add the current URL to newly opened comments, to show the context in which the reviewer made the comment.
// @author       Matthijs Kooijman <[email protected]>
// @license      CC0-1.0-Universal, https://creativecommons.org/publicdomain/zero/1.0/
// @homepage     https://codeberg.org/matthijs/greasemonkey-pr-comment-context
// @match        https://github.com/*
// @grant        none
// ==/UserScript==

// This script has no URL filter above, since github dynamically loads pages, so we also dynamically match the current URL
const URL_MATCH = /https:\/\/github.com\/[^\/]*\/[^\/]*\/pull\/[^\/]*\/commits\/.*/;

(function() {
    'use strict';

    const PREFIX = "Comment about "

    function process(textarea) {
        if (textarea.textContent.startsWith(PREFIX)) {
            console.log("Not modifiying textarea, already has prefix", textarea);
        } else {
            console.log("Adding URL to textarea", textarea);
            textarea.prepend(PREFIX + window.location + "\n\n");
        }
    }

    new MutationObserver(function(mutationsList, observer) {
        if (!document.location.toString().match(URL_MATCH)) {
            return;
        }

        for(const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                for(const node of mutation.addedNodes) {
                    if (node.classList && node.classList.contains('inline-comments')) {
                        const textarea = node.querySelector('textarea.js-comment-field');
                        process(textarea);
                    }
                }
            }
        }
    }).observe(document.body, {childList: true, subtree: true});
})();