Advertisement
Foxscotch

Randomcolor quick copy (updated)

Jan 9th, 2016
128
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. body.appendChild(document.createElement('br'));
  32.  
  33. var link = document.createElement('a');
  34. link.href = 'http://colorhexa.com/' + body.bgColor.slice(1);
  35. link.target = '_blank';
  36.  
  37. var linkBtn = document.createElement('button');
  38. linkBtn.innerText = 'Open the ColorHexa page for this color';
  39. link.appendChild(linkBtn);
  40.  
  41. body.appendChild(link);
  42.  
  43. newColor = function () {
  44.     var bg_colour = Math.floor(Math.random() * 16777215).toString(16);
  45.     bg_colour = "#" + ("000000" + bg_colour).slice(-6);
  46.     body.bgColor = bg_colour;
  47. }
  48.  
  49. function updateButton () {
  50.     btn.innerText = 'Copy hex code: ' + body.bgColor;
  51.     hiddenText.value = body.bgColor;
  52.     link.href = 'http://colorhexa.com/' + body.bgColor.slice(1);
  53. }
  54.  
  55. btn.addEventListener('click', function (e) {
  56.     hiddenText.hidden = false;
  57.     hiddenText.select();
  58.    
  59.     var copied = document.execCommand('copy');
  60.     var msg = copied ? 'copied' : 'not copied'
  61.     console.log('Hex code was ' + msg + '.');
  62.    
  63.     hiddenText.hidden = true;
  64. });
  65.  
  66. colorBtn.addEventListener('click', function (e) {
  67.     newColor();
  68.     updateButton();
  69. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement