VOOZH about

URL: https://minecraft.wiki/w/MediaWiki:Gadget-mcw-calc-loader.js

⇱ MediaWiki:Gadget-mcw-calc-loader.js – Minecraft Wiki


MediaWiki:Gadget-mcw-calc-loader.js

From Minecraft Wiki
Jump to navigation Jump to search
In other languages

Note: After saving, you have to bypass your browser's cache to see the changes.

Google Chrome, Firefox, Microsoft Edge, and Safari: Hold down the key and click the Reload toolbar button.
For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/// <reference types="types-mediawiki" />
/* global mw */
mw.hook('wikipage.content').add(()=>{
constcalcs=document.querySelectorAll('div.mcw-calc')
calcs.forEach((calc)=>{
consttype=calc.getAttribute('data-type')
if(!type){
console.error('No data-type attribute found on .mcw-calc',calc)
return
}
constiframe=document.createElement('iframe')
// a random string to use as id
constid=Math.random().toString(36).substring(7)
iframe.id=`mcw-calc-${id}`
constattributes=calc.attributes
for(leti=0;i<attributes.length;i++){
iframe.setAttribute(attributes[i].name,attributes[i].value)
}
letlocal=mw.config.get('wgContentLanguage')
if(local==='zh'){
local=
mw.config.get('wgUserVariant')||
newURL(window.location.href).searchParams.get('variant')||
mw.user.options.get('variant')
}
consturl=`/tools/${type}/#?id=${id}&locale=${local}&url=${encodeURIComponent(window.location.href)}`
if(localStorage.getItem('mcwCalcLocal')==='true'){
console.log('You are in development environment and tools are loaded from localhost.')
iframe.src=`http://localhost:5173${url}`
}else{
iframe.src=`https://tools.minecraft.wiki/static${url}`
}
// copy all children with .mcw-calc-parameter
constparameters=calc.querySelectorAll('.mcw-calc-parameter')
parameters.forEach((parameter)=>{
constiframeParameter=document.createElement('div')
iframeParameter.className='mcw-calc-parameter'
iframeParameter.innerHTML=parameter.innerHTML
iframe.appendChild(iframeParameter)
})
// inherit all styles, we'll set our own later
iframe.style=calc.style
calc.replaceWith(iframe)
iframe.style.border='none'
iframe.style.display='block'
iframe.style.width='100%'
iframe.style.colorScheme='auto'
iframe.style.maxWidth={
lifeviewer:'600px',
blockDistribution:'640px',
interactiveMap:'900px',
chunkbase:'900px',
}[type]
iframe.allow=
"accelerometer 'src'; clipboard-write 'src'; encrypted-media 'src'; fullscreen 'src'; picture-in-picture 'src'; autoplay 'src'"
iframe.allowFullscreen=true
if(calcs.length>5){
iframe.loading='lazy'
}
constdataset={}
Object.entries(calc.dataset).forEach((entry)=>{
constkey=entry[0]
constvalue=entry[1]
dataset[key]=value
})
mw.hook('wgl.themeChanged').add((theme)=>{
iframe.contentWindow.postMessage(
{
type:'mcw-calc-theme-change',
data:{
theme,
},
},
newURL(iframe.src).origin
)
})
window.addEventListener('message',(event)=>{
if(event.origin!==newURL(iframe.src).origin)return
if(event.data.id!==id)return
if(event.data.type==='mcw-calc-init-request-data'){
event.source.postMessage(
{
type:'mcw-calc-init',
data:{
dataset,
innerHTML:calc.innerHTML,
},
},
newURL(iframe.src).origin
)
}elseif(event.data.type==='mcw-calc-init-request-styles'){
event.source.postMessage(
{
type:'mcw-calc-theme-change',
data:{
theme:document.body.classList.contains('wgl-theme-light')?'light':'dark',
},
},
newURL(iframe.src).origin
)
}elseif(event.data.type==='mcw-calc-height-change'){
iframe.style.height=`${event.data.data.height}px`
}elseif(event.data.type==='mcw-calc-clipboard'){
navigator.clipboard.writeText(event.data.data.text)
}
})
})
})
Retrieved from "https://minecraft.wiki/w/MediaWiki:Gadget-mcw-calc-loader.js?oldid=3467710"

Navigation menu