Update button download A developer published a userscript, "Meshy Model Downloader" (v1.2.0), that intercepts and captures decrypted GLB model files on meshy.ai and exports them as GLB, STL, or OBJ. The script parses GLB binary chunks and caches captured models by task ID and URL in localStorage, exposing a debug state object for inspection. | | // ==UserScript== | | | // @name Meshy Model Downloader | | | // @namespace https://kirakira.cloud | | | // @version 1.2.0 | | | // @description Meshy model downloader – captura GLB desencriptado + export GLB/STL/OBJ | | | // @author kirakira updated | | | // @match https://www.meshy.ai/ | | | // @run-at document-start | | | // @grant none | | | // ==/UserScript== | | | function { | | | 'use strict'; | | | if window. meshyEasyDownloadTMInstalled return; | | | window. meshyEasyDownloadTMInstalled = true; | | | pageScript ; | | | function pageScript { | | | 'use strict'; | | | const DEBUG MODE = true; | | | const MODEL CACHE KEY = 'meshy easy download models v3'; | | | const MAX MODEL CACHE = 100; | | | const capturedGlbByTask = new Map ; | | | const capturedGlbByUrl = new Map ; | | | const modelUrlByTask = new Map ; | | | const captureSignatures = new Set ; | | | let lastCapturedGlb = null; | | | let lastCapturedTaskId = null; | | | let lastSeenTaskId = null; | | | const CHUNK TYPE JSON = 0x4e4f534a; | | | const CHUNK TYPE BIN = 0x004e4942; | | | const GLB MAGIC = 0x46546c67; | | | const COMPONENT TYPE BYTES = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 }; | | | const TYPE COMPONENTS = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 }; | | | const ARRAY CTORS = { 5120: Int8Array, 5121: Uint8Array, 5122: Int16Array, 5123: Uint16Array, 5125: Uint32Array, 5126: Float32Array }; | | | function log ...args { | | | if DEBUG MODE console.log ' meshy-tm ', ...args ; | | | } | | | function publishDebugState { | | | try { | | | window. meshyEasyDownloadState = { | | | lastSeenTaskId, | | | lastCapturedTaskId, | | | capturedTaskCount: capturedGlbByTask.size, | | | capturedModelUrlCount: capturedGlbByUrl.size, | | | lastCapturedSize: lastCapturedGlb ? lastCapturedGlb.byteLength : 0 | | | }; | | | } catch {} | | | } | | | function slugify text { | | | return text \|\| 'model' | | | .replace / ^a-zA-Z0-9\s /g, '' | | | .replace /\s+/g, '-' | | | .replace /^-+\|-+$/g, '' | | | .substring 0, 60 \|\| 'model'; | | | } | | | function formatSize bytes { | | | if bytes \|\| bytes <= 0 return ''; | | | if bytes = 1024 1024 return ${ bytes / 1024 1024 .toFixed 1 }MB ; | | | if bytes = 1024 return ${Math.round bytes / 1024 }KB ; | | | return ${bytes}B ; | | | } | | | function extractTaskIdFromUrl path = window.location.pathname { | | | const match = path.match /\/workspace\/ a-f0-9- + /i \|\| path.match /\/3d-models\/ ^/ - a-f0-9- {36} /i ; | | | return match ? match 1 : null; | | | } | | | function currentTaskId { | | | return extractTaskIdFromUrl ; | | | } | | | function normalizeTaskId taskId { | | | return taskId \|\| currentTaskId \|\| lastSeenTaskId \|\| null; | | | } | | | function canonicalModelUrl url { | | | if url return ''; | | | try { | | | const parsed = new URL url, window.location.origin ; | | | return ${parsed.origin}${parsed.pathname} ; | | | } catch { | | | return url; | | | } | | | } | | | function loadModels { | | | try { | | | const raw = localStorage.getItem MODEL CACHE KEY ; | | | if raw return {}; | | | const parsed = JSON.parse raw ; | | | return parsed && typeof parsed === 'object' ? parsed : {}; | | | } catch e { | | | log 'model cache parse failed:', e.message ; | | | return {}; | | | } | | | } | | | function saveModels models { | | | try { | | | localStorage.setItem MODEL CACHE KEY, JSON.stringify models ; | | | } catch e { | | | log 'model cache save failed:', e.message ; | | | } | | | } | | | function getModel taskId { | | | if taskId return null; | | | return loadModels taskId \|\| null; | | | } | | | function saveModel taskId, model { | | | if taskId \|\| model return; | | | const models = loadModels ; | | | models taskId = { ...model, ts: Date.now }; | | | const keys = Object.keys models ; | | | if keys.length MAX MODEL CACHE { | | | keys | | | .sort a, b = models a ?.ts \|\| 0 - models b ?.ts \|\| 0 | | | .slice 0, keys.length - MAX MODEL CACHE | | | .forEach k = delete models k ; | | | } | | | saveModels models ; | | | } | | | async function fetchSizeText url { | | | if url return ''; | | | try { | | | const res = await fetch url, { method: 'HEAD' } ; | | | const size = Number.parseInt res.headers.get 'content-length' \|\| '0', 10 ; | | | return formatSize size ; | | | } catch { | | | return ''; | | | } | | | } | | | function isGLBBuffer buffer { | | | if buffer instanceof ArrayBuffer \|\| buffer.byteLength < 12 return false; | | | try { | | | return new DataView buffer .getUint32 0, true === GLB MAGIC; | | | } catch { | | | return false; | | | } | | | } | | | function isMeshyEncryptedBuffer buffer { | | | if buffer instanceof ArrayBuffer \|\| buffer.byteLength < 8 return false; | | | try { | | | return new TextDecoder .decode new Uint8Array buffer, 0, 8 .startsWith 'MESHY.AI' ; | | | } catch { | | | return false; | | | } | | | } | | | function glbSignature buffer { | | | const head = Array.from new Uint8Array buffer, 0, Math.min 16, buffer.byteLength ; | | | return ${buffer.byteLength}:${head.join ',' } ; | | | } | | | function arrayBufferFromUnknown value { | | | if value instanceof ArrayBuffer return value.slice 0 ; | | | if ArrayBuffer.isView value { | | | return value.buffer.slice value.byteOffset, value.byteOffset + value.byteLength ; | | | } | | | return null; | | | } | | | function cacheGlbBuffer taskId, name, buffer, source { | | | if isGLBBuffer buffer return; | | | const normalizedTaskId = normalizeTaskId taskId ; | | | const signature = glbSignature buffer ; | | | if captureSignatures.has signature return; | | | captureSignatures.add signature ; | | | const cloned = buffer.slice 0 ; | | | lastCapturedGlb = cloned; | | | lastCapturedTaskId = normalizedTaskId; | | | if normalizedTaskId capturedGlbByTask.set normalizedTaskId, cloned ; | | | const knownUrl = normalizedTaskId ? modelUrlByTask.get normalizedTaskId : ''; | | | if knownUrl capturedGlbByUrl.set canonicalModelUrl knownUrl , cloned ; | | | log captured decrypted GLB from ${source} , normalizedTaskId ? task ${normalizedTaskId} : ' task unknown ', formatSize cloned.byteLength ; | | | if normalizedTaskId { | | | const existing = getModel normalizedTaskId \|\| {}; | | | saveModel normalizedTaskId, { | | | url: existing.url \|\| '', | | | name: name \|\| existing.name \|\| 'model', | | | size: existing.size \|\| formatSize cloned.byteLength | | | } ; | | | } | | | updateFloatingPanel ; | | | publishDebugState ; | | | } | | | function downloadBuffer buffer, filename, mimeType = 'application/octet-stream' { | | | const blob = new Blob buffer , { type: mimeType } ; | | | const url = URL.createObjectURL blob ; | | | const a = document.createElement 'a' ; | | | a.href = url; | | | a.download = filename; | | | a.click ; | | | setTimeout = URL.revokeObjectURL url , 1500 ; | | | } | | | function downloadText text, filename { | | | downloadBuffer new TextEncoder .encode text , filename, 'text/plain;charset=utf-8' ; | | | } | | | // ---------- GLB → STL / OBJ mismo código que tenías ---------- | | | function readComponent view, offset, componentType { | | | switch componentType { | | | case 5120: return view.getInt8 offset ; | | | case 5121: return view.getUint8 offset ; | | | case 5122: return view.getInt16 offset, true ; | | | case 5123: return view.getUint16 offset, true ; | | | case 5125: return view.getUint32 offset, true ; | | | case 5126: return view.getFloat32 offset, true ; | | | default: throw new Error unsupported component type ${componentType} ; | | | } | | | } | | | function parseGLB buffer { | | | const view = new DataView buffer ; | | | if view.byteLength < 20 throw new Error 'invalid GLB: too small' ; | | | if view.getUint32 0, true == GLB MAGIC throw new Error 'invalid GLB: bad magic' ; | | | if view.getUint32 4, true == 2 throw new Error 'unsupported GLB version' ; | | | const declaredLength = view.getUint32 8, true ; | | | if declaredLength buffer.byteLength throw new Error 'invalid GLB: truncated' ; | | | let offset = 12, json = null, bin = null; | | | while offset + 8 <= declaredLength { | | | const chunkLength = view.getUint32 offset, true ; | | | const chunkType = view.getUint32 offset + 4, true ; | | | offset += 8; | | | if offset + chunkLength buffer.byteLength throw new Error 'invalid GLB: bad chunk size' ; | | | if chunkType === CHUNK TYPE JSON { | | | const jsonText = new TextDecoder .decode new Uint8Array buffer, offset, chunkLength .replace /\u0000+$/g, '' ; | | | json = JSON.parse jsonText ; | | | } else if chunkType === CHUNK TYPE BIN { | | | bin = new Uint8Array buffer, offset, chunkLength ; | | | } | | | offset += chunkLength; | | | } | | | if json throw new Error 'invalid GLB: missing JSON chunk' ; | | | if bin bin = new Uint8Array ; | | | return { json, bin }; | | | } | | | function readAccessor json, bin, accessorIndex { | | | const accessor = json.accessors?. accessorIndex ; | | | if accessor throw new Error missing accessor ${accessorIndex} ; | | | if accessor.sparse throw new Error 'sparse accessors are not supported' ; | | | const bufferView = json.bufferViews?. accessor.bufferView ; | | | if bufferView throw new Error missing bufferView for accessor ${accessorIndex} ; | | | if bufferView.buffer \|\| 0 == 0 throw new Error 'external buffers are not supported' ; | | | const componentType = accessor.componentType; | | | const componentBytes = COMPONENT TYPE BYTES componentType ; | | | const components = TYPE COMPONENTS accessor.type ; | | | const ctor = ARRAY CTORS componentType ; | | | if componentBytes \|\| components \|\| ctor throw new Error unsupported accessor format ; | | | const count = accessor.count; | | | const packedStride = componentBytes components; | | | const byteStride = bufferView.byteStride \|\| packedStride; | | | const byteOffset = bufferView.byteOffset \|\| 0 + accessor.byteOffset \|\| 0 ; | | | const out = new ctor count components ; | | | if byteStride === packedStride { | | | const src = new ctor bin.buffer, bin.byteOffset + byteOffset, count components ; | | | out.set src ; | | | return out; | | | } | | | const dv = new DataView bin.buffer, bin.byteOffset + byteOffset, count byteStride ; | | | for let i = 0; i < count; i++ { | | | const rowOffset = i byteStride; | | | for let c = 0; c < components; c++ { | | | out i components + c = readComponent dv, rowOffset + c componentBytes, componentType ; | | | } | | | } | | | return out; | | | } | | | function triangulate indices, mode { | | | const src = Array.from indices ; | | | if mode === 4 \|\| mode === undefined { | | | if src.length % 3 == 0 throw new Error 'triangle index count is not divisible by 3' ; | | | return src; | | | } | | | const out = ; | | | if mode === 5 { | | | for let i = 0; i < src.length - 2; i++ { | | | const a = src i , b = src i + 1 , c = src i + 2 ; | | | if a === b \|\| b === c \|\| a === c continue; | | | if i % 2 === 0 out.push a, b, c ; | | | else out.push b, a, c ; | | | } | | | return out; | | | } | | | if mode === 6 { | | | for let i = 1; i < src.length - 1; i++ { | | | const a = src 0 , b = src i , c = src i + 1 ; | | | if a === b \|\| b === c \|\| a === c continue; | | | out.push a, b, c ; | | | } | | | return out; | | | } | | | throw new Error unsupported primitive mode ${mode} ; | | | } | | | function primitiveIndices json, bin, primitive, vertexCount { | | | if primitive.indices === undefined return Array.from { length: vertexCount }, , i = i ; | | | const idx = readAccessor json, bin, primitive.indices ; | | | return Array.from idx, n = Number n ; | | | } | | | function collectMeshPrimitives json, bin { | | | const out = ; | | | for const mesh of json.meshes \|\| { | | | for const primitive of mesh.primitives \|\| { | | | const positionAccessor = primitive.attributes?.POSITION; | | | if positionAccessor === undefined continue; | | | const positions = readAccessor json, bin, positionAccessor ; | | | const vertexCount = json.accessors positionAccessor .count; | | | let normals = null; | | | if primitive.attributes?.NORMAL == undefined { | | | normals = readAccessor json, bin, primitive.attributes.NORMAL ; | | | if normals.length / 3 == vertexCount normals = null; | | | } | | | let uvs = null; | | | if primitive.attributes?.TEXCOORD 0 == undefined { | | | uvs = readAccessor json, bin, primitive.attributes.TEXCOORD 0 ; | | | if uvs.length / 2 == vertexCount uvs = null; | | | } | | | const mode = primitive.mode === undefined ? 4 : primitive.mode; | | | const idx = primitiveIndices json, bin, primitive, vertexCount ; | | | const triangles = triangulate idx, mode ; | | | out.push { positions, normals, uvs, triangles, vertexCount } ; | | | } | | | } | | | if out.length === 0 throw new Error 'no mesh primitives found' ; | | | return out; | | | } | | | function calculateNormal v0, v1, v2 { | | | const ax = v1 0 - v0 0 , ay = v1 1 - v0 1 , az = v1 2 - v0 2 ; | | | const bx = v2 0 - v0 0 , by = v2 1 - v0 1 , bz = v2 2 - v0 2 ; | | | const nx = ay bz - az by, ny = az bx - ax bz, nz = ax by - ay bx; | | | const len = Math.sqrt nx nx + ny ny + nz nz \|\| 1; | | | return nx / len, ny / len, nz / len ; | | | } | | | function glbToStlBuffer buffer { | | | const { json, bin } = parseGLB buffer ; | | | const primitives = collectMeshPrimitives json, bin ; | | | let triangleCount = 0; | | | for const prim of primitives triangleCount += prim.triangles.length / 3; | | | const out = new ArrayBuffer 84 + triangleCount 50 ; | | | const view = new DataView out ; | | | const header = new Uint8Array out, 0, 80 ; | | | const headerText = 'binary stl - meshy easy download'; | | | for let i = 0; i < headerText.length && i < 80; i++ header i = headerText.charCodeAt i ; | | | view.setUint32 80, triangleCount, true ; | | | let offset = 84; | | | for const prim of primitives { | | | const pos = prim.positions; | | | for let i = 0; i < prim.triangles.length; i += 3 { | | | const a = prim.triangles i 3, b = prim.triangles i + 1 3, c = prim.triangles i + 2 3; | | | const v0 = pos a , pos a + 1 , pos a + 2 ; | | | const v1 = pos b , pos b + 1 , pos b + 2 ; | | | const v2 = pos c , pos c + 1 , pos c + 2 ; | | | const normal = calculateNormal v0, v1, v2 ; | | | view.setFloat32 offset, normal 0 , true ; offset += 4; | | | view.setFloat32 offset, normal 1 , true ; offset += 4; | | | view.setFloat32 offset, normal 2 , true ; offset += 4; | | | for const v of v0, v1, v2 { | | | view.setFloat32 offset, v 0 , true ; offset += 4; | | | view.setFloat32 offset, v 1 , true ; offset += 4; | | | view.setFloat32 offset, v 2 , true ; offset += 4; | | | } | | | view.setUint16 offset, 0, true ; offset += 2; | | | } | | | } | | | return out; | | | } | | | function glbToObjText buffer, objectName { | | | const { json, bin } = parseGLB buffer ; | | | const primitives = collectMeshPrimitives json, bin ; | | | const lines = ' generated by Meshy Easy Download', o ${slugify objectName \|\| 'model' } ; | | | let vertexOffset = 0, uvOffset = 0, normalOffset = 0; | | | for const prim of primitives { | | | const positions = prim.positions, uvs = prim.uvs, normals = prim.normals; | | | const hasUV = Boolean uvs , hasNormal = Boolean normals ; | | | for let i = 0; i < prim.vertexCount; i++ { | | | const p = i 3; | | | lines.push v ${positions p } ${positions p + 1 } ${positions p + 2 } ; | | | } | | | if hasUV { | | | for let i = 0; i < prim.vertexCount; i++ { | | | const t = i 2; | | | lines.push vt ${uvs t } ${1 - uvs t + 1 } ; | | | } | | | } | | | if hasNormal { | | | for let i = 0; i < prim.vertexCount; i++ { | | | const n = i 3; | | | lines.push vn ${normals n } ${normals n + 1 } ${normals n + 2 } ; | | | } | | | } | | | for let i = 0; i < prim.triangles.length; i += 3 { | | | const a = prim.triangles i + 1, b = prim.triangles i + 1 + 1, c = prim.triangles i + 2 + 1; | | | const va = vertexOffset + a, vb = vertexOffset + b, vc = vertexOffset + c; | | | if hasUV && hasNormal { | | | lines.push f ${va}/${uvOffset + a}/${normalOffset + a} ${vb}/${uvOffset + b}/${normalOffset + b} ${vc}/${uvOffset + c}/${normalOffset + c} ; | | | } else if hasUV { | | | lines.push f ${va}/${uvOffset + a} ${vb}/${uvOffset + b} ${vc}/${uvOffset + c} ; | | | } else if hasNormal { | | | lines.push f ${va}//${normalOffset + a} ${vb}//${normalOffset + b} ${vc}//${normalOffset + c} ; | | | } else { | | | lines.push f ${va} ${vb} ${vc} ; | | | } | | | } | | | vertexOffset += prim.vertexCount; | | | if hasUV uvOffset += prim.vertexCount; | | | if hasNormal normalOffset += prim.vertexCount; | | | } | | | lines.push '' ; | | | return lines.join '\n' ; | | | } | | | function cloneIfGlb buffer { | | | return buffer && isGLBBuffer buffer ? buffer.slice 0 : null; | | | } | | | function fallbackCapturedGlb taskId, modelUrl { | | | const candidates = ; | | | const normalizedTaskId = normalizeTaskId taskId ; | | | if taskId candidates.push capturedGlbByTask.get taskId ; | | | if normalizedTaskId && normalizedTaskId == taskId candidates.push capturedGlbByTask.get normalizedTaskId ; | | | if modelUrl candidates.push capturedGlbByUrl.get canonicalModelUrl modelUrl ; | | | if lastCapturedTaskId && normalizedTaskId && lastCapturedTaskId === normalizedTaskId candidates.push lastCapturedGlb ; | | | candidates.push lastCapturedGlb ; | | | if capturedGlbByTask.size === 1 candidates.push capturedGlbByTask.values .next .value ; | | | for const buffer of candidates { | | | const cloned = cloneIfGlb buffer ; | | | if cloned return cloned; | | | } | | | return null; | | | } | | | async function resolveGLBBuffer taskId, modelUrl { | | | const fallback = fallbackCapturedGlb taskId, modelUrl ; | | | if fallback return fallback; | | | if modelUrl throw new Error 'No model URL yet – open the model preview and wait 2-3s' ; | | | const res = await fetch modelUrl ; | | | const buffer = await res.arrayBuffer ; | | | if isGLBBuffer buffer return buffer; | | | if isMeshyEncryptedBuffer buffer { | | | throw new Error 'Encrypted model – open the 3D preview, wait a couple seconds, then retry' ; | | | } | | | throw new Error 'Downloaded data is not a valid GLB' ; | | | } | | | async function downloadAs format, taskId, model, filename { | | | try { | | | const glbBuffer = await resolveGLBBuffer taskId, model?.url ; | | | if format === 'glb' { | | | downloadBuffer glbBuffer, filename, 'model/gltf-binary' ; | | | return; | | | } | | | if format === 'stl' { | | | downloadBuffer glbToStlBuffer glbBuffer , filename, 'model/stl' ; | | | return; | | | } | | | if format === 'obj' { | | | downloadText glbToObjText glbBuffer, model?.name \|\| 'model' , filename ; | | | return; | | | } | | | } catch error { | | | log ${format} export failed: , error.message ; | | | alert ${format.toUpperCase } export failed:\n${error.message} ; | | | } | | | } | | | // ---------- Floating panel mucho más fiable que inyectar al lado del botón nativo ---------- | | | function ensureFloatingPanel { | | | let panel = document.getElementById 'meshy-tm-float' ; | | | if panel return panel; | | | panel = document.createElement 'div' ; | | | panel.id = 'meshy-tm-float'; | | | panel.style.cssText = | | | position: fixed; bottom: 24px; right: 24px; z-index: 99999; | | | background: 1a1a1e; border: 1px solid 3f3f46; border-radius: 12px; | | | padding: 12px 14px; box-shadow: 0 8px 32px rgba 0,0,0,0.45 ; | | | font-family: system-ui, -apple-system, sans-serif; color: e4e4e7; | | | min-width: 200px; display: none; | | | ; | | | panel.innerHTML = | | |