Skip to content

Instantly share code, notes, and snippets.

@CEBracco
Created April 2, 2020 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CEBracco/5fe83c882bda523e2193d2fbb5e5cb41 to your computer and use it in GitHub Desktop.
Save CEBracco/5fe83c882bda523e2193d2fbb5e5cb41 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name fastclipboard
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var cntrlIsPressed = false;
$(document).keydown(function(event){
if(event.which=="17") {
cntrlIsPressed = true;
}
});
$(document).keyup(function(){
cntrlIsPressed = false;
});
$(document).click(function(e){
selectMe(e.target,1)
});
$('body').append("<style>.elem-to-copy{ border: 2px solid blue; cursor: default; }</style>")
$('*').hover(function(e){
if(cntrlIsPressed) {
$(e.target).addClass('elem-to-copy');
}
}, function(e) {
$(e.target).removeClass('elem-to-copy');
});
function selectMe(elem, mouseButton)
{
if(cntrlIsPressed)
{
switch(mouseButton)
{
case 1:
console.log("Cntrl + left click");
copyToClipboard($(elem).text().trim())
break;
case 2:
console.log("Cntrl + right click");
break;
default:
break;
}
}
}
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment