Last active
July 14, 2020 02:34
-
-
Save AnastasiaDunbar/51dcdedfbf4936b4dd879aa3058cb3a4 to your computer and use it in GitHub Desktop.
A bookmarklet to save current page's code/form, like a textarea cache (except it doesn't save automatically).
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
javascript:(()=>{ | |
let magic="store(code);",hn=location.hostname,d=document,qS=(s,o=document)=>o.querySelector(s), | |
element,value,description=[new Date().getTime()]; | |
if(/openprocessing\.org/.test(hn)){ | |
element=codeMirror; | |
value=element.getValue(); | |
}else if(/(wavepot|tinyrave)\.com/.test(hn)){ | |
element=ace.edit("editor").getSession(); | |
value=element.getValue(); | |
}else if(/mytextarea\.com/.test(hn)){ | |
element=d.getElementById("textarea"); | |
value=element.value; | |
}else if(/editpad\.org/.test(hn)){ | |
/*This site isn't made to preserve your text.*/ | |
element=d.textform.text; | |
value=element.value; | |
}else if(/glslsandbox\.com/.test(hn)){ | |
/*TODO: Fix this.*/ | |
element=d.getElementsByClassName("CodeMirror-lines")[0]; | |
value=element.innerText; | |
}else if(/shadertoy\.com/.test(hn)){ | |
/*TODO: Fix this.*/ | |
element=d.getElementsByClassName("CodeMirror-code")[0]; | |
value=Array.from(element.getElementsByTagName("pre")).map(x=>x.innerText).join(""); | |
}else if(/pastebin\.com/.test(hn)){ | |
element=d.getElementById("paste_code"); | |
value=element.value; | |
}else if(/codeshare\.io|(gist\.github|jshint)\.com/.test(hn)){ | |
element=qS(".CodeMirror").CodeMirror; | |
value=element.getValue(); | |
}else if(/collabedit\.com/.test(hn)){ | |
element=qS("textarea"); | |
value=element.value; | |
}else if(/wik(tionary|ipedia).org/.test(hn)){ | |
let p=new URL(location).searchParams; | |
element=qS("div[class='wikiEditor-ui-text']").firstChild; | |
value=element.value; | |
description.push(p.get("title")); | |
}else if(/4chan(nel)?.org/.test(hn)){ | |
/*TODO: 8ch.net (dead), lainchan.org, thread ID.*/ | |
let header=qS("tr[data-type='Comment'] textarea"), | |
floating=qS("div[id='qrForm'] textarea[name^='com']"); | |
if(header!==null){element=header;value=element.value;description.push("header comment");} | |
if(floating!==null){element=floating;value=element.value;description.push("floating comment");} | |
}else{ | |
console.error("Unknown domain name."); | |
} | |
let stored=Object.keys(localStorage).filter(x=>x.startsWith(magic)), | |
save=()=>{ | |
if(value.length){ | |
console.log(value); | |
}else if(confirm("The string is empty. Would you like to cancel?")){ | |
return; | |
} | |
let i=stored.findIndex(x=>value===localStorage.getItem(x)); | |
if(i>=0){ | |
alert(`An identical already exists “${stored[i]}”.`); | |
}else{ | |
let k=`${magic} (${description.join(", ")})`; | |
localStorage.setItem(k,value); | |
alert(`Saved as “${k}”.`); | |
} | |
}; | |
if(stored.length&&confirm("Read older storage?\n(Otherwise save.)")){ | |
if(value!==undefined&&confirm("Save current one beforehand?")){ | |
save(); | |
} | |
if(confirm(`Go through?\n${stored.length}: ${stored.join(", ")}`)){ | |
for(let i=0;i<stored.length;i++){ | |
if(confirm(`Load this?\n${stored[i]}\n${localStorage.getItem(stored[i])}`)){ | |
element.value=localStorage.getItem(stored[i]); /*TODO*/ | |
break; | |
} | |
} | |
} | |
}else if(value!==undefined){ | |
save(); | |
}else{ | |
alert("There's nothing."); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
div.codeContainer
there'sdiv#codeTabs
abovediv[class^="CodeMirror"]
, andli.selected
within theul
element is the current tab.