User:Monster Domosed/common.js: Difference between revisionsGive feedback
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 8: | Line 8: | ||
var tooltipCache = {}; | var tooltipCache = {}; | ||
var | var stylesLoaded = {}; // Track which stylesheets have been loaded | ||
var $tooltip = null; | var $tooltip = null; | ||
var currentTarget = null; | var currentTarget = null; | ||
| Line 17: | Line 17: | ||
var HIDE_DELAY = 300; | var HIDE_DELAY = 300; | ||
var EDGE_MARGIN = 10; // margin from viewport edges | var EDGE_MARGIN = 10; // margin from viewport edges | ||
/* ─── Load and inject CSS from a template page ─── */ | |||
function loadTemplateStyles( pageName, callback ) { | |||
// Already loaded, just callback | |||
if ( stylesLoaded[ pageName ] ) { | |||
if ( callback ) callback(); | |||
return; | |||
} | |||
// Fetch the CSS content | |||
new mw.Api().get( { | |||
action: 'query', | |||
prop: 'revisions', | |||
titles: pageName, | |||
rvprop: 'content', | |||
rvslots: 'main', | |||
formatversion: 2 | |||
} ).then( function ( data ) { | |||
if ( data.query && data.query.pages && data.query.pages[0] ) { | |||
var page = data.query.pages[0]; | |||
if ( page.revisions && page.revisions[0] ) { | |||
var css = page.revisions[0].slots.main.content; | |||
// Create a style element and inject the CSS | |||
var $style = $( '<style>' ) | |||
.attr( 'data-template-styles', pageName ) | |||
.text( css ) | |||
.appendTo( 'head' ); | |||
stylesLoaded[ pageName ] = true; | |||
if ( callback ) callback(); | |||
} | |||
} | |||
} ).catch( function ( error ) { | |||
console.error( 'Failed to load template styles:', pageName, error ); | |||
if ( callback ) callback(); // Continue anyway | |||
} ); | |||
} | |||
/* ─── Create tooltip DOM element ─── */ | /* ─── Create tooltip DOM element ─── */ | ||
| Line 122: | Line 160: | ||
return char.toUpperCase(); | return char.toUpperCase(); | ||
} ); | } ); | ||
} | |||
/* ─── Extract templatestyles tags from HTML ─── */ | |||
function extractTemplateStyles( html ) { | |||
var regex = /<link[^>]+href="[^"]*\/load\.php\?[^"]*modules=ext\.templateStyles[^"]*"[^>]*>/gi; | |||
var stylesheets = []; | |||
var match; | |||
// Try to find templatestyles link tags | |||
while ( ( match = regex.exec( html ) ) !== null ) { | |||
stylesheets.push( match[0] ); | |||
} | |||
return stylesheets; | |||
} | } | ||
| Line 172: | Line 224: | ||
} | } | ||
// | // Load the template styles first | ||
new mw.Api().post( { | var stylesheetToLoad = type === 'item' ? 'Template:Infobox item/styles.css' : 'Template:Ability card v2/styles.css'; | ||
loadTemplateStyles( stylesheetToLoad, function () { | |||
// Now fetch the rendered template | |||
new mw.Api().post( { | |||
action: 'parse', | |||
text: templateText, | |||
title: mw.config.get( 'wgPageName' ), | |||
prop: 'text', | |||
disablelimitreport: true, | |||
disableeditsection: true, | |||
wrapoutputclass: '', | |||
format: 'json' | |||
} ).then( function ( data ) { | |||
var html = data.parse.text[ '*' ]; | |||
tooltipCache[ cacheKey ] = html; | |||
// Only show if the mouse is still on this element | |||
if ( currentTarget === anchor ) { | if ( currentTarget === anchor ) { | ||
tt.html( html ).show(); | tt.html( html ).show(); | ||
positionTooltip( anchor ); | positionTooltip( anchor ); | ||
// Initialize ability upgrades for new content | |||
if ( type === 'ability' ) { | if ( type === 'ability' ) { | ||
// Trigger MediaWiki's content hook so other gadgets can initialize | |||
mw.hook( 'wikipage.content' ).fire( tt ); | mw.hook( 'wikipage.content' ).fire( tt ); | ||
initAbilityUpgrades( tt ); | initAbilityUpgrades( tt ); | ||
} | } | ||
} | } | ||
} ).catch( function () { | |||
if ( currentTarget === anchor ) { | |||
tt.hide(); | |||
} | |||
} | } ); | ||
} ); | } ); | ||
| Line 299: | Line 334: | ||
} )(); | } )(); | ||
/** | /** | ||