Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Randomcolor quick copy
- // @version 1
- // @description Add a small button to copy the color from randomcolor.com to your clipboard.
- // @author Foxscotch
- // @match http://randomcolour.com/
- // ==/UserScript==
- var body = document.body;
- var btn = document.createElement('button');
- btn.innerText = 'Copy hex code: ' + body.bgColor;
- body.appendChild(btn);
- var hiddenText = document.createElement('textarea');
- hiddenText.value = body.bgColor;
- hiddenText.hidden = true;
- // Real quick, let's make sure you won't notice the text box when it's unhidden.
- // Just in case it stays longer than it should.
- hiddenText.style.background = 'transparent';
- hiddenText.style.color = 'transparent';
- hiddenText.style.border = 'none';
- body.appendChild(hiddenText);
- body.appendChild(document.createElement('br'));
- var colorBtn = document.createElement('button');
- colorBtn.innerText = 'Get new color';
- body.appendChild(colorBtn);
- newColor = function () {
- var bg_colour = Math.floor(Math.random() * 16777215).toString(16);
- bg_colour = "#" + ("000000" + bg_colour).slice(-6);
- body.bgColor = bg_colour;
- }
- function updateButton () {
- btn.innerText = 'Copy hex code: ' + body.bgColor;
- hiddenText.value = body.bgColor;
- }
- btn.addEventListener('click', function (e) {
- hiddenText.hidden = false;
- hiddenText.select();
- var copied = document.execCommand('copy');
- var msg = copied ? 'copied' : 'not copied'
- console.log('Hex code was ' + msg + '.');
- hiddenText.hidden = true;
- });
- colorBtn.addEventListener('click', function (e) {
- newColor();
- updateButton();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement