Last active
June 29, 2024 13:53
-
-
Save MisterSirCode/8342aff651c10f5f81d70a75ba2469df to your computer and use it in GitHub Desktop.
Smorf.nl Model Extractor - Userscript for TamperMonkey. This simple tool will add a download button to model pages on smorf.nl and allow direct downloading of any crystal form you like.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Smorf Downloader | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-06-11 | |
// @description Adds a Download STL button to Smorf.nl | |
// @author SirCode | |
// @match https://www.smorf.nl/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=smorf.nl | |
// @grant unsafeWindow | |
// ==/UserScript== | |
(function() { | |
"use strict"; | |
JSC3D.Scene.prototype.getChild = function(a) { | |
unsafeWindow.scene = this; | |
return this.children[a]; | |
}; | |
window.addEventListener("load", function () { | |
// Inject into a common function in JSC3D to send the scene back to the extension | |
// Ugly code from old smorf editor page. Not my downloader, just adapted and cleaned up to work on normal smorf pages. | |
unsafeWindow.downloadstl = function() { | |
for (var a = "solid smorf\n", | |
b = scene.children[0].indexBuffer, | |
f = 0, | |
g = 1, | |
e = 2, | |
k = scene.children[0].faceNormalBuffer, | |
l = 0, | |
c = scene.children[0].vertexBuffer, | |
d; e < b.length; | |
) { | |
for (; - 1 != b[e];) | |
(a += "facet normal " + k[l].toFixed(4) + " " + k[l + 1].toFixed(4) + " " + k[l + 2].toFixed(4) + "\nouter loop\n"), | |
(d = 3 * b[f]), | |
(a += "vertex " + c[d].toFixed(4) + " " + c[d + 1].toFixed(4) + " " + c[d + 2].toFixed(4) + "\n"), | |
(d = 3 * b[g]), | |
(a += "vertex " + c[d].toFixed(4) + " " + c[d + 1].toFixed(4) + " " + c[d + 2].toFixed(4) + "\n"), | |
(d = 3 * b[e]), | |
(a += "vertex " + c[d].toFixed(4) + " " + c[d + 1].toFixed(4) + " " + c[d + 2].toFixed(4) + "\n"), | |
(a += "endloop\nendfacet\n"), | |
g++, | |
e++; | |
f = e + 1; | |
g = f + 1; | |
e = g + 1; | |
l += 3; | |
} | |
a += "endsolid smorf\n"; | |
// Old-style click-to-download | |
b = document.createElement("a"); | |
b.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(a)); | |
let el = document.querySelector("#naam > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)"); | |
b.setAttribute("download", `${el.innerText}.stl`); | |
b.style.display = "none"; | |
document.body.appendChild(b); | |
b.click(); | |
document.body.removeChild(b); | |
}; | |
// Add button to smorf menu to make use of the aforementioned downloader code | |
let btn = document.createElement("input"); | |
btn.setAttribute("type", "button"); | |
btn.setAttribute("onclick", "downloadstl()"); | |
btn.value = "Download STL"; | |
document.querySelector("#selects_buttons").append(btn); | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment