MediaWiki:Gadget-enableTopNavBar.js

// ________________________________________________________________________________________ // |                                                                                        | // |                    === WARNING: GLOBAL GADGET FILE ===                                 | // |                 Changes to this page affect many users. | // |																						| // |________________________________________________________________________________________| // // Rolling Release 04.07.2021 "use strict"; var PoPgetNumberText = function(origTxt, txt) { var strIndex = origTxt.indexOf(txt); if (strIndex != -1) { strIndex = strIndex + 10; var WidthText = origTxt.slice(strIndex, strIndex + 5).replace(/[^0-9]/g,''); if (WidthText.length > 0) { return parseInt(WidthText, 10); } else { return null; }	} else { return null; } }; var PoPremoveAndText = function(origTxt, txt) { var strIndex = origTxt.indexOf(txt); var leftPar = origTxt.lastIndexOf("(",strIndex);	var rightPar = origTxt.indexOf(")",strIndex + txt.length); var leftAnd = origTxt.toLowerCase.lastIndexOf("and",leftPar); if (leftAnd != -1 && ((leftPar - leftAnd) == 4)) { return (origTxt.slice(0,leftAnd) + origTxt.slice(rightPar + 2)).trim; }	var rightAnd = origTxt.toLowerCase.Index("and",rightPar); if (rightAnd != -1 && ((rightAnd - rightPar) == 2)) { return (origTxt.slice(0,leftPar) + origTxt.slice(rightAnd + 2)).trim; }	return ""; }; var getQueryStringValue = function (url, key) { return decodeURIComponent(url.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")); };

function enableTopNavBar { var catFix = document.getElementById("sidebar-normal-catlinks"); if (catFix !== null) { document.getElementById("page-tools").getElementsByClassName("sidebar-inner")[0].appendChild(catFix); }	for (var st = 0; st < document.styleSheets.length; st++) {		//Config Variables var targetWidth = 1100; var cozyContentFix = ["@media screen and (min-width:1100px) {" + "#mw-content-block { right: auto; margin-left: auto; margin-right: auto; }}"]; // Modify Skin CSS if ((document.styleSheets[st].href !== null) && 		   (getQueryStringValue(document.styleSheets[st].href,"modules").indexOf("skins.timeless") !== -1)) { var sheet = document.styleSheets[st]; var ruleArray = []; for (var i = 0; i < sheet.cssRules.length; i++) { if(sheet.cssRules[i].toString.slice( 8, -1 ) === "CSSMediaRule") { ruleArray.push(i); }			}			var ruleRemoveArray = []; for (i = 0; i < ruleArray.length; i++) { try { var maxWidth = PoPgetNumberText(sheet.cssRules[ruleArray[i]].media[0],"max-width:"); if (maxWidth !== null) { if (maxWidth == (targetWidth - 1)) { var newRule = PoPremoveAndText(sheet.cssRules[ruleArray[i]].media[0],"max-width:"); if (newRule.length > 0) { var oldRule = sheet.cssRules[ruleArray[i]].media[0]; sheet.cssRules[ruleArray[i]].media.appendMedium(newRule); sheet.cssRules[ruleArray[i]].media.deleteMedium(oldRule); }							else { console.log("couldn't modify css rule"); }						} else if (maxWidth >= targetWidth) { ruleRemoveArray.push(ruleArray[i]); }					}				} catch (e) { // Do nothing in case Firefox doesnt allow access }				try { var minWidth = PoPgetNumberText(sheet.cssRules[ruleArray[i]].media[0],"min-width:"); if (minWidth !== null) { if (minWidth >= (targetWidth - 1)) { ruleRemoveArray.push(ruleArray[i]); }					}				} catch (e) { // Do nothing in case Firefox doesnt allow access }			}			ruleRemoveArray = ruleRemoveArray.reverse. ruleRemoveArray = ruleRemoveArray.filter(function(item, index) {return ruleRemoveArray.indexOf(item) === index}); for (i = 0; i < ruleRemoveArray.length; i++) { sheet.deleteRule(ruleRemoveArray[i]); }		}		//Modify our custom CSS if ((document.styleSheets[st].href !== null) && 		   (getQueryStringValue(document.styleSheets[st].href,"modules").indexOf("site.styles") !== -1)) { var sheet = document.styleSheets[st]; var ruleArray = []; for (var i = 0; i < sheet.cssRules.length; i++) { if(sheet.cssRules[i].toString.slice( 8, -1 ) === "CSSMediaRule") { ruleArray.push(i); }			}			var ruleRemoveArray = []; for (i = 0; i < ruleArray.length; i++) { try { var maxWidth = PoPgetNumberText(sheet.cssRules[ruleArray[i]].media[0],"max-width:"); if (maxWidth !== null) { if (maxWidth == (targetWidth - 1)) { var newRule = PoPremoveAndText(sheet.cssRules[ruleArray[i]].media[0],"max-width:"); if (newRule.length > 0) { var oldRule = sheet.cssRules[ruleArray[i]].media[0]; sheet.cssRules[ruleArray[i]].media.appendMedium(newRule); sheet.cssRules[ruleArray[i]].media.deleteMedium(oldRule); }							else { console.log("couldn't modify css rule"); }						}					}				} catch (e) { // Do nothing in case Firefox doesnt allow access }				try { var minWidth = PoPgetNumberText(sheet.cssRules[ruleArray[i]].media[0],"min-width:"); if (minWidth !== null) { //Modify our custom skin CSS if (minWidth == (targetWidth)) { var newRule = PoPremoveAndText(sheet.cssRules[ruleArray[i]].media[0],"min-width:"); if (newRule.length > 0) { if (newRule == "screen") { for (var y = 0; y < sheet.cssRules[ruleArray[i]].cssRules.length; y++) { if (sheet.cssRules[ruleArray[i]].cssRules[y].selectorText == ".sidebar-chunk") { sheet.cssRules[ruleArray[i]].deleteRule(y); break; }									}								}							}							else { console.log("couldn't modify css rule"); }						}					}				} catch (e) { // Do nothing in case Firefox doesnt allow access }			}		}		// Gadget-cozyContent.css Detection if (mw.user.options.get('gadget-cozyContent',"") === "1") { if ((document.styleSheets[st].href !== null) && 				(getQueryStringValue(document.styleSheets[st].href,"modules").indexOf("ext.gadget.") === 0) && 				(getQueryStringValue(document.styleSheets[st].href,"modules").indexOf("cozyContent") !== -1)) { var sheet = document.styleSheets[st]; if (sheet.href.indexOf("cozyContent") !== -1) { for (var y = 0; y < cozyContentFix.length; y++) { var index = sheet.insertRule(cozyContentFix[y], sheet.cssRules.length); }				}			}		}	} } if (document.readyState!="loading") enableTopNavBar; // modern browsers else if (document.addEventListener) document.addEventListener("DOMContentLoaded", enableTopNavBar);