MediaWiki:Common.js: Difference between revisions
Appearance
Auto-created: Network graph enhancements |
Fix: poll for vis.js before applying namespace colors |
||
| Line 1: | Line 1: | ||
/* Network Extension Enhancements - Filter external links, color by namespace */ | /* Network Extension Enhancements - Filter external links, color by namespace. | ||
* vis.js is loaded asynchronously by the Network extension, so we poll | |||
* until window.vis.DataSet is available before monkey-patching. | |||
*/ | |||
(function() { | (function() { | ||
'use strict'; | 'use strict'; | ||
var nsColors = { | var nsColors = { | ||
'Workflow': {bg: '#4CAF50', border: '#2E7D32', font: '#1B5E20'}, | 'Workflow': {bg: '#4CAF50', border: '#2E7D32', font: '#1B5E20'}, | ||
'Principle': {bg: '#2196F3', border: '#1565C0', font: '#0D47A1'}, | 'Principle': {bg: '#2196F3', border: '#1565C0', font: '#0D47A1'}, | ||
'Implementation': {bg: '#FF9800', border: '#EF6C00', font: '#E65100'}, | 'Implementation': {bg: '#FF9800', border: '#EF6C00', font: '#E65100'}, | ||
'Artifact': {bg: '#9C27B0', border: '#7B1FA2', font: '#4A148C'}, | 'Artifact': {bg: '#9C27B0', border: '#7B1FA2', font: '#4A148C'}, | ||
'Heuristic': {bg: '#F44336', border: '#C62828', font: '#B71C1C'}, | 'Heuristic': {bg: '#F44336', border: '#C62828', font: '#B71C1C'}, | ||
'Environment': {bg: '#00BCD4', border: '#0097A7', font: '#006064'}, | 'Environment': {bg: '#00BCD4', border: '#0097A7', font: '#006064'}, | ||
'Resource': {bg: '#795548', border: '#5D4037', font: '#3E2723'} | 'Resource': {bg: '#795548', border: '#5D4037', font: '#3E2723'} | ||
}; | }; | ||
var excludedPrefixes = ['Template:', 'Category:', 'MediaWiki:', 'Special:', 'File:']; | var excludedPrefixes = ['Template:', 'Category:', 'MediaWiki:', 'Special:', 'File:']; | ||
function applyPatch() { | |||
var originalUpdate = window.vis.DataSet.prototype.update; | var originalUpdate = window.vis.DataSet.prototype.update; | ||
window.vis.DataSet.prototype.update = function(data, senderId) { | window.vis.DataSet.prototype.update = function(data, senderId) { | ||
| Line 44: | Line 48: | ||
}; | }; | ||
} | } | ||
/* Poll for vis.js readiness - it loads async via ResourceLoader */ | |||
var attempts = 0; | |||
var maxAttempts = 100; | |||
var timer = setInterval(function() { | |||
attempts++; | |||
if (window.vis && window.vis.DataSet) { | |||
clearInterval(timer); | |||
applyPatch(); | |||
} else if (attempts >= maxAttempts) { | |||
clearInterval(timer); | |||
} | |||
}, 100); | |||
})(); | })(); | ||
Latest revision as of 14:02, 18 February 2026
/* Network Extension Enhancements - Filter external links, color by namespace.
* vis.js is loaded asynchronously by the Network extension, so we poll
* until window.vis.DataSet is available before monkey-patching.
*/
(function() {
'use strict';
var nsColors = {
'Workflow': {bg: '#4CAF50', border: '#2E7D32', font: '#1B5E20'},
'Principle': {bg: '#2196F3', border: '#1565C0', font: '#0D47A1'},
'Implementation': {bg: '#FF9800', border: '#EF6C00', font: '#E65100'},
'Artifact': {bg: '#9C27B0', border: '#7B1FA2', font: '#4A148C'},
'Heuristic': {bg: '#F44336', border: '#C62828', font: '#B71C1C'},
'Environment': {bg: '#00BCD4', border: '#0097A7', font: '#006064'},
'Resource': {bg: '#795548', border: '#5D4037', font: '#3E2723'}
};
var excludedPrefixes = ['Template:', 'Category:', 'MediaWiki:', 'Special:', 'File:'];
function applyPatch() {
var originalUpdate = window.vis.DataSet.prototype.update;
window.vis.DataSet.prototype.update = function(data, senderId) {
if (!Array.isArray(data)) data = [data];
data = data.filter(function(item) {
var id = item.id || '', to = item.to || '', from = item.from || '';
if (id.indexOf('http://') === 0 || id.indexOf('https://') === 0) return false;
if (to.indexOf('http://') === 0 || to.indexOf('https://') === 0) return false;
if (from.indexOf('http://') === 0 || from.indexOf('https://') === 0) return false;
for (var i = 0; i < excludedPrefixes.length; i++) {
if (id.indexOf(excludedPrefixes[i]) === 0) return false;
if (to.indexOf(excludedPrefixes[i]) === 0) return false;
if (from.indexOf(excludedPrefixes[i]) === 0) return false;
}
return true;
}).map(function(item) {
if (item.id && !item.from && !item.to) {
for (var ns in nsColors) {
if (item.id.indexOf(ns + ':') === 0) {
item.color = {background: nsColors[ns].bg, border: nsColors[ns].border,
highlight: {background: nsColors[ns].bg, border: nsColors[ns].border}};
item.font = item.font || {};
item.font.color = nsColors[ns].font;
break;
}
}
}
return item;
});
return originalUpdate.call(this, data, senderId);
};
}
/* Poll for vis.js readiness - it loads async via ResourceLoader */
var attempts = 0;
var maxAttempts = 100;
var timer = setInterval(function() {
attempts++;
if (window.vis && window.vis.DataSet) {
clearInterval(timer);
applyPatch();
} else if (attempts >= maxAttempts) {
clearInterval(timer);
}
}, 100);
})();