Bookmarklet to Save & Retrieve Frequently Used Text Snippets

Do you often type the same text over and over again? 

Snippets Bookmarklet saves snippets of the text you need, in your browser storage with names of your choice & retrieves them with a single click. 

Just click the bookmarklet anytime you find something you want to save for later. Give your snippet a name, and it will be stored securely in your browser.

To view your saved snippets, click the bookmarklet again and select the name you gave it. The snippet will be inserted in the input element where you need it.

I employed Anthropic's Claude AI assistant to generate bookmarklet code by prompting my requirement:

javascript:(function(){
//The getInput function is defined to prompt the user for input.
function getInput(text){return prompt(text)}
//the localStorage will be used to store & retrieve keys and values.
d = localStorage;
//The user is prompted to enter either "get" or "set" to choose the action.
a = getInput('Enter "get" or "set": ');
//If the user chooses "get," they are prompted to enter a key.
if (a == "get") {
k = d.length ? [...Array(d.length)].map(i => d.key(i)).filter(key => key.startsWith('z_')) : []; // Filter keys with "z_" prefix
key = getInput("Keys: " + k + "\nEnter key:");
//If the key exists, the corresponding value is inserted into the textarea with the ID "prompt-textarea".
if (d.getItem(key)) document.getElementById("prompt-textarea").value = d.getItem(key);
//If not, an alert is displayed indicating that the key does not exist.
else alert("Key does not exist!");
}
//If the user chooses "set," they are prompted to enter a new key and a value, and this key-value pair is stored in the local storage.
else if (a == "set") {
key = "z_" + getInput("Enter new key: "); // Add "z_" prefix automatically to identify our custom keys
value = getInput("Enter value to store:");
//key-value pair is stored in localStorage.
d.setItem(key, value);
} else {
alert("Invalid action");
}
})()
view raw snippets.js hosted with ❤ by GitHub

Here's the compact version of the same code:
javascript:(function(){function g(t){return prompt(t)}d=localStorage;a=g('Enter "get" or "set": ');if(a=="get"){k=d.length?[...Array(d.length)].map(i=>d.key(i)).filter(k=>k.startsWith('z_')):[];k=g("Keys: "+k+"\nEnter key:");if(d.getItem(k))document.getElementById("prompt-textarea").value=d.getItem(k);else alert("Key does not exist!")}else if(a=="set"){k="z_"+g("Enter new key: ");v=g("Enter value to store:");d.setItem(k,v)}else alert("Invalid action")})()

Here's how to use it:
  1. Create a new bookmark in your browser.
  2. Edit the bookmark and set the name to something like "Snippets".
  3. Paste the above code into the URL or Location field.
Alternatively drag and drop this link (after checking and adapting the words-per-minute rate) to the Bookmarks bar or Favorites bar in Chrome or Edge to see how it works - Snippets

In the code make sure to change the id of the text area/text box from the value of prompt-textarea as specified in the code to something that matches the id of your input element.

Comments

Popular posts from this blog

The Mercurial Grok AI Assistant Understands & Speaks Indian Languages

Things Near Me – Find & Learn About Landmarks Nearby