MediaWiki:Gadget-darkmode.js: Skirtumas tarp puslapio versijų
Pereiti į navigaciją
Jump to search
Nėra keitimo santraukos Žyma: Atmesta |
Žyma: Anuliuoti |
||
1 eilutė: | 1 eilutė: | ||
(function() { | |||
var isDark = document.cookie.includes('theme=dark'); | |||
if (isDark) { | |||
document.documentElement.classList.add('wgl-theme-dark', 'wgl-darkmode'); | |||
} else { | |||
document.documentElement.classList.add('wgl-theme-light', 'wgl-lightmode'); | |||
} | |||
})(); | |||
(function($, mw) { | (function($, mw) { | ||
var DARK_COOKIE = 'darkmode', | |||
THEME_COOKIE = 'theme', | |||
isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'), | |||
isMobile = mw.config.get('wgMFMode') !== null, | |||
portletLink; | |||
var self = { | |||
init: function() { | |||
// Apply the current theme on page load | |||
self.applyTheme(isUsingDarkmode); | |||
// Create the dark mode toggle link | |||
portletLink = mw.util.addPortletLink( | |||
(isMobile ? 'p-navigation' : 'p-personal'), | |||
'#', // Use '#' to prevent redirection | |||
(isMobile ? 'Toggle dark mode' : '🌓 '), | |||
'pt-dm-toggle', | |||
'Toggle dark mode', | |||
null, | |||
$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0] | |||
); | |||
// Toggle theme on link click | |||
$(portletLink).click(function(e) { | |||
e.preventDefault(); | |||
isUsingDarkmode = !isUsingDarkmode; | |||
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'}); | |||
$.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'}); | |||
self.applyTheme(isUsingDarkmode); | |||
// Optional: update link text | |||
$(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️'); | |||
}); | |||
}, | |||
applyTheme: function(isDark) { | |||
if (isDark) { | |||
$('body') | |||
.addClass('wgl-theme-dark wgl-darkmode') | |||
.removeClass('wgl-theme-light wgl-lightmode'); | |||
mw.hook('wgl.themeChanged').fire('dark'); | |||
} else { | |||
$('body') | |||
.addClass('wgl-theme-light wgl-lightmode') | |||
.removeClass('wgl-theme-dark wgl-darkmode'); | |||
mw.hook('wgl.themeChanged').fire('light'); | |||
} | |||
// Make the body visible after applying the theme | |||
$('body').css({visibility: 'visible', opacity: 1}); | |||
} | |||
}; | |||
// | |||
$ | |||
} | |||
} | |||
// Ensure | // Ensure mediawiki.util is loaded before init | ||
mw.loader.using(['mediawiki.util'], function () { | mw.loader.using(['mediawiki.util'], function () { | ||
$( | $(self.init); | ||
}); | }); | ||
})(jQuery, mediaWiki); | })(jQuery, mediaWiki); |
09:30, 14 balandžio 2025 versija
(function() {
var isDark = document.cookie.includes('theme=dark');
if (isDark) {
document.documentElement.classList.add('wgl-theme-dark', 'wgl-darkmode');
} else {
document.documentElement.classList.add('wgl-theme-light', 'wgl-lightmode');
}
})();
(function($, mw) {
var DARK_COOKIE = 'darkmode',
THEME_COOKIE = 'theme',
isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
isMobile = mw.config.get('wgMFMode') !== null,
portletLink;
var self = {
init: function() {
// Apply the current theme on page load
self.applyTheme(isUsingDarkmode);
// Create the dark mode toggle link
portletLink = mw.util.addPortletLink(
(isMobile ? 'p-navigation' : 'p-personal'),
'#', // Use '#' to prevent redirection
(isMobile ? 'Toggle dark mode' : '🌓 '),
'pt-dm-toggle',
'Toggle dark mode',
null,
$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
);
// Toggle theme on link click
$(portletLink).click(function(e) {
e.preventDefault();
isUsingDarkmode = !isUsingDarkmode;
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
$.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'});
self.applyTheme(isUsingDarkmode);
// Optional: update link text
$(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️');
});
},
applyTheme: function(isDark) {
if (isDark) {
$('body')
.addClass('wgl-theme-dark wgl-darkmode')
.removeClass('wgl-theme-light wgl-lightmode');
mw.hook('wgl.themeChanged').fire('dark');
} else {
$('body')
.addClass('wgl-theme-light wgl-lightmode')
.removeClass('wgl-theme-dark wgl-darkmode');
mw.hook('wgl.themeChanged').fire('light');
}
// Make the body visible after applying the theme
$('body').css({visibility: 'visible', opacity: 1});
}
};
// Ensure mediawiki.util is loaded before init
mw.loader.using(['mediawiki.util'], function () {
$(self.init);
});
})(jQuery, mediaWiki);