MediaWiki:Gadget-NameSwitch.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
const path = window.location.pathname;
const hashTab = "tab-"+window.location.hash.split("#").pop();
const redirectNotice = document.getElementById("contentSub");
var config;
switch (true) {
case path.includes('Vergina') || path.includes('Hatsune'):
config = {
tabs: {
'tab-Manga-0': 'Vergina',
'tab-TV_Drama-0': 'Hatsune Morishima'
},
firstTab: ['tab-Manga-0'],
secondTab: ['tab-TV_Drama-0']
};
break;
case path.includes('Wonder_of_U') || path.includes('Satoru_Akefu'):
config = {
tabs: {
'tab-Stand-0': 'Wonder of U',
'tab-Satoru_Akefu-0': 'Satoru Akefu'
},
firstTab: ['tab-Stand-0'],
secondTab: ['tab-Satoru_Akefu-0'],
additionalTabs: {
'Stand-1': 'Wonder of U',
'Akefu-1': 'Satoru Akefu',
'Wonder_of_U-2': 'Wonder of U',
'Satoru_Akefu-2': 'Satoru Akefu'
}
};
break;
case path.includes('Dio') || path.includes('DIO'):
config = {
tabs: {
'tab-Part_1-0': 'Dio Brando',
'tab-Part_3-0': 'DIO',
'tab-Part_6-0': 'DIO',
'tab-JORGE_JOESTAR-4': 'Dio Brando',
'tab-CDDH-4': 'DIO'
},
firstTab: ['tab-Part_1-0', 'tab-Spin-Off-0', 'tab-JORGE_JOESTAR-4'],
secondTab: ['tab-Part_3-0', 'tab-Part_6-0', 'tab-CDDH-4']
};
break;
case path.includes('Erina_Pendleton') || path.includes('Erina_Joestar'):
config = {
tabs: {
'tab-Part_1-0': 'Erina Pendleton',
'tab-Part_2-0': 'Erina Joestar'
},
firstTab: ['tab-Part_1-0'],
secondTab: ['tab-Part_2-0']
};
break;
case path.includes('Suzi_Q') || path.includes('Suzi_Q_Joestar'):
config = {
tabs: {
'tab-Part_2-0': 'Suzi Q',
'tab-Part_3-0': 'Suzi Q Joestar'
},
firstTab: ['tab-Part_2-0'],
secondTab: ['tab-Part_3-0']
};
break;
}
jQuery(function() {
var switchSpan = document.body.getElementsByClassName("switcherTabber");
Array.from(switchSpan).forEach(function(ss) {
initTabs(ss, "true");
});
});
function updateTab(tab, subTabbers) {
if (tab.ariaSelected === "true") {
if (tab.dataset.pageName && tab.dataset.pageName !== "undefined") {
selectTabAndSetTitle(tab.id, tab.dataset.pageName, 5);
toggleIntroSwitches(tab.dataset.slot);
}
if (subTabbers) {
subTabbers.forEach(function(ss) {
var subTabs = Array.from(ss.getElementsByClassName("tabber__tabs"))[0];
subTabs = Array.from(subTabs.getElementsByClassName("tabber__tab"));
subTabs.some(function(subtab) {
if (subtab.ariaSelected === "true") {
subtab.click();
}
});
});
}
}
}
function initTabs(switcherTabber, tabberVisible) {
if (!switcherTabber.dataset || !switcherTabber.dataset.processed || switcherTabber.dataset.processed !== "true") {
var switchTabs = switcherTabber.getElementsByClassName("tabber__tabs")[0];
switchTabs = Array.from(switchTabs.getElementsByClassName("tabber__tab"));
var switchNames = [];
for (var i=1; switcherTabber.getAttribute('data-name'+i); i++) {
switchNames.push(switcherTabber.getAttribute('data-name'+i))
};
var i = 0;
switchTabs.some(function(tab) {
tab.dataset.pageName = switchNames[i] || switchNames[switchNames.length-1];
tab.dataset.slot = i;
var query = "#"+CSS.escape(tab.id.split("tab-").pop());
var tabContent = document.body.querySelector(query);
tabContent = Array.from(tabContent.getElementsByClassName("switcherTabber"));
tab.addEventListener("click", function() {updateTab(tab, tabContent)});
tab.dataset.listenerAdded = true;
if (tabContent) {
tabContent.forEach(function(ss) {
initTabs(ss, tab.ariaSelected);
});
}
if (hashTab === tab.id && redirectNotice) {
redirectNotice.style.display = "none";
}
if (tab.ariaSelected === "true" && tabberVisible === "true") {
if (tab.dataset.pageName && tab.dataset.pageName !== "undefined") {
tab.click();
}
}
i++;
});
switcherTabber.dataset.processed = "true";
}
}
// Function to check for redirects and set tab and title accordingly
function checkRedirects() {
const apiUrl = "https://jojowiki.com/api.php?action=query&titles=" + path.split('/').pop() + "&redirects&format=json&origin=*";
const redirectNotice = document.getElementById("contentSub");
fetch(apiUrl)
.then(function(response) {return response.json()})
.then(function(data) {
const redirects = data.query.redirects;
if (!config || !Object.keys(config.tabs).includes(hashTab)) {
if (redirects) {
redirects.forEach(function(redirect) {
if (redirect.from === "Hatsune" || redirect.from === "Hatsune Morishima") {
selectTabAndSetTitle('tab-TV_Drama-0', 'Hatsune Morishima', 1, 5);
} else if (redirect.from === "Satoru Akefu") {
selectTabAndSetTitle('tab-Satoru_Akefu-0', 'Satoru Akefu', 1, 5);
} else if (redirect.from === "DIO") {
selectTabAndSetTitle('tab-Part_3-0', 'DIO', 1, 5);
} else if (redirect.from === "Erina Joestar") {
selectTabAndSetTitle('tab-Part_2-0', 'Erina Joestar', 1, 5);
} else if (redirect.from === "Suzi Q Joestar") {
selectTabAndSetTitle('tab-Part_3-0', 'Suzi Q Joestar', 1, 5);
}
});
redirectNotice.style.display = "none";
}
}
});
}
// Function to set page title
function setPageTitle(newTitle) {
const pageTitle = document.getElementById("firstHeading");
if (pageTitle) {
pageTitle.textContent = newTitle;
}
}
// Function to set infobox title
function setInfoboxTitle(newTitle) {
const titleElement = document.querySelector(".pi-item.pi-item-spacing.pi-title[data-source='title']");
if (titleElement) {
titleElement.textContent = newTitle;
}
}
// Function to select tab and set title with retry mechanism
function selectTabAndSetTitle(tabId, newTitle, slot, retryCount) {
if (retryCount == 0) {
console.error('Failed to set title and select tab for ' + newTitle + 'after multiple attempts');
return;
}
const tab = document.getElementById(tabId);
if (tab) {
console.log('Setting title to ' + newTitle + ' and selecting tab ' + tabId);
history.replaceState(null, null, ' ');
setPageTitle(newTitle);
setInfoboxTitle(newTitle);
toggleIntroSwitches(slot);
} else {
console.warn('Retrying to set title and select tab for ' + newTitle + '(' + (retryCount - 1) + ' retries left)');
setTimeout(function() {selectTabAndSetTitle(tabId, newTitle, retryCount - 1)}, 500);
}
}
// Function to toggle introSwitch and introSwitch2 visibility
function toggleIntroSwitches(activeSlot) {
const introSwitchElements = document.querySelectorAll(".introSwitch");
const introSwitch2Elements = document.querySelectorAll(".introSwitch2");
introSwitchElements.forEach(function(element) {
element.style.display = (activeSlot == 0) ? "inline" : "none";
});
introSwitch2Elements.forEach(function(element) {
element.style.display = (activeSlot != 0) ? "inline" : "none";
});
}
// Call the function to check redirects on page load
checkRedirects();