Last active
October 19, 2020 03:04
-
-
Save jizusun/84495bd63e2ed4a33823a9cfc728a810 to your computer and use it in GitHub Desktop.
Copy LeetCode Solution to clipboard
This file contains hidden or 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 Copy LeetCode Solution to clipboard | |
// @namespace https://gist.github.com/jizusun/84495bd63e2ed4a33823a9cfc728a810 | |
// @version 0.3 | |
// @description Better format to paste into Notion | |
// @author You | |
// @match https://leetcode.com/problems/*/solution/ | |
// @grant GM_setClipboard | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function copyDivToClipboard(str) { | |
// GM_setClipboard(result, 'html'); | |
function listener(e) { | |
e.clipboardData.setData("text/html", str); | |
e.clipboardData.setData("text/plain", str); | |
e.preventDefault(); | |
} | |
document.addEventListener("copy", listener); | |
document.execCommand("copy"); | |
document.removeEventListener("copy", listener); | |
} | |
const processKatex = (elem) => { | |
elem.querySelectorAll('.katex').forEach( i => { | |
const val = i.querySelector('annotation').innerHTML.trim() | |
i.innerHTML= '$$' + val + '$'; | |
}) | |
} | |
const processImage = (elem) => { | |
elem.querySelectorAll('img').forEach(i => { | |
const src = i.src | |
i.setAttribute('src', src) | |
}) | |
} | |
const processIframe = (elem) => { | |
elem.querySelectorAll('iframe').forEach( i=> { | |
const url = i.src | |
const parent = i.parentNode | |
var a = document.createElement('a'); | |
var linkText = document.createTextNode(url); | |
a.appendChild(linkText); | |
a.title = url; | |
a.href = url; | |
parent.insertBefore(a, i) | |
}) | |
} | |
const removeH2 = (elem) => { | |
const h2 = elem.querySelector('h2') | |
const hr = elem.querySelector('hr') | |
h2 && elem.removeChild(h2) | |
hr && elem.removeChild(hr) | |
return h2 | |
} | |
const copySolution = (solutionElem) => { | |
const clonedElem = solutionElem.cloneNode(true) | |
removeH2(clonedElem) | |
processKatex(clonedElem) | |
processIframe(clonedElem) | |
processImage(clonedElem) | |
copyDivToClipboard(clonedElem.innerHTML) | |
console.log('Solution Copied'); | |
} | |
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
const observer = new MutationObserver(function(mutations, observer) { | |
const contentElem = document.querySelector("#solution *[class^='content__']"); | |
if (contentElem) { | |
const solutionElem = contentElem.querySelector(':first-child') | |
const btn = document.createElement("BUTTON"); | |
btn.innerHTML = "Copy solution to clipboard"; | |
btn.addEventListener('click', e => copySolution(solutionElem)) | |
contentElem.insertBefore(btn, solutionElem) | |
observer.disconnect(); | |
}; | |
}); | |
observer.observe(document, { | |
subtree: true, | |
attributes: true | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment