News for hackers. Stuff that matters.

Show HN: Replay your typing in a few lines of JavaScript

https://einenlum.github.io/human-replay/

I recently needed to make a text appear on a website and I wanted to get this real human feeling that computers don't have. I only found the TypeIt lib but it was not free and I didn't want to add a dependency for such a simple case.

Human replay allows to copy paste a few lines of JS to make a text appear exactly how you typed it.

( 37 comments | 3 points | on HN )
Re:Show HN: Replay your typing in a few lines of JavaScript by OmarShehata on Friday March 01, 2024 @11:54AM (#39563700)
This is great!

I was just talking to a coworker today about "libraries" vs "templates". You could have made this a library, but I think it's actually way nicer for it to be code I can copy/paste, so I can integrate it however I want. Some things are better as templates that I can copy.

If I have a weird use case or want to integrate it deeply into something else, the library doesn't need to accommodate that.

(the tradeoff of course is, if you update the library, my copied code won't update, so obviously this doesn't work for everything, but I think it works for some things!)

Re:Show HN: Replay your typing in a few lines of JavaScript by sonofhans on Friday March 01, 2024 @02:00PM (#39565407)
This is nice.

I’m curious — it looks like you’re storing the full text string along with the timestamp. At first I thought you were capturing keystrokes, but it makes more sense to capture the full text, so you can allow people to edit with mouse/arrow keys, etc.

It will fall over at some point though, right? If you work on this more, is the next logical step to store a diff with each timestamp, rather than the full string?

Re:Show HN: Replay your typing in a few lines of JavaScript by seafoamteal on Friday March 01, 2024 @05:53PM (#39567790)
That's what I was thinking as well. I loved the demo, but was a bit surprised at how much redundant data was stored behind the scenes.

Capturing keystrokes could still be a viable option if you maintained something like a virtual cursor that could move around the text display area.

Or of course, diffs are the most straightforward way to do it.

Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @06:53PM (#39568291)
Thanks, this is an interesting point. Storing the full message indeed has a downside in terms of storage (but for a short text it should be unnoticeable). I guess it also brings something positive: you can select the whole text, delete it, and start again, and the whole action should be stored. Making a diff to represent this is way more complicated.

I'll still think about this though. I think I could find a way to reduce the data size for most use cases.

Re:Show HN: Replay your typing in a few lines of JavaScript by a-r-t on Friday March 01, 2024 @07:50PM (#39568739)
You can introduce multiple types of changes, for example: addition at the end, removal of N characters from the end, and complete string replacement, etc. Then for each change you can calculate which type is more efficient to store.
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @08:06PM (#39568832)
Yes, already working on it! Thanks!
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Saturday March 02, 2024 @04:44AM (#39571365)
I changed the code to add a diff. Thanks for your input! (pun intended)
Re:Show HN: Replay your typing in a few lines of JavaScript by sonofhans on Saturday March 02, 2024 @12:45PM (#39574201)
That was quick! It seems to work as well as ever. The data structure is less legible, but a lot more efficient.

Such a little project. I hope to find a use for it.

Re:Show HN: Replay your typing in a few lines of JavaScript by jmkni on Friday March 01, 2024 @02:02PM (#39565450)
Very clever, I can imagine a chat app where you see the message how it was typed before seeing the final message sent, could be novel
Re:Show HN: Replay your typing in a few lines of JavaScript by michaelmior on Friday March 01, 2024 @02:10PM (#39565559)
Sounds like ICQ circa late 90s :)
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @06:54PM (#39568304)
Oh, I had not thought about this. Cool idea!
Re:Show HN: Replay your typing in a few lines of JavaScript by michaelmior on Friday March 01, 2024 @02:11PM (#39565575)
Not quite the same, but if you want to do this with a terminal session, asciinema[0] is great!

[0] https://asciinema.org/

Re:Show HN: Replay your typing in a few lines of JavaScript by amelius on Friday March 01, 2024 @04:06PM (#39566802)
Why can I only replay once?
Re:Show HN: Replay your typing in a few lines of JavaScript by sourabhrathour on Friday March 01, 2024 @04:31PM (#39567035)
exactly, there should have been to replay it for more number of times.
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @07:03PM (#39568375)
Oh, interesting point. I fixed this. Thanks!
Re:Show HN: Replay your typing in a few lines of JavaScript by MegaSec on Friday March 01, 2024 @04:20PM (#39566929)
Man, this is really cool!
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @06:53PM (#39568294)
Thank you!
Re:Show HN: Replay your typing in a few lines of JavaScript by js4ever on Friday March 01, 2024 @04:59PM (#39567283)
Amazing, I love how it's generating short js code and no library needed
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @06:54PM (#39568301)
Thanks! I appreciate. I indeed think for such short snippets, adding a dependency is unnecessary.
Re:Show HN: Replay your typing in a few lines of JavaScript by daniel_iversen on Friday March 01, 2024 @07:15PM (#39568497)
That’s cool. Wonder how well this could be used to create a digital fingerprint of users?
Re:Show HN: Replay your typing in a few lines of JavaScript by kylecazar on Friday March 01, 2024 @08:16PM (#39568877)
Using current methods it would be difficult to uniquely identify an individual user out of all based solely on their typing pattern (because entropy, they aren't unique enough). But you could fairly reliably use it as a 2nd factor to verify someone as themselves vs. a malicious user
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Friday March 01, 2024 @07:27PM (#39568595)
Thanks! It's not the point of this lib, but I know that unfortunately some tools exist to fingerprint users by recording their keystrokes: https://www.whonix.org/wiki/Keystroke_Deanonymization
Re:Show HN: Replay your typing in a few lines of JavaScript by avery17 on Friday March 01, 2024 @09:53PM (#39569396)
Hold backspace from the end until you erase everything. Start recording and hold down undo(ctrl-z). You're welcome.
Re:Show HN: Replay your typing in a few lines of JavaScript by codetrotter on Friday March 01, 2024 @11:36PM (#39569840)
It even captures changes made by iOS when auto correct changes the capitalization and spelling of words! :D
Re:Show HN: Replay your typing in a few lines of JavaScript by darkest_ruby on Saturday March 02, 2024 @01:33AM (#39570376)
What is everyone excited about? A mediocre piece of code that actually hardcodes th string? Same could be achieved with generic code with 2 Parms - a string, and duration
Re:Show HN: Replay your typing in a few lines of JavaScript by onion2k on Saturday March 02, 2024 @06:06AM (#39571717)
If you want to focus on code maintainability of your web app then you're right - a generalized library with a simple API is the way to go.

On the other hand though, if you want to deliver the minimum code to the user to optimize for download speed, parsing time, and efficiency then a code generator that outputs some 'hardcoded' JS that you drop into the page is the way to go. I really like this approach and I've written plenty of generator functions like this in the past. It's more user-focused than importing a library to the same job.

This is one of the decisions every web dev has to make for themselves. Ideally when a site calls for a tiny function for an effect like this they'll opt for some 'bad' code that results in a better user experience.

Re:Show HN: Replay your typing in a few lines of JavaScript by mosselman on Saturday March 02, 2024 @02:32AM (#39570704)
While we are on the subject: what does chatgpt use for the typing effect they have?

I know a stream of diffs comes in, but I think the animation is smoothened by something.

Re:Show HN: Replay your typing in a few lines of JavaScript by blharr on Saturday March 02, 2024 @07:58PM (#39577352)
I don't think it does any other processing honestly, it might have a slight delay added in on each token, but it's pretty close to just streaming the output
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Saturday March 02, 2024 @04:43AM (#39571362)
Update: I first had a naive approach and stored the full content of the text on each stroke. I updated the code with some small algorithm to reduce data and avoid storing the full text when a character was added or removed.
Re:Show HN: Replay your typing in a few lines of JavaScript by p2hari on Saturday March 02, 2024 @01:07PM (#39574407)
Really cool. Thanks for sharing.
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Saturday March 02, 2024 @01:31PM (#39574650)
Thank you!
Re:Show HN: Replay your typing in a few lines of JavaScript by anon115 on Saturday March 02, 2024 @01:08PM (#39574419)
a web component in vanilla js would fit nice here
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Saturday March 02, 2024 @01:31PM (#39574654)
Mmm interesting idea Gonna think about it
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Monday March 11, 2024 @04:46PM (#39673049)
It has been added to the project.
Re:Show HN: Replay your typing in a few lines of JavaScript by tamimio on Saturday March 02, 2024 @03:02PM (#39575332)
Cool!! Thanks for sharing it.
Re:Show HN: Replay your typing in a few lines of JavaScript by Einenlum on Saturday March 02, 2024 @03:14PM (#39575402)
Thanks! :)
Re:Show HN: Replay your typing in a few lines of JavaScript by t_zahil on Monday March 04, 2024 @03:02AM (#39588066)
What a smart idea! I immediately sent it to my mom who has such a weird way of typing can't wait to see if the AI can reproduce it!