copylinkaddress.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. The way the extension works is, once you hover a link:
  3. If something is already selected in the page, it does nothing. Else, it
  4. takes the URL of the link you are hovering, copies it to an invisible span,
  5. programmatically selects the span.
  6. Now when you hits Cmd-C (Ctrl-C), the hidden selection is copied to clipboard.
  7. When you move pointer away from the link, it
  8. clears the hidden selection, clears the invisible span.
  9. If, at the time of hover, the cursor was in a textbox (without anything selected),
  10. it is technically a zero-length selection in Chrome. So, the extension goes ahead and clears that selection
  11. (thereby taking the cursor away from the textbox), saving the caret position.
  12. When you move away from the link, the caret position is restored.
  13. */
  14. COPYL_DEBUG = false;
  15. let url = chrome.runtime.getURL('sounds/pop.mp3');
  16. let a = new Audio(url);
  17. var previousCaretPosition = -1;
  18. var linkAddress = $('<span id="copylAddress" style="display: inline-block;" />');
  19. $('body').append(linkAddress);
  20. /* This is a DOM element that has to be selectable but not visible to anybody */
  21. linkAddress.css({position: 'absolute', left:'-9999em'});
  22. /* when the user presses y (=yank), copy URL to clipboard when hovering */
  23. window.onkeydown = function(event) {
  24. if (event.key == "y") {
  25. if (linkAddress.text().length != 0) {
  26. document.execCommand('copy');
  27. a.play();
  28. }
  29. }
  30. }
  31. function write_to_console(text) {
  32. if (COPYL_DEBUG)
  33. console.log(text);
  34. }
  35. function selectElement(el) {
  36. /* Check if anything is currently selected, if so backup */
  37. if (window.getSelection().rangeCount > 0) {
  38. previousCaretPosition = document.activeElement.selectionStart;
  39. }
  40. write_to_console("Previous cursor position: " + previousCaretPosition);
  41. var range = document.createRange();
  42. write_to_console("Selecting element " + el);
  43. write_to_console("el[0] is " + el[0]);
  44. range.selectNodeContents(el[0]);
  45. write_to_console("Range: " + range);
  46. write_to_console("Window selection range count before: " + window.getSelection().rangeCount);
  47. if (window.getSelection().rangeCount > 0) {
  48. window.getSelection().removeAllRanges();
  49. }
  50. write_to_console("Window selection range count now: " + window.getSelection().rangeCount);
  51. write_to_console("Active Element: " + document.activeElement);
  52. window.getSelection().addRange(range);
  53. write_to_console("Window selection range count after: " + window.getSelection().rangeCount);
  54. }
  55. function clearLinkAddress() {
  56. if (linkAddress.text()) {
  57. linkAddress.text("");
  58. linkAddress.blur();
  59. write_to_console("Cleared linkAddress");
  60. write_to_console("window.getSelection: " + window.getSelection());
  61. window.getSelection().removeAllRanges();
  62. }
  63. if (previousCaretPosition != -1)
  64. {
  65. document.activeElement.selectionStart = previousCaretPosition;
  66. write_to_console("Previous cursor position set: " + document.activeElement.selectionStart);
  67. }
  68. write_to_console("Current selection: " + window.getSelection().toString());
  69. }
  70. $(function() {
  71. /* The code attaches itself to all anchor elements */
  72. $("html").on("mouseenter", "a", function(){
  73. /* Everytime the user hovers (enters) a link */
  74. if(window.getSelection().toString()) {
  75. write_to_console("Something is already selected. Don't do anything.");
  76. } else {
  77. write_to_console("Nothing is selected.");
  78. linkAddress.text($(this).prop('href'));
  79. write_to_console("linkAddress: " + linkAddress.text());
  80. selectElement(linkAddress);
  81. }
  82. write_to_console("Current Selection: " + window.getSelection().toString());
  83. }).on("mouseleave", "a", function(){
  84. /* Every time the user leaves a link */
  85. write_to_console("Leaving link.");
  86. clearLinkAddress();
  87. });
  88. $(window).on("beforeunload", function() {
  89. clearLinkAddress();
  90. });
  91. });