-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodingame-output-copy.user.js
97 lines (77 loc) · 2.93 KB
/
codingame-output-copy.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
// ==UserScript==
// @name Codingame output copy
// @namespace https://namal.ovh
// @version 0.4
// @description Allows you to copy the output of a test case in codingame
// @author Namal
// @match https://www.codingame.com/*
// @grant none
// ==/UserScript==
'use strict';
$(function(){
//source: http://stackoverflow.com/a/22581382/5795409
function copyToClipboard(elem) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
//var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.text().replace(/^\s*[\r\n]/gm, "");
// select the content
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}
// clear temporary content
target.textContent = "";
return succeed;
}
$('<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">').appendTo('head');
$('<style>#namalCopy:before{ content: "\\f0c5"; height: 12px; width: 12px; color: white; font-family: FontAwesome;}</style>').appendTo('head');
var inIde = false;
var intervalID = null;
function intervalIde(){
if(intervalID!=null)
clearInterval(intervalID);
intervalID = setInterval(ideCode,inIde ? 10000 : 2000);
}
function setupCopy(){
$($('.menu-entries.ps-container')[0]).append('<div class="menu-entry ng-scope copy"><a class="menu-entry-inner" id="namalCopy"><span class="entry-label">Copy</span></a></div>');
$("#namalCopy").click(function(){
copyToClipboard($(".cg-ide-console-frame-container"));
});
inIde=true;
intervalIde();
}
var ideCode = function(){
if(!inIde && $(".ide").length>0)
setupCopy()
else if(inIde){
if($(".ide").length==0){
inIde=false;
intervalIde();
}else if($("#namalCopy").length==0)
setupCopy();
}
};
intervalIde();
});