Skip to content

Instantly share code, notes, and snippets.

@cyyyu
Last active March 8, 2024 03:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cyyyu/f45a8bb9bb9e2b7a1a3a468e592c808b to your computer and use it in GitHub Desktop.
Save cyyyu/f45a8bb9bb9e2b7a1a3a468e592c808b to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Type Guard
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Chuang Yu
// @match *://*/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant none
// ==/UserScript==
(function () {
"use strict";
// Get all text boxs including inputs and textareas
const textboxes = document.querySelectorAll("input, textarea");
function setCache(key, value) {
localStorage.setItem(key, value);
}
function getCache(key) {
return localStorage.getItem(key);
}
// Generate a unique id based on the input's properties
// Even after the page is refreshed, the id will be the same
function generateId(element) {
return element.id || element.name || element.placeholder;
}
// Save the value to cache
function onInput(event) {
const id = generateId(event.target);
// Update the value only when it's not empty. This is to avoid accidentally clear up the input box or leave a space but press enter key
// (thanks @jt-wang)
if (event.target.value !== undefined && event.target.value !== null && event.target.value.trim() !== "") {
setCache(id, event.target.value);
}
}
// When pressing cmd + shift + p, retrieve the value from cache to the active input
function onKeyCombo(event) {
if (event.metaKey && event.shiftKey && event.key === "p") {
event.preventDefault();
const id = generateId(event.target);
const value = getCache(id);
event.target.value = value;
}
}
let loaded = false;
if (!loaded) {
// Listen to all input events
for (var i = 0; i < textboxes.length; i++) {
textboxes[i].addEventListener("input", onInput);
textboxes[i].addEventListener("keydown", onKeyCombo);
}
loaded = true;
}
console.log("Type Guard Loaded");
})();
@jt-wang
Copy link

jt-wang commented Mar 7, 2024

Hi @cyyyu, thanks for this wonderful gist! It helps me a lot with using ChatGPT. I made a revision to make sure it doesn't store empty input by accident (e.g. sometimes I clean up the input box). It would be great if you consider merging this. Thanks!

https://gist.github.com/jt-wang/f66017a67a80a75457020a82a01cff8a/revisions

@cyyyu
Copy link
Author

cyyyu commented Mar 8, 2024

Hi @cyyyu, thanks for this wonderful gist! It helps me a lot with using ChatGPT. I made a revision to make sure it doesn't store empty input by accident (e.g. sometimes I clean up the input box). It would be great if you consider merging this. Thanks!

https://gist.github.com/jt-wang/f66017a67a80a75457020a82a01cff8a/revisions

Thanks @jt-wang for the fix! I don't know how merging works for gists so I simply copy/paste the change over here. Glad it helps you

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