CopyFixerfix

Copy Title and URL by Ctrl-C

  1. // ==UserScript==
  2. // @name CopyFixerfix
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Copy Title and URL by Ctrl-C
  6. // @author MSG
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. window.addEventListener('keydown', copyfixer, true);
  13.  
  14. function copyfixer(event) {
  15. if (event.keyCode != 67) return;
  16. var isWin = (navigator.platform.indexOf("Win") != -1);
  17. var isMac = (navigator.platform.indexOf("Mac") != -1);
  18. if ((! isMac && ! event.ctrlKey) || (isMac && ! event.metaKey)) return;
  19. if (isSelected()) return;
  20. var crlf = isWin ? "\r\n" : "\n";
  21. var txt = document.title + crlf + document.location.href + crlf + crlf;
  22. sendMessage(txt);
  23. }
  24.  
  25. function isSelected() {
  26. var sel = window.getSelection();
  27. if (sel.rangeCount <= 0) return false;
  28. if (sel.rangeCount > 1) return true;
  29.  
  30. var range = sel.getRangeAt(0);
  31. if (! range.collapsed) return true;
  32. if (range.startContainer != range.endContainer) return true;
  33. if (range.startOffset != range.endOffset) return true;
  34. if (document.activeElement.tagName.toLowerCase() != "body") return true;
  35.  
  36. return false;
  37. }
  38.  
  39. function sendMessage(data) {
  40. //if ( request.command != "copyfixerCopy" ) return;
  41. const textarea = document.createElement('textarea');
  42. textarea.style.position = 'fixed';
  43. textarea.style.opacity = 0;
  44. textarea.value = data;
  45.  
  46. document.body.appendChild(textarea);
  47.  
  48. textarea.select();
  49. document.execCommand('Copy');
  50. }
  51.  
  52. })();