Skip to content

Instantly share code, notes, and snippets.

@heyitsaamir
Last active October 15, 2024 21:03
Show Gist options
  • Save heyitsaamir/d9e36e23ee2402e6d41e92903827bf32 to your computer and use it in GitHub Desktop.
Save heyitsaamir/d9e36e23ee2402e6d41e92903827bf32 to your computer and use it in GitHub Desktop.
Github Title Selector
/* globals jQuery, $, waitForKeyElements */
// ==UserScript==
// @name Github Title Selector
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Aamir Jawaid
// @match https://github.com/*/pull/*
// @icon https://www.google.com/s2/favicons?domain=tampermonkey.net
// @grant none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @include https://github.com/*/pull/*
// ==/UserScript==
/*
When you click on the title of a github pull request,
this script will copy the title, diff and url to your clipboard.
Use with tapermonkey.
*/
function copyToClipboard(text) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val(text).select();
document.execCommand("copy");
$temp.remove();
}
(function() {
'use strict';
$( ".gh-header-title" ).click(function() {
const titleDOM = $('span.markdown-title');
if (!titleDOM) return;
const title = (titleDOM.length) ? titleDOM[0].innerText.trim() : titleDOM.innerText.trim();
const diffStatDOM = $('span#diffstat');
if (!diffStatDOM) return;
const diffStat = (diffStatDOM.length) ? diffStatDOM[0].innerText.trim() : diffStatDOM.innerText.trim();
const url = window.location.href;
const text = `\`${title}\` (${diffStat}) ${url}`;
copyToClipboard(text);
});
})();
@camflint
Copy link

Thanks for this!!

@vikram-k9
Copy link

Awesome!! Just built an updated version from this:

/* globals jQuery, $, waitForKeyElements */

// ==UserScript==
// @name         Github Title Selector
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Copy pasteable slack summary for Github PR/Pull Request link
// @author       Vikram
// @match        https://github.com/*/pull/*
// @icon         https://www.google.com/s2/favicons?domain=tampermonkey.net
// @grant        none
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @include      https://github.com/*/pull/*
// ==/UserScript==


/*
  When you click on the title of a github pull request,
  this script will copy the title, diff and url to your clipboard.
  Use with tampermonkey.
*/

function copyToClipboard(text) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val(text).select();
  document.execCommand("copy");
  $temp.remove();
}

(function() {
    'use strict';
    $( ".gh-header-title" ).click(function() {
        const reviewTitleDOM = $('title');
        const githubReviewURL = window.location.href;
        const numLinesAddedDOM = $('span.color-fg-success');
        const numLinesRemovedDOM = $('span.color-fg-danger');

        if (!reviewTitleDOM || !githubReviewURL) return;
        if (!numLinesAddedDOM || !numLinesRemovedDOM) return;

        // Something like: Fix that bug thing by john-doe · Pull Request #123 · companyName/taco
        const reviewTitleOverall = reviewTitleDOM.length ? reviewTitleDOM[0].innerText : reviewTitleDOM.innerText;
        const splitReview = reviewTitleOverall.split("·");

        if (splitReview.length !== 3) {
            console.log(`Github Title Selector didn't find 3 components in summary split by ·: ${reviewTitleOverall}. Failed copy`);
            return;
        }

        const splitReviewTitle = splitReview[0].split("by");
        const reviewTitle = splitReviewTitle[0]; // Fix that bug thing
        if (splitReviewTitle.length !== 2) {
            console.log(`Github Title Selector found extra "by" in title. Might have truncation: ${reviewTitleOverall}. Continuing`);
        }
        const pullRequestNumber = splitReview[1].split("#")[1].trim(); // 123
        const repositoryName = splitReview[2].split("/")[1]; // taco

        const numLinesAdded = numLinesAddedDOM.length ? numLinesAddedDOM[0].innerText.trim() : numLinesAddedDOM.innerText.trim();
        const numLinesRemoved = numLinesRemovedDOM.length ? numLinesRemovedDOM[0].innerText.trim() : numLinesRemovedDOM.innerText.trim();

        // Something like: (+56/-45)
        const diffNums = `(${numLinesAdded}/${numLinesRemoved})`;

        const text = `[${reviewTitle}](${githubReviewURL}) \`${diffNums}\` *[${repositoryName} PR#${pullRequestNumber}]*`;

        copyToClipboard(text);
    });
})();

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