Created
January 2, 2024 16:35
-
-
Save mvark/aabbe96eb2d62a627749fc700a5dfac4 to your computer and use it in GitHub Desktop.
Snippets Bookmarklet saves snippets you need with custom names & retrieves them with a single click
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
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"); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment