-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcopy2clipboard.user.js
125 lines (111 loc) · 4.28 KB
/
copy2clipboard.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ==UserScript==
// @name copy2clipboard
// @namespace https://github.com/Neulana/copy2clipboard
// @version 1.0
// @description a tampermonkey to copy code from stackoverflow.com etc.
// @author pinple; https://github.com/pinple
// @include https://stackoverflow.com/*
// @include https://*.zhihu.com/*
// @include https://www.jianshu.com/*
// @include https://dev.to/*
// @include *.github.io/*
// @grant none
// @license Apache-2.0
// ==/UserScript==
// To remove IDE warnings
var $ = window.jQuery;
(() => {
"use strict";
function selectElementText(el) {
var range = document.createRange();
range.selectNodeContents(el);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
function getSelectedText() {
var t = '';
if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
}
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
return window.clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
$("pre").each(function () {
var pre = this;
$(pre).wrapAll('<div style= "position: relative;"></div>');
var $copyCodeButton = $("<button class='copy-code-button'>Copy</button>");
$copyCodeButton.css({
"position": "absolute",
"top": "1px",
"right": "1px",
"padding": "3px",
"display": "none",
"background-color": "white",
"color": "#313E4E",
"border-radius": "5px",
"-moz-border-radius": "5px",
"-webkit-border-radius": "5px",
"border": "2px solid #CCCCCC"
});
setTimeout(function () {
if ($codeContainer.length == 0) {
$(pre).contents().filter(function () {
return this.className !== "copy-code-button";
}).wrapAll('<code style= "overflow-x: auto; padding: 0px;"></code>');
$codeContainer = $copyCodeButton.siblings("code").get(0);
} else {
$codeContainer = $codeContainer.get(0);
}
}, 0);
$copyCodeButton.click(function () {
selectElementText($codeContainer);
var selectedText = getSelectedText();
var buttonNewText = "";
if (copyToClipboard(selectedText) == true) {
buttonNewText = "Copied";
selectElementText($codeContainer);
} else {
buttonNewText = "Unable to copy";
selectElementText($codeContainer);
}
$(this).text(buttonNewText);
var that = this;
setTimeout(function () {
$(that).text("Copy");
var selection = window.getSelection(); // clear text range
selection.removeAllRanges();
}, 400);
});
$(this).append($copyCodeButton);
var $codeContainer = $copyCodeButton.siblings("code");
$("pre").hover(function () {
$(this).children(".copy-code-button").css("display", "block");
}, function () {
$(this).children(".copy-code-button").css("display", "none");
});
});
})();