Advertisement
Foxscotch

Randomcolor quick copy

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