copylinkaddress.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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. console.log("Cleared linkAddress");
  9. window.getSelection().removeAllRanges();
  10. }
  11. console.log("Current selection: " + window.getSelection().toString());
  12. }
  13. $(function() {
  14. $('a').on({
  15. mouseenter: function() {
  16. if(window.getSelection().toString()) {
  17. console.log("Something is already selected. Copyl backs out.");
  18. } else {
  19. console.log("Nothing is selected. Copyl kicks in.");
  20. linkAddress.val($(this).prop('href'));
  21. linkAddress.select();
  22. console.log("linkAddress: " + linkAddress.val());
  23. }
  24. console.log("Current Selection: " + window.getSelection().toString());
  25. },
  26. mouseleave: function() {
  27. console.log("Leaving link.");
  28. clearLinkAddress();
  29. }
  30. });
  31. window.onbeforeunload = clearLinkAddress();
  32. });