Get a random ID string in MongoDB

December 22nd, 2015 by Alex No comments »

Sometimes you need to make an ID a string while inserting some data in MongoDB. This is very simple, you just need to create a Mongo object id like this:

db.mycollection.insert({ _id: ObjectId().valueOf(), other_field: 123 });

Test server Accept-Range with cURL

June 22nd, 2015 by Alex No comments »

In case you want to make sure your videos on your very own web server can handle seeking, here is a very simple command to test it:

curl -r 1-20000 http://myurl.mp4 -o test.mp4

If the result test.mp4 is 20KB, it works.

Enable Apache2 mod_rewrite on Linux

September 22nd, 2014 by Alex No comments »

There is a special tool for that, called a2enmod, so we can just run

a2enmod rewrite

Or sudo if you are not root

sudo a2enmod rewrite

Then, restart apache

service apache2 restart

Download all images from current page with JavaScript

August 18th, 2014 by Alex No comments »
// get possible elements, init variables
var elems = document.querySelectorAll('*'), elems2 = document.querySelectorAll('img');
var images = {}, a = 0, href = '';
// search for CSS background-image
for (a = 0; a < elems.length; a++) {
  style = elems[a].currentStyle || window.getComputedStyle(elems[a], false);
  var bg = style.backgroundImage.slice(4, -1);
  if (bg) {
    images[bg] = bg;
// search for <img src="" />
for (a = 0; a < elems2.length; a++) {
  if (href = elems2[a].getAttribute('src'))
    images[href] = href;
// generate <a> and force download
for (var i in images) {
  var link = document.createElement('a');
  link.setAttribute('download', '');
  link.setAttribute('href', i);;
  delete link;

Adding a message with JavaScript before leaving the page

June 20th, 2014 by Alex 1 comment »

It is very very easy, the callback you add should return a string that will be prepended to the confirmation message. Plain javascript:

window.onbeforeunload = function(){
  return 'You are about to lose your changes!';

Or with jQuery if you wish so

$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';