steamdb.info - download depot sha1 file

5/30/2023, 5:53:13 PM

// ==UserScript==
// @name        steamdb.info - download depot sha1 file
// @namespace   Violentmonkey Scripts
// @match       https://steamdb.info/depot/*
// @grant       none
// @version     1.3
// @author      klaufir
// @license     MIT
// @description 5/30/2023, 5:53:13 PM
// ==/UserScript==

// Function to download data to a file
// https://stackoverflow.com/a/30832210
function download(data, filename, type) {
    var file = new Blob([data], {type: type});
    if (window.navigator.msSaveOrOpenBlob) // IE10+
        window.navigator.msSaveOrOpenBlob(file, filename);
    else { // Others
        var a = document.createElement("a"),
                url = URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        setTimeout(function() {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 0);
    }
}

function get_metadata() {
  var meta_table_trs = document.querySelector('table.table.table-bordered.table-hover.table-responsive-flex')
    .querySelectorAll('tr');
  var meta_pairs = Array.from(meta_table_trs).map(tr => [tr.querySelector('td:nth-child(1)').innerText, tr.querySelector('td:nth-child(2)').innerText]);
  var meta = Object.fromEntries(meta_pairs);
  meta['creation_date'] = document.querySelector('time.timeago').getAttribute('datetime').slice(0,10);
  return meta;
}

function onclick(elem) {
  function get_hashes() {
      var tr_rows = document.querySelectorAll('#files table tr');
      var hashes = Array.from(tr_rows)
          .map(e => Array.from(e.querySelectorAll('td')))
          .filter(tds => tds.length >= 5 && tds[1].innerText != 'NULL')
          .map(tds => ({filename: tds[0].innerText, hash: tds[1].innerText}));
      //var hashes = Array.from($('.file-tree').DataTable().rows().data()).map(row => ({filename: row[0], hash: row[1]})).filter(row => row.hash != 'NULL');
      return hashes.map(e => `${e.hash} *${e.filename}\r\n`).join('');
  }

  var meta = get_metadata();
  var filename = `depot-${meta['Depot ID']}-build-${meta['Build ID']}-manifest-${meta['Manifest ID']}-${meta['creation_date']}.sha1`;
  download(get_hashes(), filename, 'text/plain');
}

function main() {
  var files_h2 = document.querySelector('div#files h2');
  var br = document.createElement('br');
  var btn = document.createElement('button');
  btn.innerText = 'download sha1 file'
  files_h2.appendChild(br);
  files_h2.appendChild(btn);
  btn.onclick = onclick;


  var select = document.querySelector('select');
  select.selectedIndex = 5
  select.dispatchEvent(new Event('change'))
}


if (document.querySelector('a[href$="?show_hashes"]') === null
    && document.querySelector('div[style="margin-left:auto;align-self:center"] a[href^="/login/?page=depot"]') === null ) {

  main();
}