anonymous1184/bitwarden-autotype

[Enhancement] Restore previous clipboard value

KnightTim opened this issue · 3 comments

When putting a TOTP code in the clipboard save the previous value in the clipboard and after 30sec (when the TOTP is no longer valid) restore the previous clipboard value.

While doable it shouldn't be forced to 30 seconds even if the vast majority of Key Uris use the 30 second default period, a more sensible approach is to use as timeout the period in the Key Uri (or the default if missing).

The TOTP algorithm uses floor divide to get the time counter, so for the Clipboard should be restored when the current second is either 0 or matches the period.

Again, thanks for your valuable input.

Looking at your commit I have a question. I am not familiar AHK scripting, but it looks like in bw-at.ahk the ClipData variable you added is for a string.
Do you need to check the type of data in the clipboard? Would non-text data cause an issue?

(I come from a background of strongly typed coding, in my world I would be likely to crash if the clipboard had say an image in it.)

Don't worry my friend, AHK is a bit weird as it started as a fork of AutoIt and that was a BASIC-like language, since then it has been slowly bastardized into C-like. And yes, being loosely typed means you can get away with this, also is the closest to the "proper way" of initializing a variable.

The variable doesn't even need to be initialized but I like to do it to comply with what's considered the "strict mode" (which is having warnings enabled via #Warn directive). The line where the Clipboard data is backed up:

ClipData := ClipboardAll

Uses the special built-in variable ClipboardAll which holds the binary representation of whatever the Clipboard contains, thus making it reliable for images, RTF-formatted text, etc.


I also come from more serious programming languages, but AHK scripting is so easy that is my to-go automation tool when it comes to Windows. However the code is written with the Linux kernel coding style (where applicable) and I even avoided expressions that might confuse beginners so the code remains as simple as possible. It also uses AHK own guidelines for code inclusion (even if it means underscores in function names); all in all, it would be a starting point if you're interested in learning the ropes.

That said I recommend you to keep an eye to the r/AutoHotkey sub as is a nice place to learn as the community is not plagued with purists like the C/Rust crowd.