Skip to content

Instantly share code, notes, and snippets.

@AnastasiaDunbar
Last active July 14, 2020 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnastasiaDunbar/51dcdedfbf4936b4dd879aa3058cb3a4 to your computer and use it in GitHub Desktop.
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).
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.");
}
})();
@AnastasiaDunbar
Copy link
Author

AnastasiaDunbar commented Sep 13, 2019

To read (on mobile):

javascript:(()=>{
	let k=Object.keys(localStorage).filter(x=>x.startsWith("store(code);"));
	if(confirm(k.length+": "+k.join(","))){
		k.forEach(x=>prompt(x,localStorage.getItem(x)));
	}
})();

To remove:

javascript:(()=>{
	let k=Object.keys(localStorage).filter(x=>x.startsWith("store(code);"));
	if(confirm(k.length+" to remove: "+k.join(","))){
		k.forEach(x=>{
			if(confirm("Remove?\n"+localStorage.getItem(x))){
				localStorage.removeItem(x);
			}
		});
	}
})();

@AnastasiaDunbar
Copy link
Author

AnastasiaDunbar commented Sep 15, 2019

  • I should add a function to restore from storage.

@AnastasiaDunbar
Copy link
Author

Now it looks kinda bad.

@AnastasiaDunbar
Copy link
Author

AnastasiaDunbar commented Sep 15, 2019

  • I think I'd also like to read their dates, to see when they were saved as in “x hours ago”, “x days ago”.
{let magic="store(code); ";Object.keys(localStorage).filter(x=>x.startsWith(magic)).map(x=>new Date(+x.substring(magic.length).match(/\d+/)[0]));}
  • Could be helpful to add an ability to remove old items from within the script.
  • It shouldn't ask to restore if there aren't any stored, which would make it more explanatory as well not to start with this question to someone first time using it.
  • Avoid storing an identical value.
  • Ask to replace the previous one? (I'm not sure if it's good or not.)
  • Add Google, Discord, Repl.it, p5.js editor, online text editor tools.
  • On OpenProcessing, inside the div.codeContainer there's div#codeTabs above div[class^="CodeMirror"], and li.selected within the ul element is the current tab.

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