drifterz28
12/19/2017 - 5:23 PM

Copy to clipboard

Copy to clipboard

// ==UserScript==
// @name         PR Copy
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://github.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    setTimeout(function() {
        var link = location.href;
        var title = document.querySelector('.js-issue-title').textContent.trim();
        var codeDiffAdd = document.querySelector('.diffstat .text-green').textContent.trim();
        var codeDiffSubtract = document.querySelector('.diffstat .text-red').textContent.trim();
        var headerActions = document.querySelector('.gh-header-actions');

        headerActions.insertAdjacentHTML('afterbegin', '<button type="button" class="btn btn-sm js-textareacopybtn" style="margin-left:10px;">Copy</button>');
        headerActions.insertAdjacentHTML('afterbegin', `<input type="text" autocomplete="off" style="float: left;" class="js-copytextarea" value="${link} - ${title} (${codeDiffAdd} ${codeDiffSubtract})">`);
        var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

        copyTextareaBtn.addEventListener('click', function(event) {
            var copyTextarea = document.querySelector('.js-copytextarea');
            copyTextarea.select();

            try {
                var successful = document.execCommand('copy');
                var msg = successful ? 'successful' : 'unsuccessful';
                console.log('Copying text command was ' + msg);
            } catch (err) {
                console.log('Oops, unable to copy');
            }
        });
    }, 500);
})();