Skip to content

Commit 31a063d

Browse files
authored
Add an option to 'copy to clipboard' (#37)
1 parent ec14f1f commit 31a063d

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ <h2>Instantly convert <a href="http://curl.haxx.se/">curl</a> commands to <a hre
4343

4444
<main>
4545
<textarea id="input" rows="4" placeholder="Paste curl here"></textarea>
46+
<div class="copy-div">
47+
<a href="javascript:" type="button" onclick="copyCode()">Copy to clipboard</a>
48+
</div>
4649
<div id="output"></div>
4750
</main>
4851

resources/css/common.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ footer {
9494
margin: 50px auto;
9595
text-align: center;
9696
}
97+
98+
.copy-div {
99+
text-align: right;
100+
}
101+
102+
.copy-div > a {
103+
text-decoration: none;
104+
}

resources/js/common.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ function initAnalytics()
1010
ga('send', 'pageview');
1111
}
1212

13+
function copyCode() {
14+
selectCode("output");
15+
document.execCommand("copy");
16+
}
17+
18+
// select the entire text present in the "output"
19+
// div across all the spans
20+
// ref: https://stackoverflow.com/questions/1173194/select-all-div-text-with-single-mouse-click
21+
function selectCode(containerid) {
22+
if (document.selection) { // IE
23+
var range = document.body.createTextRange();
24+
range.moveToElementText(document.getElementById(containerid));
25+
range.select();
26+
} else if (window.getSelection) {
27+
var range = document.createRange();
28+
range.selectNode(document.getElementById(containerid));
29+
window.getSelection().removeAllRanges();
30+
window.getSelection().addRange(range);
31+
}
32+
}
33+
1334
$(function()
1435
{
1536
var emptyOutputMsg = "Go code will appear here";

0 commit comments

Comments
 (0)