Skip to content

Instantly share code, notes, and snippets.

@R44VC0RP
Created February 24, 2025 18:44
Show Gist options
  • Select an option

  • Save R44VC0RP/90e893398c906f956ed1615cb1bc7f81 to your computer and use it in GitHub Desktop.

Select an option

Save R44VC0RP/90e893398c906f956ed1615cb1bc7f81 to your computer and use it in GitHub Desktop.
(function () {
// 1. Intercept calls to URL.createObjectURL to capture the Blob
const originalCreateObjectURL = URL.createObjectURL;
URL.createObjectURL = function (blob) {
// Store the blob in a global variable
window.lastCapturedBlob = blob;
console.log("Captured blob:", blob);
return originalCreateObjectURL.apply(this, arguments);
};
// 2. Locate the existing STL button by its aria-label
const stlButton = document.querySelector('button[aria-label="download STL file"]');
if (!stlButton) {
console.error("STL download button not found.");
return;
}
// 3. Create a new "3D Print" button with the same styling
const printButton = stlButton.cloneNode(true);
// Create and append the logo image
const logo = document.createElement('img');
logo.src = "https://0o4pg1fpby.ufs.sh/f/RSbfEU0J8DcdJpd0Kp53YcMCbI7gKOhHekyato51XAsQxF68";
logo.style.height = "16px";
logo.style.width = "16px";
logo.style.marginRight = "8px";
logo.style.objectFit = "contain";
// Create text span
const textSpan = document.createElement('span');
textSpan.textContent = "3D Print via Mandarin 3D";
// Apply the new styles
printButton.style.padding = "0.75rem 1.5rem";
printButton.style.backgroundColor = "#0D939B";
printButton.style.color = "white";
printButton.style.borderRadius = "9999px";
printButton.style.transition = "all 300ms";
printButton.style.display = "flex";
printButton.style.alignItems = "center";
printButton.style.cursor = "pointer";
printButton.style.border = "none";
// Clear existing content and add new elements
printButton.textContent = "";
printButton.appendChild(logo);
printButton.appendChild(textSpan);
// Add hover effect
printButton.addEventListener('mouseenter', () => {
printButton.style.backgroundColor = "#0B7F86";
});
printButton.addEventListener('mouseleave', () => {
printButton.style.backgroundColor = "#0D939B";
});
// 4. Add a click handler that posts the blob and opens the response URL in a new window
printButton.onclick = async function () {
if (!window.lastCapturedBlob) {
alert("No blob captured yet. Click the 'Download STL' button first.");
return;
}
try {
const formData = new FormData();
formData.append("file", window.lastCapturedBlob, "model.stl");
formData.append("external_source", "adamcad");
const res = await fetch("https://backend.mandarin3d.com/api/submit-remote", {
method: "POST",
body: formData,
});
if (!res.ok) {
throw new Error("Network response was not ok");
}
const data = await res.json();
console.log("Upload response:", data);
if (data.url) {
window.open(data.url, "_blank");
} else {
console.error("No URL found in response.");
}
} catch (err) {
console.error("Error submitting file:", err);
}
};
// 5. Insert the new button right after the STL button
stlButton.parentNode.insertBefore(printButton, stlButton.nextSibling);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment