Double-click Image Downloader

Double-click images to download them.

// ==UserScript==
// @name        Double-click Image Downloader
// @namespace   leaumar
// @match       *://*/*
// @grant       GM.download
// @grant       GM.xmlHttpRequest
// @connect     *
// @version     3
// @author      leaumar@mailbox.org
// @description Double-click images to download them.
// @license     MPL-2.0
// @icon        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAQAAAD2e2DtAAAhcUlEQVR42u2dCXAU15nHOXUiC51IoAsEQhKHxGUQEggJG4QccUrikoXEYZBBCBxCgnet2NnlSBav7XLKriRe4i1iXFuwKbtMiqJ2zS42pfWWqRhT5SPGTkFsqhJIII6LdUQM//2/7p7RSKh7umd6RjM9r7+yC0kz73W/7/e+473X7w0aJC95+X5hsBSr4kTFD5FiSpwAQj9KHyrFtNwDQ7iqXjzMMMpwSpQm0VJ0xdVGor1Eu7lgCBcMeqleVbxQeAxiKXGIV2SEFF1RWyhOaa8YBQgXCG4MQl39LuUL1QvFC4Xfh0SMRBKSKSmUVCn9imgb0UZJbK1EtpoAQoCgYuCGILT7vlC+6PVC9Ql8jGQ+WDoyMBpjkIVs5FBypfQrom2y2Upj2FoZbLVUtl4iW1FgIKyBCkEo2gEP5Q/nrcaRXaH6NGTycXIxDhMwEUWYhMmYgqmUEin3iGiXKWyhSWypiWyxcWy5LLZgmoLBCLZqtNsShBICHn1/uNLzR9CApWAUbz6Pj1HEx5qOWZiDcszDfCxAlSLVUnqJ2ioL2ELz2FJz2GLT2XJFbME8tuQotuhItmysAkEo2YFehj+GxiqRt5pBQ5bPmy/hY5TzoR7EEtRhOVahAY1YjTWUtVJ6iWiT1WydBrbScrbWErbaArbeLLZiEVszm62awtaNV2KCULEDHuoXfT+B4Us6ec1HMfktI9E1WMaHakIrtmAbtqMdHdiF3ZTHpPQS0Sa72DrtbKVtbK1WtlojW6+GrVjG1ixmq2axdZPYysIOhAICvUx/HKPWFHqsPBRiGuZiISluRDMeOde5+1h1V8FnKTeiu4fcHQQpxhLdnXKj4LPqrt3HznX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wSzVdZieRjSZ2a7po8jtY4gaWvIw1zGdNJaxRKH+Uovw+hl+qP9B2YIhyDLXqDBLpewUEYzERUzCT8Xg1lmA5ldXM/rrj3ScfP1bTlX9F72AavcNc8q/UdD1+7N0n6e23sKTVLHEJSy5nDVNY01hF+Sms3WX4hyn3JZUfVDvQA4FwB6MZio1HMXvnLCqqCjVYinrm6BuowkfRcebAwSObTtZ0zfgg/0r69YSvoruH3lHmne5Edyd8lX49/8qMD2q6Np08eOTMAfb4R/mtDfx2PUupYWnlLLWEpY9nLaMVs++pfNn3BxgC4Q6SkMpYXNiCCQoGMxmkVeIBqq+OuXojldnM2H0zjXkb8/d2evQOTXbyp+387Vb+tZWfWsdPr+S3avjtSpYyU1H9BKXfZ7CWJMXsS+WHFAQiMExQbEEaMqmoPPbVQubnpey5c1CBBViIxQzfvkU/voKBXD0j+UZNGvjTKv52Gf9ay08t5Kcr+K1Z/PZkljKepWWx1DSl3ycoAZ9UfshBEKXZggSmZAKDDKZnOey14+mzi+m5SzFDgWEuzfk8hnKVVLMqlfxpHn87V1H6DH5yCr8xkd8cyxLGsCSh+pEsWe33UVL5oQdBDwYxGgaJNNUpCgij2X9zqcx8mvEC9ugiqncS+/YUTSbzp2L+tpB/ncBPjeWns/gtofgUlpKoqT7GQ/VS+SEKgac1iNNAEBZBoJBOlWZSsWOo3mxKjibi31n87Wj+NYOfEmoXPV5VfFyvXi+VH9IQ9MUgSosO4hmxJzB0EzCMZJ9OVpDoEfFzkvK3RH4qgZ+O1zx9VH+ql8oPFwxUEHpQiFHsQpyCRG9RfxurfMaldpfiperDFAMVhB4UVBhUHPqT4ZrSPdU+pKc02bLhC4ILhR4c+peeTw2WincmCPcCcY/CpeIjDQapdHmF2vX/rkkFZNtytJkAAAAASUVORK5CYII=
// ==/UserScript==

class HttpError extends Error {
  constructor(verb, response) {
    super(`HTTP request ${verb}.`, {
      cause: response
    });
  }
}

function httpRequest(method, url) {
  return new Promise((resolve, reject) => {
    function fail(verb) {
      return error => reject(new HttpError(verb, error));
    }

    GM.xmlHttpRequest({
      url: url.href,
      onload: resolve,
      onerror: fail('errored'),
      onabort: fail('aborted'),
      ontimeout: fail('timed out'),
      responseType: 'blob',
    });
  });
}

function httpDownload(url, name) {
  return new Promise((resolve, reject) => {
    function fail(verb) {
      return error => reject(new HttpError(verb, error));
    }

    GM.download({
      url: url.href,
      name,
      onload: () => resolve(),
      onerror: fail('errored'),
      onabort: fail('aborted'),
      ontimeout: fail('timed out'),
      responseType: 'blob',
    });
  });
}

// -----------------

// from the greasemonkey docs
const lineSeparator = '\r\n';
const headerSeparator = ": ";

// is it still the 90s?
function parseHeaders(headersString) {
  return headersString.split(lineSeparator).reduce((accumulator, line) => {
    const pivot = line.indexOf(headerSeparator);
    const name = line.slice(0, pivot).trim().toLowerCase();
    const value = line.slice(pivot + headerSeparator.length).trim();
    accumulator[name] = value;
    return accumulator;
  }, {});
}

// ----------------

function filterFilename(name) {
  // foo.jpg
  return /^.+\.(?:jpe?g|png|gif|webp)$/iu.exec(name)?.[0];
}

async function queryFilename(url) {
  const response = await httpRequest('HEAD', url);
  const disposition = parseHeaders(response.responseHeaders)['content-disposition'];
  if (disposition != null) {
    // naive approach, but proper parsing is WAY overkill
    // attachment; filename="foo.jpg" -> foo.jpg
    const serverName = /^(?:attachment|inline)\s*;\s*filename="([^"]+)"/iu.exec(disposition)?.[1];
    if (serverName != null) {
      return filterFilename(serverName);
    }
  }
}

function readFilename(url) {
  const branch = url.pathname;
  const leaf = branch.slice(branch.lastIndexOf('/') + 1);
  return filterFilename(leaf);
}

function sleep(millis) {
    return new Promise(resolve => setTimeout(resolve, millis));
}

async function downloadImage(url, name, image) {
  const opacity = image.style.opacity ?? 1;

  image.style.opacity = 0.5;
  await Promise.all([httpDownload(url, name), sleep(100)]);
  image.style.opacity = opacity;
}

async function onDoubleClick(dblClick) {
  if (dblClick.target.nodeName === 'IMG') {
    const imageElement = dblClick.target;
    const url = new URL(imageElement.src, location.origin);
    const name = readFilename(url) ?? await queryFilename(url);
    if (name == null) {
      throw new Error('Could not determine a filename.');
    }
    await downloadImage(url, name, imageElement);
  }
}

(function main() {
  document.body.addEventListener('dblclick', dblClick => onDoubleClick(dblClick).catch(console.error));
})();