-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlueCat Address Manager Copy to Clipboard on Hover.user.js
168 lines (160 loc) · 6.44 KB
/
BlueCat Address Manager Copy to Clipboard on Hover.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// ==UserScript==
// @name BlueCat Address Manager Copy to Clipboard on Hover
// @namespace *
// @description Add a copy to clipboard button to table cell while hovering over it in Table View in BlueCat Address Manager
// @include */app*
// @version 4
// @grant none
// @author Marius Galm
// @copyright 2018, Marius Galm
// @license MIT
// @icon https://www.bluecatnetworks.com/wp-content/uploads/2018/03/cropped-bluecat-favicon-32x32.png
// @require http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==
//copy function
function copyThis() {
var btn = document.getElementById("copybtn");
if (btn !== undefined) {
var parent = btn.parentNode;
var text = parent.innerText.trim();
if (text !== "") {
copyTextToClipboard(text);
} else {
var span = parent.getElementsByTagName("span");
if (span !== undefined) {
var spantext = span[0].innerText.trim();
copyTextToClipboard(spantext);
}
}
}
};
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Place in top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;
// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
// Avoid flash of white box if rendered for any reason.
textArea.style.background = 'transparent';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
var msgtext = 'Copying text command was ' + msg + '- Copied: "' +text+'"';
//console.log('Copying text command was ' + msg);
tempAlert(msgtext,1000);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
function tempAlert(msg,duration)
{
var el = document.createElement("div");
el.setAttribute("style","position:fixed;top:3%;left:50%;background-color:rgba(0,0,0,0);");
el.innerHTML = msg;
setTimeout(function(){
el.parentNode.removeChild(el);
},duration);
document.body.appendChild(el);
}
if (document.readyState === "interactive" ) {
//button to add
var img = document.createElement('img');
img.id = "copybtn";
img.src = "/images/icons/small/copy.gif";
img.border="0";
img.style = "display: inline; height: 75%; padding-top: 2px;";
img.alt="Copy to clipboard";
img.title="Copy to clipboard";
img.addEventListener('click', function () {
copyThis();
}, false);
//console.log(img);
//jQuery fix - is now broken or not necessary any more
//this.$ = this.jQuery = jQuery.noConflict(true);
// object detail fields
$(".detailsPane-fieldValue").hover(
function () {
//console.log($(this));
if ($(this)[0].innerText.trim() !== "") {
var text2 = $(this)[0].innerText;
// if text doesn't contains newline
//console.log(text2);
var match = /\r|\n|^No$|^Yes$/.exec(text2);
if (!match) {
if ($(this)[0].scrollWidth > $(this).innerWidth()) {
//Text has over-flown, add before the text (will collide with sCC though
$(this)[0].insertBefore(img,$(this)[0].firstChild);
} else {
// append
var parHeight = $(this)[0].scrollHeight;
if (parHeight >= 25) {
img.height = parHeight - 14;
} else if (parHeight >= 21) {
img.height = parHeight - 9;
} else {
//img.height = "75%";
}
$(this)[0].appendChild(img);
}
//console.log($(this));
}
}
},
function () {
$("#copybtn").remove();
}
);
// table values no header
$("#outerTable tr td").not(':first').hover(
function () {
var list = $(this)[0].classList;
if (list.contains("skinImage")||list.contains("first-selection")||list.contains("percent-bar-used")||list.contains("percent-bar-free")||list.contains("percent-bar")||list.contains("percent-bar-gateway")||list.contains("percent-bar-dhcp-reserved")||list.contains("percent-bar-static")||list.contains("percent-bar-dhcp-allocated")||list.contains("percent-bar-reserved")) {
// list will probably grow
//console.log($(this)[0].classList[0]);
} else {
if ($(this)[0].innerText.trim() !== "") {
var text2 = $(this)[0].innerText;
// if text doesn't contains newline
//console.log(text2);
var match = /\r|\n|^No$|^Yes$/.exec(text2);
if (!match) {
if ($(this)[0].scrollWidth > $(this).innerWidth()) {
//Text has over-flown, add before the text (will collide with sCC though
$(this)[0].insertBefore(img,$(this)[0].firstChild);
} else {
// append
var parHeight = $(this)[0].scrollHeight;
if (parHeight >= 25) {
img.height = parHeight - 14;
} else if (parHeight >= 21) {
img.height = parHeight - 9;
} else {
//img.height = "75%";
}
$(this)[0].appendChild(img);
}
//console.log($(this));
}
}
}
},
function () {
$("#copybtn").remove();
}
);
}