Skip to content

Instantly share code, notes, and snippets.

@real-jiakai
Last active August 18, 2022 07:24
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 real-jiakai/d932718f9407cacbeadafc69dbe18df5 to your computer and use it in GitHub Desktop.
Save real-jiakai/d932718f9407cacbeadafc69dbe18df5 to your computer and use it in GitHub Desktop.
tampermonkey scripts learning
// ==UserScript==
// @name Codechef IO Copy
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author jiakai
// @match https://www.codechef.com/problems/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment