User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
Undo Tag: Undo |
||
| Line 1: | Line 1: | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
const urlCache = new Map(); | const urlCache = new Map(); | ||
| Line 51: | Line 11: | ||
const match = className.match(/model_size_(\d+)/); | const match = className.match(/model_size_(\d+)/); | ||
return match ? parseInt(match[1], 10) : 500; | |||
} | } | ||
function getFileUrl(fileName) { | function getFileUrl(fileName) { | ||
if (urlCache.has(fileName)) { | if (urlCache.has(fileName)) { | ||
return urlCache.get(fileName); | return urlCache.get(fileName); | ||
} | } | ||
| Line 76: | Line 26: | ||
'&iiprop=url' + | '&iiprop=url' + | ||
'&format=json'; | '&format=json'; | ||
const promise = fetch(apiUrl) | const promise = fetch(apiUrl) | ||
.then(function (response) { | .then(function (response) { | ||
if (!response.ok) { | if (!response.ok) { | ||
throw new Error( | throw new Error('API request failed'); | ||
} | } | ||
| Line 100: | Line 36: | ||
}) | }) | ||
.then(function (data) { | .then(function (data) { | ||
const pages = data.query && data.query.pages; | const pages = data.query && data.query.pages; | ||
if (!pages) { | if (!pages) { | ||
return null; | return null; | ||
} | } | ||
| Line 111: | Line 44: | ||
const page = Object.values(pages)[0]; | const page = Object.values(pages)[0]; | ||
if (!page | if (!page || !page.imageinfo || !page.imageinfo[0]) { | ||
return null; | return null; | ||
} | } | ||
return page.imageinfo[0].url; | |||
}) | }) | ||
.catch(function ( | .catch(function () { | ||
return null; | return null; | ||
}); | }); | ||
| Line 138: | Line 60: | ||
function loadModel(preview) { | function loadModel(preview) { | ||
if (preview.dataset.loaded) { | if (preview.dataset.loaded) { | ||
return; | return; | ||
} | } | ||
| Line 159: | Line 69: | ||
if (!fileName) { | if (!fileName) { | ||
return; | return; | ||
} | } | ||
| Line 165: | Line 74: | ||
const displayName = preview.dataset.name || fileName; | const displayName = preview.dataset.name || fileName; | ||
const size = getSize(preview); | const size = getSize(preview); | ||
getFileUrl(fileName + '.glb') | getFileUrl(fileName + '.glb') | ||
.then(function (url) { | .then(function (url) { | ||
if (!url || !preview.isConnected) { | |||
if (!url | |||
return; | return; | ||
} | } | ||
const wrap = document.createElement('div'); | const wrap = document.createElement('div'); | ||
| Line 216: | Line 94: | ||
viewer.setAttribute('camera-controls', ''); | viewer.setAttribute('camera-controls', ''); | ||
viewer.setAttribute('touch-action', 'pan-y'); | viewer.setAttribute('touch-action', 'pan-y'); | ||
container.appendChild(viewer); | container.appendChild(viewer); | ||
| Line 240: | Line 103: | ||
wrap.appendChild(name); | wrap.appendChild(name); | ||
preview.replaceWith(wrap); | preview.replaceWith(wrap); | ||
}); | }); | ||
} | } | ||
function initPreview(preview) { | function initPreview(preview) { | ||
if ( | if ( | ||
!preview || | |||
preview.dataset.initialized | preview.dataset.initialized | ||
) { | |||
return; | return; | ||
} | } | ||
preview.dataset.initialized = '1'; | preview.dataset.initialized = '1'; | ||
const button = document.createElement('button'); | const button = document.createElement('button'); | ||
| Line 297: | Line 122: | ||
button.className = 'model-load-button'; | button.className = 'model-load-button'; | ||
button.type = 'button'; | button.type = 'button'; | ||
button.addEventListener('click', function () { | button.addEventListener('click', function () { | ||
loadModel(preview); | loadModel(preview); | ||
}); | }); | ||
preview.appendChild(button); | preview.appendChild(button); | ||
} | } | ||
function scan(root) { | function scan(root) { | ||
if (!root) { | if (!root) { | ||
return; | return; | ||
} | } | ||
if ( | if ( | ||
| Line 344: | Line 139: | ||
root.matches('.model-placeholder') | root.matches('.model-placeholder') | ||
) { | ) { | ||
initPreview(root); | initPreview(root); | ||
} | } | ||
if (root.querySelectorAll) { | if (root.querySelectorAll) { | ||
root | |||
.querySelectorAll('.model-placeholder') | |||
.forEach(initPreview); | |||
} | } | ||
} | } | ||
function startObserver() { | function startObserver() { | ||
if (!document.body) { | if (!document.body) { | ||
return; | return; | ||
} | } | ||
const observer = new MutationObserver(function (mutations) { | const observer = new MutationObserver(function (mutations) { | ||
for (const mutation of mutations) { | |||
for (const node of mutation.addedNodes) { | |||
if (node.nodeType === Node.ELEMENT_NODE) { | |||
scan(node); | |||
} | |||
} | } | ||
} | |||
} | |||
}); | }); | ||
| Line 440: | Line 168: | ||
subtree: true | subtree: true | ||
}); | }); | ||
} | } | ||
function init() { | function init() { | ||
scan(document); | scan(document); | ||
startObserver(); | startObserver(); | ||
} | } | ||
mw.hook('wikipage.content').add(function ($content) { | |||
scan($content[0]); | |||
}); | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', init, { | |||
once: true | |||
}); | }); | ||
} else { | } else { | ||
init(); | init(); | ||
} | } | ||