User:Monster Domosed/common.jsGive feedback
Jump to navigation
Jump to search
Note: After publishing, you may need to reload with your cache disabled to see your changes.
- Windows / Linux: Press Ctrl-Shift-R
- macOS: Press ⌘-Shift-R
(function () {
'use strict';
const LQ_ICON = '/images/d/d8/LQ_icon.png';
const HQ_ICON = '/images/1/10/HQ_icon.png';
function getSize(element) {
const match = element.className.match(/model_size_(\d+)/);
return match ? parseInt(match[1], 10) : 500;
}
function getFileUrl(fileName) {
const apiUrl =
mw.config.get('wgScriptPath') +
'/api.php?action=query' +
'&titles=File:' + encodeURIComponent(fileName) +
'&prop=imageinfo' +
'&iiprop=url' +
'&format=json';
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
const page = Object.values(data.query.pages)[0];
if (!page.imageinfo) {
return null;
}
return page.imageinfo[0].url;
});
}
function addQualityButton(container, viewer, lqUrl, hqUrl) {
if (!hqUrl || lqUrl === hqUrl) {
return;
}
const button = document.createElement('button');
button.className = 'mv-quality-toggle';
button.type = 'button';
const icon = document.createElement('img');
icon.src = LQ_ICON;
icon.alt = '';
button.appendChild(icon);
const buttonSize = Math.max(
28,
Math.min(56, Math.round(container.offsetWidth * 0.14))
);
button.style.width = buttonSize + 'px';
button.style.height = buttonSize + 'px';
button.addEventListener('click', function () {
const isLQ = viewer.src === lqUrl;
viewer.src = isLQ ? hqUrl : lqUrl;
icon.src = isLQ ? HQ_ICON : LQ_ICON;
});
container.appendChild(button);
}
function loadModel(preview) {
if (preview.dataset.loaded) {
return;
}
preview.dataset.loaded = '1';
const fileName = preview.dataset.model;
const size = getSize(preview);
Promise.all([
getFileUrl(fileName + '-compressed.glb'),
getFileUrl(fileName + '.glb')
]).then(function (urls) {
const lqUrl = urls[0] || urls[1];
const hqUrl = urls[1];
if (!lqUrl) {
return;
}
const container = document.createElement('div');
container.className = 'model-viewer-container';
container.style.width = size + 'px';
container.style.height = size + 'px';
const viewer = document.createElement('model-viewer');
viewer.src = lqUrl;
viewer.setAttribute('camera-controls', '');
viewer.setAttribute('touch-action', 'pan-y');
container.appendChild(viewer);
preview.replaceWith(container);
viewer.addEventListener('load', function () {
addQualityButton(
container,
viewer,
lqUrl,
hqUrl
);
});
});
}
function initPreview(preview) {
if (preview.dataset.initialized) {
return;
}
preview.dataset.initialized = '1';
const button = document.createElement('button');
button.className = 'model-load-button';
button.type = 'button';
button.addEventListener('click', function () {
loadModel(preview);
});
preview.appendChild(button);
}
function scan() {
document
.querySelectorAll('.model-preview')
.forEach(initPreview);
}
scan();
new MutationObserver(scan).observe(document.body, {
childList: true,
subtree: true
});
})();
function syncWrapperSize(viewer) {
const wrapper = viewer.closest('.model-preview');
if (!wrapper) {
return;
}
const resizeObserver = new ResizeObserver(function () {
wrapper.style.width = viewer.offsetWidth + 'px';
wrapper.style.height = viewer.offsetHeight + 'px';
});
resizeObserver.observe(viewer);
}
const personal = document.querySelector(".vector-menu-content-list");
if (personal){
const test2 = document.createElement("li");
const test3 = document.createElement("li");
const test4 = document.createElement("li");
test2.innerHTML = `<a href="/User:Monster_Domosed/common.js">JS</a>`;
test3.innerHTML = `<a href="/User:Monster_Domosed/common.css">CSS</a>`;
test4.innerHTML = `<a href="/User:Monster_Domosed/Sandbox">SB</a>`;
personal.prepend(test2, test3, test4);
}