Advertisement
Foxscotch

Randomcolour quick copy (2nd update)

Jan 9th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Randomcolour quick copy
  3. // @version      1
  4. // @description  Add a small button to copy the color from randomcolour.com to your clipboard.
  5. // @author       Foxscotch
  6. // @match        http://randomcolour.com/
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. var body = document.body;
  11.  
  12. var btn = document.createElement('button');
  13. btn.textContent = 'Copy hex code: ' + body.bgColor;
  14. body.appendChild(btn);
  15.  
  16. var hiddenText = document.createElement('textarea');
  17. hiddenText.value = body.bgColor;
  18. hiddenText.hidden = true;
  19. // Real quick, let's make sure you won't notice the text box when it's unhidden.
  20. // Just in case it stays longer than it should.
  21. hiddenText.style.background = 'transparent';
  22. hiddenText.style.color = 'transparent';
  23. hiddenText.style.border = 'none';
  24. body.appendChild(hiddenText);
  25.  
  26. body.appendChild(document.createElement('br'));
  27.  
  28. var colorBtn = document.createElement('button');
  29. colorBtn.textContent = 'Get new color';
  30. body.appendChild(colorBtn);
  31.  
  32. body.appendChild(document.createElement('br'));
  33.  
  34. var link = document.createElement('a');
  35. link.href = 'http://colorhexa.com/' + body.bgColor.slice(1);
  36. link.target = '_blank';
  37.  
  38. var linkBtn = document.createElement('button');
  39. linkBtn.textContent = 'Open the ColorHexa page for this color';
  40. link.appendChild(linkBtn);
  41.  
  42. body.appendChild(link);
  43.  
  44. newColor = function () {
  45.     var bg_colour = Math.floor(Math.random() * 16777215).toString(16);
  46.     bg_colour = "#" + ("000000" + bg_colour).slice(-6);
  47.     body.bgColor = bg_colour;
  48. }
  49.  
  50. function updateButton () {
  51.     btn.textContent = 'Copy hex code: ' + body.bgColor;
  52.     hiddenText.value = body.bgColor;
  53.     link.href = 'http://colorhexa.com/' + body.bgColor.slice(1);
  54. }
  55.  
  56. btn.addEventListener('click', function (e) {
  57.     hiddenText.hidden = false;
  58.     hiddenText.select();
  59.    
  60.     var copied = document.execCommand('copy');
  61.     var msg = copied ? 'copied' : 'not copied'
  62.     console.log('Hex code was ' + msg + '.');
  63.    
  64.     hiddenText.hidden = true;
  65. });
  66.  
  67. colorBtn.addEventListener('click', function (e) {
  68.     newColor();
  69.     updateButton();
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement