Skip to content

Instantly share code, notes, and snippets.

@nikhilkumarsingh
Created March 15, 2020 17:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nikhilkumarsingh/11de9d7cb1221545a61c9a0294e7c7b7 to your computer and use it in GitHub Desktop.
Save nikhilkumarsingh/11de9d7cb1221545a61c9a0294e7c7b7 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Codechef IO Copy
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author nikhilksingh97
// @match https://www.codechef.com/problems/*
// @grant none
// ==/UserScript==
let styleSheet = `
.copyBtn {
background-color: green;
padding: 5px;
font-size: 12px;
}
`;
let s = document.createElement('style');
s.type = "text/css";
s.innerHTML = styleSheet;
(document.head || document.documentElement).appendChild(s);
window.addEventListener('load', function() {
'use strict';
function copy(ele) {
let temp = document.createElement('textarea');
document.body.appendChild(temp);
temp.value = ele.textContent;
temp.select();
document.execCommand('copy');
temp.remove();
}
function addCopyBtn(ele) {
let btn = document.createElement("button");
btn.innerHTML = "Copy";
btn.className = "copyBtn";
btn.onclick = () => {
copy(ele.lastChild);
}
ele.insertBefore(document.createElement('br'), ele.childNodes[0]);
ele.insertBefore(btn, ele.childNodes[0]);
}
let preTags = document.getElementsByTagName("pre");
console.log(preTags);
for (let preTag of preTags) {
addCopyBtn(preTag);
}
});
@robosidd
Copy link

robosidd commented Aug 6, 2021

Thanks

Copy link

ghost commented Sep 29, 2021

is it work for make spam?

@s703851
Copy link

s703851 commented Feb 1, 2022

lol

@HavkoSVK
Copy link

HavkoSVK commented Apr 5, 2022

Wheare i can do userscrip?

@real-jiakai
Copy link

Thank you. My great teacher.❤️❤️❤️❤️❤️ In my case, it doesn't work in Chrome browser. But it works in Edge browser. Maybe That my Chrome browser has installed too many plugins leads to this failure.

@real-jiakai
Copy link

real-jiakai commented Aug 18, 2022

document.execCommand() method has been discarded. Maybe you should change the 33th line code to the code below

navigator.clipboard.writeText(temp.value); 

you can find solutions in this discussionmdn document about clipboard and mdn document about execCommand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment