Last active
August 18, 2022 07:24
-
-
Save real-jiakai/d932718f9407cacbeadafc69dbe18df5 to your computer and use it in GitHub Desktop.
tampermonkey scripts learning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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