Skip to content

Instantly share code, notes, and snippets.

@GrzegorzManiak
Created May 1, 2022 18:31
Show Gist options
  • Save GrzegorzManiak/1f68f180e19490886fd278533861553c to your computer and use it in GitHub Desktop.
Save GrzegorzManiak/1f68f180e19490886fd278533861553c to your computer and use it in GitHub Desktop.
Allows you zip up all files from thingiverse as they removed it
// ==UserScript==
// @name ThingiBetter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Makes it so you get a ZIP of all files instead of having to download them 1 by 1
// @author You
// @match https://www.thingiverse.com/thing:*
// @icon https://www.google.com/s2/favicons?sz=64&domain=thingiverse.com
// @grant none
// ==/UserScript==
// find the span element with the innerText "Download All Files"
const button = () => Array.from(document.querySelectorAll('span'))
.find(el => el.textContent === 'Download All Files') ?? undefined;
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getThingID = () => {
const url = window.location.href;
// /.+/thing:([0-9]+)
const regex = /\/.+\/thing:([0-9]+)/;
const match = url.match(regex);
if (match)
return match[1];
return undefined;
}
const main = async() => {
// wait for the button to appear
while (!button()) {
await sleep(100);
}
const id = getThingID(),
btn = button();
// If there is no id, return
if (!id) return;
// get the 2nd parrent of the button
const parent = btn.parentElement.parentElement;
// add onclick handler with our own
parent.onclick = async() => {
// Download the filles
const files = await fetch(`https://www.thingiverse.com/thing:${id}/zip`);
// save the zip file
const blob = await files.blob();
const url = window.URL.createObjectURL(blob);
// save the blob to the user's computer
const a = document.createElement('a');
a.href = url;
a.download = `thingiverse_${id}.zip`;
a.click();
return;
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment