BlueCat Address Manager Copy to Clipboard on Hover

Add a copy to clipboard button to table cell while hovering over it in Table View in BlueCat Address Manager

  1. // ==UserScript==
  2. // @name BlueCat Address Manager Copy to Clipboard on Hover
  3. // @namespace *
  4. // @description Add a copy to clipboard button to table cell while hovering over it in Table View in BlueCat Address Manager
  5. // @include */app*
  6. // @version 4
  7. // @grant none
  8. // @author Marius Galm
  9. // @copyright 2018, Marius Galm
  10. // @license MIT
  11. // @icon https://www.bluecatnetworks.com/wp-content/uploads/2018/03/cropped-bluecat-favicon-32x32.png
  12. // @require http://code.jquery.com/jquery-latest.min.js
  13. // ==/UserScript==
  14.  
  15. //copy function
  16. function copyThis() {
  17. var btn = document.getElementById("copybtn");
  18. if (btn !== undefined) {
  19. var parent = btn.parentNode;
  20. var text = parent.innerText.trim();
  21. if (text !== "") {
  22. copyTextToClipboard(text);
  23. } else {
  24. var span = parent.getElementsByTagName("span");
  25. if (span !== undefined) {
  26. var spantext = span[0].innerText.trim();
  27. copyTextToClipboard(spantext);
  28. }
  29. }
  30. }
  31. };
  32.  
  33. function copyTextToClipboard(text) {
  34. var textArea = document.createElement("textarea");
  35. textArea.value = text;
  36. // Place in top-left corner of screen regardless of scroll position.
  37. textArea.style.position = 'fixed';
  38. textArea.style.top = 0;
  39. textArea.style.left = 0;
  40. // Ensure it has a small width and height. Setting to 1px / 1em
  41. // doesn't work as this gives a negative w/h on some browsers.
  42. textArea.style.width = '2em';
  43. textArea.style.height = '2em';
  44. // We don't need padding, reducing the size if it does flash render.
  45. textArea.style.padding = 0;
  46. // Clean up any borders.
  47. textArea.style.border = 'none';
  48. textArea.style.outline = 'none';
  49. textArea.style.boxShadow = 'none';
  50. // Avoid flash of white box if rendered for any reason.
  51. textArea.style.background = 'transparent';
  52. document.body.appendChild(textArea);
  53. textArea.focus();
  54. textArea.select();
  55. try {
  56. var successful = document.execCommand('copy');
  57. var msg = successful ? 'successful' : 'unsuccessful';
  58. var msgtext = 'Copying text command was ' + msg + '- Copied: "' +text+'"';
  59. //console.log('Copying text command was ' + msg);
  60. tempAlert(msgtext,1000);
  61. } catch (err) {
  62. console.log('Oops, unable to copy');
  63. }
  64. document.body.removeChild(textArea);
  65. }
  66.  
  67. function tempAlert(msg,duration)
  68. {
  69. var el = document.createElement("div");
  70. el.setAttribute("style","position:fixed;top:3%;left:50%;background-color:rgba(0,0,0,0);");
  71. el.innerHTML = msg;
  72. setTimeout(function(){
  73. el.parentNode.removeChild(el);
  74. },duration);
  75. document.body.appendChild(el);
  76. }
  77.  
  78. if (document.readyState === "interactive" ) {
  79.  
  80. //button to add
  81. var img = document.createElement('img');
  82. img.id = "copybtn";
  83. img.src = "/images/icons/small/copy.gif";
  84. img.border="0";
  85. img.style = "display: inline; height: 75%; padding-top: 2px;";
  86. img.alt="Copy to clipboard";
  87. img.title="Copy to clipboard";
  88. img.addEventListener('click', function () {
  89. copyThis();
  90. }, false);
  91. //console.log(img);
  92.  
  93. //jQuery fix - is now broken or not necessary any more
  94. //this.$ = this.jQuery = jQuery.noConflict(true);
  95.  
  96. // object detail fields
  97. $(".detailsPane-fieldValue").hover(
  98. function () {
  99. //console.log($(this));
  100. if ($(this)[0].innerText.trim() !== "") {
  101. var text2 = $(this)[0].innerText;
  102. // if text doesn't contains newline
  103. //console.log(text2);
  104. var match = /\r|\n|^No$|^Yes$/.exec(text2);
  105. if (!match) {
  106. if ($(this)[0].scrollWidth > $(this).innerWidth()) {
  107. //Text has over-flown, add before the text (will collide with sCC though
  108. $(this)[0].insertBefore(img,$(this)[0].firstChild);
  109. } else {
  110. // append
  111. var parHeight = $(this)[0].scrollHeight;
  112. if (parHeight >= 25) {
  113. img.height = parHeight - 14;
  114. } else if (parHeight >= 21) {
  115. img.height = parHeight - 9;
  116. } else {
  117. //img.height = "75%";
  118. }
  119. $(this)[0].appendChild(img);
  120. }
  121. //console.log($(this));
  122. }
  123. }
  124. },
  125. function () {
  126. $("#copybtn").remove();
  127. }
  128. );
  129.  
  130. // table values no header
  131. $("#outerTable tr td").not(':first').hover(
  132. function () {
  133. var list = $(this)[0].classList;
  134. 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")) {
  135. // list will probably grow
  136. //console.log($(this)[0].classList[0]);
  137. } else {
  138. if ($(this)[0].innerText.trim() !== "") {
  139. var text2 = $(this)[0].innerText;
  140. // if text doesn't contains newline
  141. //console.log(text2);
  142. var match = /\r|\n|^No$|^Yes$/.exec(text2);
  143. if (!match) {
  144. if ($(this)[0].scrollWidth > $(this).innerWidth()) {
  145. //Text has over-flown, add before the text (will collide with sCC though
  146. $(this)[0].insertBefore(img,$(this)[0].firstChild);
  147. } else {
  148. // append
  149. var parHeight = $(this)[0].scrollHeight;
  150. if (parHeight >= 25) {
  151. img.height = parHeight - 14;
  152. } else if (parHeight >= 21) {
  153. img.height = parHeight - 9;
  154. } else {
  155. //img.height = "75%";
  156. }
  157. $(this)[0].appendChild(img);
  158. }
  159. //console.log($(this));
  160. }
  161. }
  162. }
  163. },
  164. function () {
  165. $("#copybtn").remove();
  166. }
  167. );
  168. }