Skip to content


How to get a random item from a JavaScript array

var myArray = ['a','b','c'];
var randomItem = myArray[Math.floor(Math.random()*myArray.length)]

Math.random() returns a number between 0 (included) and 1 (not included), which is why this piece of code won’t overshoot by picking a number equal to the array length (reminder: array indexes start at 0, meaning last index is length – 1).

Math.round() is not appropriate as it would result in sometimes hitting a number equal to the array length. Hence the Math.floor().

Posted in JavaScript / TypeScript / Node.js.


How to configure email and name in Git

I’m using SourceTree as a Git GUI for the sake of simplicity, but some settings are still hard to find. Among those, how to set up (or change) the name and email that you’ll automatically attach to your commits, either system-wide or for the current repository.
Gladly, it’s easy enough to do via the command line: for the current repository, that would be simply:

git config user.email "me@example.com"
git config user.name "My Name"

If you want to set those globally, just add a --global flag, e.g.:

git config --global user.email "me@example.com"

Posted in programming.


How to make Tor Browser remember permission settings across sessions

Warning: this is not recommended at all, as it makes it easier to link your different browsing sessions. I’m actually mainly posting it here for future reference on how to disable it. If you really do know what you’re doing, read on.

It’s simply a boolean setting: in about:config, set permissions.memory_only to false if you want Tor Browser to remember your permissions from a browsing session to another. This can be used for instance when you’re tired of being asked for permissions to use HTML 5’s Canvas-of-Doom. Personally, I’d prefer an easy setting to just kill Canvas once and for all.

Posted in privacy, security, Tor.


A few things I learned from my Windows 10 disaster recovery

After a crash, Windows 10 managed to restart but decided its partition should be “fixed”. So I rebooted to let it “fix” it, which caused it to become unbootable. My few recovery attempts failed, and since I had a partition setup that would preserve my important data (ie, everything except what the stupid programs decide to store into %appdata%), I quickly reinstalled. And learned a few interesting things in the process.

About installing over a previous Windows

First, when you install Windows on a partition which already contained Windows, it warns you it will move the previous Windows installation to a Windows.old folder. I was afraid it would only move the former “Windows” folder there, but it turned out it moved everything on the partition there (NB: I only had standard folders, like Users, Program Files, etc, so I’m not sure it would also move custom root folders although my guess is it probably would). Pretty handy to grab your former program settings (those in %appdata%, sadly not those in the registry), documents (I got to keep my desktop contents, yay), etc.
Edit: well actually, something strange happened just the day after I wrote this part: most of the contents of that Windows.old folder vanished overnight… :s So now I’m happy I used a Linux LiveUSB (NB: Tails is nice for that, as it’s pretty small) to duplicate the contents of the C: partition elsewhere myself.

About Cortana and other bloatware

Second, there exists a version without Cortana, Windows Store, and Windows Media Player. It’s version N (without Windows Media Player) and Long Term Servicing Branch (LTSB), which is without Cortana, Windows Store, but also the Edge browser, Photo Viewer and the UWP version of Calculator. Wikipedia has a nice page detailing the different editions and notes that Windows 10 Enterprise N LTSB is the most stripped down edition of Windows 10 available.

The N flavor will create some issues for video playback in your browsers. For instance, Youtube will work, but Twitch won’t (there are other ways to watch Twitch streams though, for instance Streamlink). If that turns out to be a blocker, however, you can download a patch by Microsoft (the “Media Feature Pack”), which will restore those functionalities. So there’s no risk in trying out N, it’s not irreversible.

The LTSB comes without Edge, so I was wondering if I’d need Firefox on a stick to get started 😉 But it turns out it contains… good old Internet Explorer 11. The lack of Photo Viewer means your pictures will open in Paint (or Internet Explorer as usual for the GIFs). I still have to find a replacement, because opening pictures in Paint works great but I find it a bit risky (I’m always afraid to accidentally draw a few pixels and save)

About PuTTY and my backup software

Third, I already used it before but I had forgotten about it, PuTTY settings can be easily exported to a reg file with this command:

regedit /e "%userprofile%\Desktop\putty.reg" HKEY_CURRENT_USER\Software\SimonTatham

(source)
Of course, this only works on a working Windows. In my case I gladly had those settings +/- duplicated exactly on another computer, so yay ^^

Fourth (and last for this post ^^), backup software are unequally easy to migrate.
For SpiderOak, I simply copied the old AppData/Local/SpiderOak folder and then reinstalled SpiderOak, and it behaved just as if nothing changed.
For LiveDrive, I used a backup I had of the registry settings (and found nothing of value in %AppData%), and it recognized the computer but had lost the most important backup settings (namely, which folders to back up, for fucking fuck sake)

Well that will be all. Hopefully I won’t have a use for this in the future, but for some reason I’m pretty sure I will :/

Posted in Windows 10.


Emojis in a MySQL database

A while back, I’ve had issues when user input contained an emoji: the database field would then cut after (and including) the first emoji). My table seemed to be configured properly for Unicode though, so that was a bit puzzling.

It turned out that “utf8“, in MySQL/MariaDB language, doesn’t actually mean the real, proper UTF-8: it contains a MySQL-specific limitation preventing use of characters numbered above 0xFFFD (source, with also a comparison between utf8mb4_unicode_ci and utf8mb4_general_ci). So you’ll want to use utf8mb4, which is the real deal and will store emojis without issue.
The good news is, if you already have an utf8 database or table, the migration may be as easy as just switching the column types (still you should make a backup first, just to be safe), at least for me it was. You might want to have a look at this first, though: How to support full Unicode in MySQL databases.

While looking into this, I also found some stuff about utf32. Not sure if I’ll ever use it but here it is for bookmarking purpose.

Sorry that was once again a brief post, I’m cleaning up my tab groups because after Firefox decided to kill the feature, they now also killed the extension that tried to continue the feature… Thanks again Mozilla! 😡

Posted in web development.


How to generate a file of a specified size on Windows

The command is:
fsutil file createnew [filename] [size in byte]

Note that it doesn’t generate random contents: the contents is all zero bytes. If you want a random file, you can use something like Random Data File Creator, or encrypt a zero file that you generated with fsutil (I find AES Crypt pretty convenient for that purpose)

Posted in Windows.


How to get rid of Music.UI.exe

Lately (maybe since last month), I’ve had that Music.UI.exe executable, located in C:\Program Files\WindowsApps\Microsoft.ZuneMusic_3.6.19261.0_x64__8wekyb3d8bbwe, regularly (like, every few hours) popping up in my firewall, trying to access the network 😮 Every time, I denied it and killed it, but it kept coming back.

I tried deleting the file, with no success (permission denied, even as admin).
I eventually found the solution: go to Settings > Privacy > Background Apps, and turn off Groove Music. While you’re at it, you might want to disable the shitload of other apps that are there too.

Posted in Windows 10.


How to prevent Windows 10’s auto volume reduction?

Sadly this time it is still an open question.
Whenever there’s a system beep sound, Windows 10 automatically lowers the volume of everything else for a pretty damned long while (maybe 5-10 seconds). This is particularly annoying when trying to adjust the volume, as adjusting the volume will itself generate a beep, causing the volume to be automatically reduced (WTF were those engineers thinking when then did that? :o).

Anyway, apparently this could be caused by Windows trying to be smart and “guessing” that you are using VoIP, and then reducing the volume of other activities so that you can hear your communication better. The solution, as I found there for instance, would be to disable that feature, by going to system sound settings, then in the “Communications” tab and choosing “When Windows detects communications activity, do nothing” (the default is a huge “reduce the volume of other sounds by 80%” :o).

Sadly, for me it didn’t do the trick, even after a restart (which was suggested there).

I’m running out of ideas to fix this (already), so if you have a magical solution do post it in the comments ^^

Off-topic bonus: apparently Windows 10 comes with an annoyance called Xbox DVR, which doesn’t seem to be necessarily linked to owning an Xbox and can fuck up performance in some games. Steam provides a guide to disable it there.

Posted in Windows 10.


aToad #23: Dan’s Tools

Lots of little development-related tools

This time it’s not one tool but a pretty large collection of tools. I found Dan’s Tools while looking for a Unix timestamp to date and time converter (or the other way around), but the site also have many other helpful little (or not that little) things, such as:

  • HTML formatter
  • Javascript formatter and minifier
  • JSON formatter, minifier and validator
  • PHP formatter
  • Python formatter
  • Ruby formatter
  • SQL formatter
  • CSS sprite generator
  • Regex testing
  • SHA-1 hash generator
  • URL encode/decode

… and some more. Quite a tool belt 🙂

Posted in A Tool A Day, programming.


My Visual Studio Code config file (TypeScript-oriented)

Just to find it easily whenever I switch computer, my Visual Studio Code user settings file (for TypeScript development):

{
    "telemetry.enableCrashReporter": false,
    "telemetry.enableTelemetry": false,
    "typescript.check.tscVersion": false,
    "typescript.disableAutomaticTypeAcquisition": true,
    "files.exclude": {
        "**/*.js": { "when": "$(basename).ts"}
    },
    "update.channel": "none",
    "git.enabled": false,
    "git.path": null,
    "git.autofetch": false,
    "workbench.iconTheme": false
}

It should be quite self-explaining in most part, but what these settings do is:
– disable telemetry and crash reporter
– disable checking for mismatch between VSCode’s TypeScript and the globally installed TypeScript (which gives a pretty annoying warning that can’t be permanently dismissed and shouldn’t be a problem if you update everything regularly)
– disable Automatic Type Acquisition (ATA)
– hide .js files when a .ts file of the same name exists
– disable auto-updates
– disable Git integration (probably the 3 settings I used are a bit redundant though), although in the end I reactivated it because it’s pretty convenient to spot which lines you recently edited
– disable the file icon theme, which was enabled by default in version 1.13 and looks pretty nice indeed, but wastes too much space IMO and is a bit useless if your project is well-organized

Posted in JavaScript / TypeScript / Node.js.