Browse Source

Add `Copy` button to gists (#338)

master
Akshay Kadam 8 years ago
committed by Sindre Sorhus
parent
commit
06bbc495ae
  1. 5
      extension/content.css
  2. 6
      extension/content.js
  3. 26
      extension/copy-gist.js
  4. 1
      extension/manifest.json

5
extension/content.css

@ -532,3 +532,8 @@ td.blob-code.blob-code-deletion:before {
.diffbar > .diffstat {
float: right;
}
/* copy button for gists margin */
.copy-btn {
margin: 0 10px 0 0;
}

6
extension/content.js

@ -1,4 +1,4 @@
/* globals gitHubInjection, pageDetect, icons, diffFileHeader, addReactionParticipants, addFileCopyButton, enableCopyOnY, addBlameParentLinks, showRealNames, markUnread */
/* globals gitHubInjection, pageDetect, icons, diffFileHeader, addReactionParticipants, addFileCopyButton, addGistCopyButton, enableCopyOnY, addBlameParentLinks, showRealNames, markUnread */
'use strict';
const {ownerName, repoName} = pageDetect.getOwnerAndRepo();
@ -320,7 +320,9 @@ $(document).on('click', event => {
document.addEventListener('DOMContentLoaded', () => {
const username = getUsername();
if (!pageDetect.isGist()) {
if (pageDetect.isGist()) {
addGistCopyButton();
} else {
addTrendingMenuItem();
}

26
extension/copy-gist.js

@ -0,0 +1,26 @@
/* globals utils */
'use strict';
window.addGistCopyButton = () => {
// Button already added (partial page nav), or non-text file
if ($('.copy-btn').length > 0) {
return;
}
const $gistsSibling = $('.file-actions > .btn.btn-sm');
for (let i = 0; i < $gistsSibling.length; i++) {
const commonContainer = $('.file')[i];
const isSourceCodeClass = commonContainer.children[1].classList.contains('blob-wrapper');
const gistUri = $gistsSibling[i].href;
if (isSourceCodeClass) {
$(`<a href="${gistUri}" class="btn btn-sm copy-btn">Copy</a>`).insertBefore($gistsSibling[i]);
}
}
$(document).on('click', '.copy-btn', e => {
e.preventDefault();
const fileContents = $(`#${e.currentTarget.offsetParent.id}`).find('.js-file-line-container')[0].innerText;
utils.copyToClipboard(fileContents);
});
};

1
extension/manifest.json

@ -33,6 +33,7 @@
"diffheader.js",
"reactions-avatars.js",
"copy-file.js",
"copy-gist.js",
"copy-on-y.js",
"show-names.js",
"content.js",

Loading…
Cancel
Save