// ==UserScript== // @name Better GitHub Co-Authors // @name:zh-CN GitHub 共同协作者 // @description Adds a button to GitHub pull requests to make it simple to add co-author credits for all PR participants before merging. // @description:zh-CN 在 GitHub PR中添加一个按钮,以便在合并之前将所有 PR 参与者添加为共同作者. // @author delucis,人民的勤务员 // @namespace https://github.com/ChinaGodMan/UserScripts // @supportURL https://github.com/ChinaGodMan/UserScripts/issues // @homepageURL https://github.com/ChinaGodMan/UserScripts // @license MIT // @match https://github.com/* // @icon https://raw.githubusercontent.com/ChinaGodMan/UserScriptsHistory/main/scriptsIcon/github-co-authors.png // @compatible chrome // @compatible firefox // @compatible edge // @compatible opera // @compatible safari // @compatible kiwi // @compatible qq // @compatible via // @compatible brave // @version 2025.5.27.1 // @grant GM_setValue // @grant GM_getValue // @downloadURL none // ==/UserScript== /** * File: github-co-authors.user.js * Project: UserScripts * File Created: 2025/05/27,Tuesday 22:47:12 * Author: Chris Swithinbank(swithinbank@gmail.com),人民的勤务员@ChinaGodMan (china.qinwuyuan@gmail.com) * ----- * Last Modified: 2025/05/27,Tuesday 23:16:14 * Modified By: 人民的勤务员@ChinaGodMan (china.qinwuyuan@gmail.com) * ----- * License: MIT License * Copyright © 2024 - 2025 ChinaGodMan,Inc */ let GITHUB_TOKEN = GM_getValue('GITHUB_TOKEN', '') if (!GITHUB_TOKEN) { GM_setValue('GITHUB_TOKEN', prompt('Please enter your GitHub Token')) GITHUB_TOKEN = GM_getValue('GITHUB_TOKEN', '') } // Run the script addCoAuthorsButtonToMergeForm() // Re-run the script when navigating between pages document.addEventListener('turbo:render', addCoAuthorsButtonToMergeForm) /** * Create the UI for adding co-authors. * @param {HTMLElement} root * @param {Element | null} newMergeExperienceContainer */ function createCoAuthorsUI(root, newMergeExperienceContainer) { const banner = document.createElement('div') banner.setAttribute('aria-live', 'polite') banner.classList.add('color-fg-subtle') let bannerTimeout /** * Display a temporary status update about the progress adding co-authors. * @param {string} msg The message to display * @param {'subtle' | 'success' | 'danger'} color The status color to use. */ const displayStatus = (msg, color = 'subtle') => { clearTimeout(bannerTimeout) banner.textContent = msg banner.className = `color-fg-${color}` bannerTimeout = setTimeout(() => (banner.textContent = ''), 5000) } const button = document.createElement('button') button.textContent = 'Add co-authors' button.classList.add('Button', 'Button--secondary', 'Button--small') button.type = 'button' button.addEventListener('click', async () => { displayStatus('Loading co-authors…') try { const { message, count } = await getCoAuthors() /** @type {HTMLTextAreaElement | null} */ const textArea = newMergeExperienceContainer?.querySelector('textarea') ?? root.querySelector('textarea#merge_message_field') if (!textArea) { throw new Error('Couldn’t find commit message