diff --git a/.js/default.js b/.js/default.js index 34caba5..449fdf6 100644 --- a/.js/default.js +++ b/.js/default.js @@ -1,3 +1,15 @@ function addStyle(style) { $("body").after(``); +} + +function copyToClipBoard(text) { + let textArea = $(""); + textArea.css("opacity", "0"); + textArea.css("position", "fixed"); + textArea.val(text); + $("body").append(textArea); + + textArea.select(); + document.execCommand("copy"); + textArea.remove(); } \ No newline at end of file diff --git a/.js/github.com.js b/.js/github.com.js index ab5f534..d5ac749 100644 --- a/.js/github.com.js +++ b/.js/github.com.js @@ -185,4 +185,27 @@ let currentUser = $("ul.header-nav.float-right > li:last > a > img").attr("alt") }); }); }); +})(); + +// file copy button +(function() { + if (!$("[data-line-number='1']").length) { + return; + } + + let targetSibling = $("#raw-url") + let fileUrl = targetSibling.attr("href"); + + let copyBtn = $(""); + copyBtn.text("Copy"); + copyBtn.attr("href", fileUrl); + copyBtn.addClass("btn btn-sm copy-btn"); + + copyBtn.click((e) => { + e.preventDefault(); + let fileContents = $(".js-file-line-container").get(0).innerText; + copyToClipBoard(fileContents); + }); + + targetSibling.after(copyBtn); })(); \ No newline at end of file