MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
LeTrashman (talk | contribs) No edit summary |
(Disabled Minerva-specific styles workaround) |
||
(84 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ |
/* Any JavaScript here will be loaded for all users on every page load. */ |
||
function initCountdown() { |
|||
var reset = new Date(); |
|||
reset.setUTCHours(19, 0, 0, 0); |
|||
function pad(n) { |
|||
return ("0" + (n | 0)).slice(-2); |
|||
} |
|||
$(function() { |
|||
if (window.location.host === "bluearchive.miraheze.org") { |
|||
var now = new Date(); |
|||
window.location.replace("https://bluearchive.wiki" + window.location.pathname); |
|||
if (now > reset) { |
|||
} |
|||
reset.setUTCDate(reset.getUTCDate() + 1); |
|||
// Common styles are currently loaded through an @import statement in MediaWiki:Mobile.css. I do not know what calls mw.loader.using('mobile.site.styles') currently, but it appears to happen. This code is preserved in case something breaks after caches expire. |
|||
// if (mw.config.get("skin") === "minerva") { |
|||
// // This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845 |
|||
// loadMobileStylesheet(); |
|||
// } |
|||
function initCountdown() { |
|||
var reset = new Date(); |
|||
reset.setUTCHours(19, 0, 0, 0); |
|||
function pad(n) { |
|||
return ("0" + (n | 0)).slice(-2); |
|||
} |
|||
function tick() { |
|||
var now = new Date(); |
|||
if (now > reset) { |
|||
reset.setUTCDate(reset.getUTCDate() + 1); |
|||
} |
|||
var remaining = ((reset - now) / 1000); |
|||
var hours = (remaining / 3600) % 60; |
|||
var minutes = (remaining / 60) % 60; |
|||
var seconds = remaining % 60; |
|||
document.getElementById("resetCountdown").innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds); |
|||
setTimeout(tick, 1000); |
|||
} |
|||
tick(); |
|||
} |
} |
||
function initCharacterTable() { |
|||
var remaining = ((reset - now) / 1000); |
|||
mw.loader.using("jquery.tablesorter", function() { |
|||
var hours = (remaining / 3600) % 60; |
|||
$("table.sortable").tablesorter({sortList: [{2: "desc"}, {1: "asc"}]}); |
|||
var minutes = (remaining / 60) % 60; |
|||
}); |
|||
} |
|||
document.getElementById("resetCountdown").innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds); |
|||
function loadMobileStylesheet() { |
|||
setTimeout(tick, 1000); |
|||
var head = document.getElementsByTagName("head")[0]; |
|||
} |
|||
var common = document.createElement("link"); |
|||
var mobile = document.createElement("link"); |
|||
common.rel = mobile.rel = "stylesheet"; |
|||
common.type = mobile.type = "text/css"; |
|||
common.href = "https://bluearchive.miraheze.org/w/load.php?lang=en&modules=site.styles&only=styles&skin=minerva"; |
|||
mobile.href = "https://bluearchive.miraheze.org/wiki/MediaWiki:Mobile.css?action=raw&ctype=text/css"; |
|||
head.appendChild(common); |
|||
head.appendChild(mobile); |
|||
} |
|||
if (mw.config.get("wgIsArticle")) { |
|||
// These won't run in Edit mode |
|||
if (mw.config.get("wgPageName") === "Main_Page") { |
|||
initCountdown(); |
|||
initBirthdays(); |
|||
} |
|||
if (mw.config.get("wgPageName") === "Characters") { |
|||
initCharacterTable(); |
|||
} |
|||
if (mw.config.get("wgPageName") === "Characters" || mw.config.get("wgPageName") === "Characters_StatChart") { |
|||
mw.loader.load( '/w/index.php?title=MediaWiki:CharacterTable.js&action=raw&ctype=text/javascript' ); |
|||
} |
|||
if (mw.config.get("wgPageName") === "An_Unconcealed_Heart/Mitcher") { |
|||
mw.loader.load( '/w/index.php?title=MediaWiki:Mitcher.js&action=raw&ctype=text/javascript' ); |
|||
} |
|||
if (mw.config.get("wgPageName") === "A_Game_Before_the_New_Year's_Feast_~_One-and-Done_Match_~/Junby") { |
|||
mw.loader.load( '/w/index.php?title=MediaWiki:Junby.js&action=raw&ctype=text/javascript' ); |
|||
} |
|||
const title_split = mw.config.get('wgTitle').toLowerCase().split("/"); |
|||
tick(); |
|||
if (mw.config.get('wgNamespaceNumber') === 0 && title_split.length === 2 && title_split[1] === 'momotalk') { |
|||
mw.loader.load( '/w/index.php?title=MediaWiki:MomotalkChoice.js&action=raw&ctype=text/javascript' ); |
|||
} |
|||
} |
|||
}); |
|||
/* Character stat calc & affection table */ |
|||
mw.loader.load( '/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript' ); |
|||
/* Character birthdays - start */ |
|||
function initBirthdays(){ |
|||
const months = [ |
|||
'January', |
|||
'February', |
|||
'March', |
|||
'April', |
|||
'May', |
|||
'June', |
|||
'July', |
|||
'August', |
|||
'September', |
|||
'October', |
|||
'November', |
|||
'December' |
|||
]; |
|||
var birthdays = []; |
|||
var data_out = []; |
|||
var html_out = ''; |
|||
var raw_data = $('#character-birthdays').attr('data-birthdays').split('&'); |
|||
raw_data.forEach(function (character) { |
|||
var char_data = character.split(' |'); |
|||
char_data.push(char_data[0].split('(')[0].trim()); //normalized name |
|||
if (!birthdays.some(function (data) {return data[2] == char_data[2];})) birthdays.push(char_data); |
|||
}); |
|||
for (var index = -1; index <= 30; index++) { |
|||
var date = new Date(); |
|||
date.setDate(date.getDate() + index); |
|||
data_out = data_out.concat(birthdays.filter(function (data) { return data[1] == months[date.getMonth()]+' '+date.getDate();})); |
|||
if (data_out.length >= 5) { break; } |
|||
} |
|||
data_out.forEach( |
|||
function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+' ('+character[1].replace(' ', ' ')+')</a>'; } |
|||
); |
|||
$('#character-birthdays').append(html_out).css( "display", "" ); |
|||
} |
} |
||
/* Character birthdays - end */ |
|||
function initTwitter() { |
|||
document.getElementById("twitterContainer").innerHTML = "<a class=\"twitter-timeline\" href=\"https://twitter.com/Blue_ArchiveJP?ref_src=twsrc%5Etfw\">Tweets by Blue_ArchiveJP</a> <script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>"; |
|||
/* Character voice preview - start */ |
|||
$( document ).ready(function() { |
|||
initCharacterVoice(); |
|||
}); |
|||
function initCharacterVoice(){ |
|||
var voice = $(".character td.character-voice"); |
|||
if (voice.length && voice.attr('data-voice').length) { |
|||
voice.addClass('character-voice-preview').on("click", function(){playCharacterVoice();}); |
|||
voice.wrapInner('<span>'); |
|||
if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px'); |
|||
voice.find("span").children().unwrap(); |
|||
} |
|||
} |
} |
||
if(mw.config.get("wgPageName") === "Main_Page") { |
|||
function playCharacterVoice(){ |
|||
initCountdown(); |
|||
var voice = $(".character td.character-voice"); |
|||
initTwitter(); |
|||
if (voice.find('audio').length == 0) { |
|||
voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>'); |
|||
voice.find('audio')[0].volume=0.6; |
|||
} |
|||
voice.find('audio')[0].play(); |
|||
} |
} |
||
/* Character voice preview - end */ |
|||
/* Character video preview - start */ |
|||
$( document ).ready(function() { |
|||
//if video tab is open on inital page display |
|||
if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false' || window.location.hash == '#Chibi-0' || window.location.hash == '#Other-0' ) initCharacterVideo($('.character-images')); |
|||
//Tabber initializes late so events are bound at first click on parent div |
|||
$(".character-images div.tabber").on("click", function(){ |
|||
if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false') initCharacterVideo($('.character-images')); |
|||
$(".character-images [data-title='Chibi']").on("click", function(){initCharacterVideo($('.character-images'));}); |
|||
}); |
|||
}) |
|||
function initCharacterVideo(element) { |
|||
var container = element.find('div.video'); |
|||
if (!container.attr('data-video-initialized')) { |
|||
var video_src = container.attr('data-videosrc'); |
|||
container.html('<video autoplay loop playsinline><source src="'+video_src+'" type="video/webm" /></video>'); |
|||
container.on("click", function(){playToggle(container)}); |
|||
container.attr('data-video-initialized', true); |
|||
} |
|||
} |
|||
function playToggle(element) { |
|||
var video = element.find('video').get(0); |
|||
if (video.paused) video.play(); |
|||
else video.pause(); |
|||
} |
|||
/* Character video preview - end */ |
|||
/* XP tables */ |
|||
mw.loader.load( '/w/index.php?title=MediaWiki:XPtable.js&action=raw&ctype=text/javascript' ); |
Revision as of 04:21, 22 April 2024
/* Any JavaScript here will be loaded for all users on every page load. */
$(function() {
if (window.location.host === "bluearchive.miraheze.org") {
window.location.replace("https://bluearchive.wiki" + window.location.pathname);
}
// Common styles are currently loaded through an @import statement in MediaWiki:Mobile.css. I do not know what calls mw.loader.using('mobile.site.styles') currently, but it appears to happen. This code is preserved in case something breaks after caches expire.
// if (mw.config.get("skin") === "minerva") {
// // This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
// loadMobileStylesheet();
// }
function initCountdown() {
var reset = new Date();
reset.setUTCHours(19, 0, 0, 0);
function pad(n) {
return ("0" + (n | 0)).slice(-2);
}
function tick() {
var now = new Date();
if (now > reset) {
reset.setUTCDate(reset.getUTCDate() + 1);
}
var remaining = ((reset - now) / 1000);
var hours = (remaining / 3600) % 60;
var minutes = (remaining / 60) % 60;
var seconds = remaining % 60;
document.getElementById("resetCountdown").innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
setTimeout(tick, 1000);
}
tick();
}
function initCharacterTable() {
mw.loader.using("jquery.tablesorter", function() {
$("table.sortable").tablesorter({sortList: [{2: "desc"}, {1: "asc"}]});
});
}
function loadMobileStylesheet() {
var head = document.getElementsByTagName("head")[0];
var common = document.createElement("link");
var mobile = document.createElement("link");
common.rel = mobile.rel = "stylesheet";
common.type = mobile.type = "text/css";
common.href = "https://bluearchive.miraheze.org/w/load.php?lang=en&modules=site.styles&only=styles&skin=minerva";
mobile.href = "https://bluearchive.miraheze.org/wiki/MediaWiki:Mobile.css?action=raw&ctype=text/css";
head.appendChild(common);
head.appendChild(mobile);
}
if (mw.config.get("wgIsArticle")) {
// These won't run in Edit mode
if (mw.config.get("wgPageName") === "Main_Page") {
initCountdown();
initBirthdays();
}
if (mw.config.get("wgPageName") === "Characters") {
initCharacterTable();
}
if (mw.config.get("wgPageName") === "Characters" || mw.config.get("wgPageName") === "Characters_StatChart") {
mw.loader.load( '/w/index.php?title=MediaWiki:CharacterTable.js&action=raw&ctype=text/javascript' );
}
if (mw.config.get("wgPageName") === "An_Unconcealed_Heart/Mitcher") {
mw.loader.load( '/w/index.php?title=MediaWiki:Mitcher.js&action=raw&ctype=text/javascript' );
}
if (mw.config.get("wgPageName") === "A_Game_Before_the_New_Year's_Feast_~_One-and-Done_Match_~/Junby") {
mw.loader.load( '/w/index.php?title=MediaWiki:Junby.js&action=raw&ctype=text/javascript' );
}
const title_split = mw.config.get('wgTitle').toLowerCase().split("/");
if (mw.config.get('wgNamespaceNumber') === 0 && title_split.length === 2 && title_split[1] === 'momotalk') {
mw.loader.load( '/w/index.php?title=MediaWiki:MomotalkChoice.js&action=raw&ctype=text/javascript' );
}
}
});
/* Character stat calc & affection table */
mw.loader.load( '/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript' );
/* Character birthdays - start */
function initBirthdays(){
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
var birthdays = [];
var data_out = [];
var html_out = '';
var raw_data = $('#character-birthdays').attr('data-birthdays').split('&');
raw_data.forEach(function (character) {
var char_data = character.split(' |');
char_data.push(char_data[0].split('(')[0].trim()); //normalized name
if (!birthdays.some(function (data) {return data[2] == char_data[2];})) birthdays.push(char_data);
});
for (var index = -1; index <= 30; index++) {
var date = new Date();
date.setDate(date.getDate() + index);
data_out = data_out.concat(birthdays.filter(function (data) { return data[1] == months[date.getMonth()]+' '+date.getDate();}));
if (data_out.length >= 5) { break; }
}
data_out.forEach(
function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+' ('+character[1].replace(' ', ' ')+')</a>'; }
);
$('#character-birthdays').append(html_out).css( "display", "" );
}
/* Character birthdays - end */
/* Character voice preview - start */
$( document ).ready(function() {
initCharacterVoice();
});
function initCharacterVoice(){
var voice = $(".character td.character-voice");
if (voice.length && voice.attr('data-voice').length) {
voice.addClass('character-voice-preview').on("click", function(){playCharacterVoice();});
voice.wrapInner('<span>');
if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px');
voice.find("span").children().unwrap();
}
}
function playCharacterVoice(){
var voice = $(".character td.character-voice");
if (voice.find('audio').length == 0) {
voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>');
voice.find('audio')[0].volume=0.6;
}
voice.find('audio')[0].play();
}
/* Character voice preview - end */
/* Character video preview - start */
$( document ).ready(function() {
//if video tab is open on inital page display
if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false' || window.location.hash == '#Chibi-0' || window.location.hash == '#Other-0' ) initCharacterVideo($('.character-images'));
//Tabber initializes late so events are bound at first click on parent div
$(".character-images div.tabber").on("click", function(){
if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false') initCharacterVideo($('.character-images'));
$(".character-images [data-title='Chibi']").on("click", function(){initCharacterVideo($('.character-images'));});
});
})
function initCharacterVideo(element) {
var container = element.find('div.video');
if (!container.attr('data-video-initialized')) {
var video_src = container.attr('data-videosrc');
container.html('<video autoplay loop playsinline><source src="'+video_src+'" type="video/webm" /></video>');
container.on("click", function(){playToggle(container)});
container.attr('data-video-initialized', true);
}
}
function playToggle(element) {
var video = element.find('video').get(0);
if (video.paused) video.play();
else video.pause();
}
/* Character video preview - end */
/* XP tables */
mw.loader.load( '/w/index.php?title=MediaWiki:XPtable.js&action=raw&ctype=text/javascript' );