Guest User

Untitled

a guest
Dec 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. {
  2. let t=document.title;
  3. let l=location.href;
  4.  
  5. const dialog = document.createElement('div');
  6. dialog.style.position = 'fixed';
  7. dialog.style.top = '0';
  8. dialog.style.padding = '20px';
  9. dialog.style.backgroundColor = '#fff';
  10. dialog.style.boxShadow = '0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23)';
  11. dialog.style.borderRadius = '0 0 4px 0';
  12. dialog.style.zIndex = '2147483647';
  13. dialog.style.fontFamily = '-apple-system, BlinkMacSystemFont, "Helvetica Neue", "Yu Gothic", YuGothic, Verdana, Meiryo, "M+ 1p", sans-serif';
  14.  
  15. const textArea = document.createElement('textarea');
  16. textArea.setAttribute('readonly', '');
  17. const markdown = document.createElement('span');
  18. markdown.innerText = 'Markdown';
  19. markdown.style.padding = '8px';
  20. markdown.style.fontSize = '16px';
  21. markdown.style.marginRight = '8px';
  22. markdown.style.border = '1px solid #ddd';
  23. markdown.style.borderRadius = '4px';
  24. const text = document.createElement('span');
  25. text.innerText = 'Text';
  26. text.style.padding = '8px';
  27. text.style.fontSize = '16px';
  28. text.style.marginRight = '8px';
  29. text.style.border = '1px solid #ddd';
  30. text.style.borderRadius = '4px';
  31. const x = document.createElement('span');
  32. x.innerText = 'x';
  33. x.style.padding = '8px';
  34. x.style.fontSize = '16px';
  35. x.style.border = '1px solid #ddd';
  36. x.style.borderRadius = '4px';
  37.  
  38. dialog.appendChild(markdown);
  39. dialog.appendChild(text);
  40. dialog.appendChild(x);
  41.  
  42. document.body.appendChild(dialog);
  43.  
  44. const close = (copied) => {
  45. let timeout;
  46. if (copied) {
  47. dialog.innerHTML = 'Copied!'
  48. dialog.style.transition = 'opacity .4s .4s';
  49. timeout = 800;
  50. } else {
  51. dialog.style.transition = 'opacity .4s';
  52. timeout = 400;
  53. }
  54. dialog.style.opacity = '0';
  55.  
  56. if (selected) {
  57. document.getSelection().removeAllRanges();
  58. document.getSelection().addRange(selected);
  59. }
  60.  
  61. setTimeout(() => {
  62. document.body.removeChild(dialog)
  63. document.body.removeEventListener('click', close);
  64. }, timeout);
  65. };
  66.  
  67. const click = (title, url, type) => {
  68. switch(type) {
  69. case 'markdown': textArea.value = `[${title}](${url})`; break;
  70. case 'text': textArea.value = `${title}\n${url}`; break;
  71. }
  72. dialog.appendChild(textArea);
  73.  
  74. textArea.select();
  75. document.execCommand('copy');
  76.  
  77. close(true);
  78. };
  79.  
  80. markdown.addEventListener('click', () => click(t, l, 'markdown'));
  81. text.addEventListener('click', () => click(t, l, 'text'));
  82. x.addEventListener('click', () => close(false));
  83. document.body.addEventListener('click', () => close(false));
  84.  
  85. const selected = document.getSelection().rangeCount > 0
  86. ? document.getSelection().getRangeAt(0)
  87. : false;
  88. }
Add Comment
Please, Sign In to add comment