copylinkaddress.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. var linkAddress = $('<span id="copylAddress" style="display: inline-block;" />');
  15. $('body').append(linkAddress);
  16. // This is a DOM element that has to be selectable but not visible to anybody
  17. linkAddress.css({position: 'absolute', left:'-9999em'});
  18. var previousCaretPosition = -1;
  19. COPYL_DEBUG = true;
  20. function write_to_console(text) {
  21. if (COPYL_DEBUG)
  22. console.log(text);
  23. }
  24. function selectElement(el) {
  25. // Check if anything is currently selected, if so backup
  26. if (window.getSelection().rangeCount > 0) {
  27. previousCaretPosition = document.activeElement.selectionStart;
  28. }
  29. write_to_console("Previous cursor position: " + previousCaretPosition);
  30. var range = document.createRange();
  31. write_to_console("Selecting element " + el);
  32. write_to_console("el[0] is " + el[0]);
  33. range.selectNodeContents(el[0]);
  34. write_to_console("Range: " + range);
  35. write_to_console("Window selection range count before: " + window.getSelection().rangeCount);
  36. if (window.getSelection().rangeCount > 0) {
  37. window.getSelection().removeAllRanges();
  38. }
  39. write_to_console("Window selection range count now: " + window.getSelection().rangeCount);
  40. write_to_console("Active Element: " + document.activeElement);
  41. window.getSelection().addRange(range);
  42. write_to_console("Window selection range count after: " + window.getSelection().rangeCount);
  43. }
  44. function clearLinkAddress() {
  45. if (linkAddress.text()) {
  46. linkAddress.text("");
  47. linkAddress.blur();
  48. write_to_console("Cleared linkAddress");
  49. write_to_console("window.getSelection: " + window.getSelection());
  50. window.getSelection().removeAllRanges();
  51. }
  52. if (previousCaretPosition != -1)
  53. {
  54. document.activeElement.selectionStart = previousCaretPosition;
  55. write_to_console("Previous cursor position set: " + document.activeElement.selectionStart);
  56. }
  57. write_to_console("Current selection: " + window.getSelection().toString());
  58. }
  59. $(function() {
  60. // The code attaches itself to all anchor elements
  61. $("html").on("mouseenter", "a", function(){
  62. // Everytime the user hovers (enters) a link
  63. if(window.getSelection().toString()) {
  64. write_to_console("Something is already selected. Don't do anything.");
  65. } else {
  66. write_to_console("Nothing is selected.");
  67. linkAddress.text($(this).prop('href'));
  68. write_to_console("linkAddress: " + linkAddress.text());
  69. selectElement(linkAddress);
  70. }
  71. write_to_console("Current Selection: " + window.getSelection().toString());
  72. }).on("mouseleave", "a", function(){
  73. // Every time the user leaves a link
  74. write_to_console("Leaving link.");
  75. clearLinkAddress();
  76. });
  77. $(window).on("beforeunload", function() {
  78. clearLinkAddress();
  79. });
  80. });