MediaWiki:Common.js: Difference between revisions
From Artifacts of Capitalism
No edit summary |
No edit summary |
||
| Line 8: | Line 8: | ||
} | } | ||
})(); | })(); | ||
// Hide Draft namespace checkbox (except for approvers) | |||
mw.hook('wikipage.content').add(function ($c) { | mw.hook('wikipage.content').add(function ($c) { | ||
var groups = mw.config.get('wgUserGroups') || []; | var groups = mw.config.get('wgUserGroups') || []; | ||
if (groups.indexOf('approver') !== -1) return; | if (groups.indexOf('approver') !== -1) return; | ||
$c.find('input[name="ns' + 3000 + '"]').closest('label, .mw-advancedSearch-ns_option').hide(); | $c.find('input[name="ns' + 3000 + '"]').closest('label, .mw-advancedSearch-ns_option').hide(); | ||
}); | }); | ||
// Load Google CSE only on Advanced_search page | // Load Google CSE only on Advanced_search page | ||
mw.loader.using('mediawiki.util').then(function () { | mw.loader.using('mediawiki.util').then(function () { | ||
| Line 21: | Line 23: | ||
console.log('[CSE] Loading on:', page); | console.log('[CSE] Loading on:', page); | ||
window.__gcse = { | window.__gcse = { | ||
searchCallbacks: { | searchCallbacks: { | ||
| Line 42: | Line 43: | ||
}; | }; | ||
var script = document.createElement('script'); | var script = document.createElement('script'); | ||
script.async = true; | script.async = true; | ||
| Line 54: | Line 54: | ||
document.body.appendChild(script); | document.body.appendChild(script); | ||
}); | }); | ||
// Open all external links in a new | |||
// Open all external links in a new tab | |||
$(document).ready(function() { | $(document).ready(function() { | ||
$('a.external').attr('target', '_blank'); | |||
}); | }); | ||
// | |||
// Add fields to CreateAccount form | |||
mw.loader.using('mediawiki.util').then(function () { | mw.loader.using('mediawiki.util').then(function () { | ||
if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return; | |||
const observer = new MutationObserver(() => { | |||
const realNameInput = document.getElementById('wpRealName'); | |||
if (!realNameInput) return; | |||
const form = realNameInput.closest('form'); | |||
if (!form) return; | |||
if (document.getElementById('wpAffiliation')) return; | |||
const affDiv = document.createElement('div'); | |||
affDiv.className = 'mw-input'; | |||
affDiv.innerHTML = ` | |||
<label for="wpAffiliation">Affiliation</label> | |||
<input type="text" id="wpAffiliation" name="wpAffiliation" | |||
required class="mw-ui-input form-control"/> | |||
`; | |||
const instDiv = document.createElement('div'); | |||
instDiv.className = 'mw-input'; | |||
instDiv.innerHTML = ` | |||
<label for="wpInstitutionalEmail">Institutional Email</label> | |||
<input type="email" id="wpInstitutionalEmail" name="wpInstitutionalEmail" | |||
required class="mw-ui-input form-control"/> | |||
`; | |||
const realNameDiv = | |||
realNameInput.closest('.mw-input, .form-group, p') || realNameInput.parentNode; | |||
realNameDiv.insertAdjacentElement('afterend', instDiv); | |||
realNameDiv.insertAdjacentElement('afterend', affDiv); | |||
observer.disconnect(); | |||
}); | |||
observer.observe(document.body, { childList: true, subtree: true }); | |||
}); | }); | ||
// === Artifact Network Map Connections === | |||
mw.hook('wikipage.content').add(function () { | |||
// Only run on your map page | |||
if (mw.config.get('wgPageName') !== 'Artifact_Map') return; | |||
console.log('[Artifact Map] Hook fired'); | |||
function waitForMap(callback) { | |||
var attempts = 0; | |||
var maxAttempts = 25; | |||
var interval = setInterval(function () { | |||
attempts++; | |||
if (window.mw && mw.leafletMap && mw.leafletMap.map) { | |||
clearInterval(interval); | |||
console.log('[Artifact Map] Leaflet ready'); | |||
callback(mw.leafletMap.map); | |||
} | |||
if (attempts > maxAttempts) { | |||
clearInterval(interval); | |||
console.log('[Artifact Map] Leaflet never loaded'); | |||
} | |||
}, 300); | |||
} | |||
waitForMap(function (map) { | |||
console.log( | var el = document.getElementById("artifact-network-data"); | ||
if (!el) { | |||
console.log('[Artifact Map] No Cargo JSON found'); | |||
return; | |||
} | |||
var raw = el.textContent.trim(); | |||
if (!raw) { | |||
console.log('[Artifact Map] Empty JSON'); | |||
return; | |||
} | |||
var data = JSON.parse(raw); | |||
console.log('[Artifact Map] Data loaded:', data); | |||
if ( | // Build lookup table | ||
var lookup = {}; | |||
data.forEach(function(item) { | |||
if (item.Title && item.Latitude && item.Longitude) { | |||
lookup[item.Title.trim()] = [ | |||
parseFloat(item.Latitude), | |||
parseFloat(item.Longitude) | |||
]; | |||
} | |||
}); | |||
console.log('[Artifact Map] | console.log('[Artifact Map] Lookup:', lookup); | ||
// | // Draw connections | ||
function | data.forEach(function(item) { | ||
if (!item.Related_artifacts) return; | |||
var fromCoords = lookup[item.Title.trim()]; | |||
if (!fromCoords) return; | |||
var relatedList = item.Related_artifacts.split(","); | |||
relatedList.forEach(function(rel) { | |||
var target = rel.trim(); | |||
var toCoords = lookup[target]; | |||
if (toCoords) { | |||
console.log('[Artifact Map] Drawing line:', item.Title, '→', target); | |||
L.polyline([fromCoords, toCoords], { | |||
color: "purple", | |||
weight: 3, | |||
opacity: 0.8, | |||
dashArray: "4,6" | |||
}).addTo(map); | |||
} | } | ||
}); | |||
}); | |||
console.log('[Artifact Map] Network loaded'); | |||
}); | |||
}); | }); | ||
Revision as of 02:52, 19 March 2026
// Ensure viewport meta exists (iOS layout fix)
(function () {
if (!document.querySelector('meta[name="viewport"]')) {
var m = document.createElement('meta');
m.name = 'viewport';
m.content = 'width=device-width, initial-scale=1, shrink-to-fit=no';
document.head.appendChild(m);
}
})();
// Hide Draft namespace checkbox (except for approvers)
mw.hook('wikipage.content').add(function ($c) {
var groups = mw.config.get('wgUserGroups') || [];
if (groups.indexOf('approver') !== -1) return;
$c.find('input[name="ns' + 3000 + '"]').closest('label, .mw-advancedSearch-ns_option').hide();
});
// Load Google CSE only on Advanced_search page
mw.loader.using('mediawiki.util').then(function () {
var page = mw.config.get('wgPageName');
if (page !== 'Artifacts_of_Capitalism:Advanced_search') return;
console.log('[CSE] Loading on:', page);
window.__gcse = {
searchCallbacks: {
ready: function () {
try {
var params = new URLSearchParams(window.location.search);
var q = params.get('q');
if (q) {
var hash = 'gsc.tab=0&gsc.q=' + encodeURIComponent(q);
if (!window.location.hash.includes('gsc.q=')) {
window.location.hash = hash;
console.log('[CSE] Applied query:', q);
}
}
} catch (err) {
console.warn('[CSE] Query error:', err);
}
}
}
};
var script = document.createElement('script');
script.async = true;
script.src = 'https://cse.google.com/cse.js?cx=b194aa5e4d64344ad';
script.onload = function () {
console.log('[CSE] Script loaded successfully');
};
script.onerror = function () {
console.error('[CSE] Failed to load cse.js');
};
document.body.appendChild(script);
});
// Open all external links in a new tab
$(document).ready(function() {
$('a.external').attr('target', '_blank');
});
// Add fields to CreateAccount form
mw.loader.using('mediawiki.util').then(function () {
if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;
const observer = new MutationObserver(() => {
const realNameInput = document.getElementById('wpRealName');
if (!realNameInput) return;
const form = realNameInput.closest('form');
if (!form) return;
if (document.getElementById('wpAffiliation')) return;
const affDiv = document.createElement('div');
affDiv.className = 'mw-input';
affDiv.innerHTML = `
<label for="wpAffiliation">Affiliation</label>
<input type="text" id="wpAffiliation" name="wpAffiliation"
required class="mw-ui-input form-control"/>
`;
const instDiv = document.createElement('div');
instDiv.className = 'mw-input';
instDiv.innerHTML = `
<label for="wpInstitutionalEmail">Institutional Email</label>
<input type="email" id="wpInstitutionalEmail" name="wpInstitutionalEmail"
required class="mw-ui-input form-control"/>
`;
const realNameDiv =
realNameInput.closest('.mw-input, .form-group, p') || realNameInput.parentNode;
realNameDiv.insertAdjacentElement('afterend', instDiv);
realNameDiv.insertAdjacentElement('afterend', affDiv);
observer.disconnect();
});
observer.observe(document.body, { childList: true, subtree: true });
});
// === Artifact Network Map Connections ===
mw.hook('wikipage.content').add(function () {
// Only run on your map page
if (mw.config.get('wgPageName') !== 'Artifact_Map') return;
console.log('[Artifact Map] Hook fired');
function waitForMap(callback) {
var attempts = 0;
var maxAttempts = 25;
var interval = setInterval(function () {
attempts++;
if (window.mw && mw.leafletMap && mw.leafletMap.map) {
clearInterval(interval);
console.log('[Artifact Map] Leaflet ready');
callback(mw.leafletMap.map);
}
if (attempts > maxAttempts) {
clearInterval(interval);
console.log('[Artifact Map] Leaflet never loaded');
}
}, 300);
}
waitForMap(function (map) {
var el = document.getElementById("artifact-network-data");
if (!el) {
console.log('[Artifact Map] No Cargo JSON found');
return;
}
var raw = el.textContent.trim();
if (!raw) {
console.log('[Artifact Map] Empty JSON');
return;
}
var data = JSON.parse(raw);
console.log('[Artifact Map] Data loaded:', data);
// Build lookup table
var lookup = {};
data.forEach(function(item) {
if (item.Title && item.Latitude && item.Longitude) {
lookup[item.Title.trim()] = [
parseFloat(item.Latitude),
parseFloat(item.Longitude)
];
}
});
console.log('[Artifact Map] Lookup:', lookup);
// Draw connections
data.forEach(function(item) {
if (!item.Related_artifacts) return;
var fromCoords = lookup[item.Title.trim()];
if (!fromCoords) return;
var relatedList = item.Related_artifacts.split(",");
relatedList.forEach(function(rel) {
var target = rel.trim();
var toCoords = lookup[target];
if (toCoords) {
console.log('[Artifact Map] Drawing line:', item.Title, '→', target);
L.polyline([fromCoords, toCoords], {
color: "purple",
weight: 3,
opacity: 0.8,
dashArray: "4,6"
}).addTo(map);
}
});
});
console.log('[Artifact Map] Network loaded');
});
});