copylinkaddress.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var linkAddress = $('<input id="copylAddress" type="text" />');
  2. $('body').append(linkAddress);
  3. // 'Hidden' yet selectable
  4. linkAddress.css({position: 'absolute', left:'-9999em'});
  5. function clearLinkAddress() {
  6. if (linkAddress.val()) {
  7. linkAddress.val(null);
  8. // linkAddress.blur();
  9. console.log("Cleared linkAddress");
  10. window.getSelection().removeAllRanges();
  11. }
  12. console.log("Current selection: " + window.getSelection().toString());
  13. }
  14. $(function() {
  15. $('a').on({
  16. mouseenter: function() {
  17. if(window.getSelection().toString()) {
  18. console.log("Something is already selected. Copyl backs out.");
  19. } else {
  20. console.log("Nothing is selected. Copyl kicks in.");
  21. linkAddress.val($(this).prop('href'));
  22. linkAddress.select();
  23. linkAddress.blur();
  24. console.log("linkAddress: " + linkAddress.val());
  25. }
  26. console.log("Current Selection: " + window.getSelection().toString());
  27. },
  28. mouseleave: function() {
  29. console.log("Leaving link.");
  30. clearLinkAddress();
  31. }
  32. });
  33. // Clear linkAddress when user closes or moves away from the page
  34. window.onbeforeunload = function() {
  35. console.log("Leaving page.");
  36. clearLinkAddress();
  37. }
  38. });