MediaWiki:Gadget-ViewerMode.js:修订间差异
外观
LocalAdmin(留言 | 贡献) 读者模式切换 JS |
LocalAdmin(留言 | 贡献) Fix portlet ID for Vector 2022 |
||
| 第1行: | 第1行: | ||
/** | /** | ||
* ViewerMode gadget — toggles a "visitor view" for editors. | * ViewerMode gadget — toggles a "visitor view" for editors. | ||
* Hides all editing UI so editors can | * Hides all editing UI so editors can preview the site as a regular visitor. | ||
*/ | */ | ||
( function () { | ( function () { | ||
| 第13行: | 第13行: | ||
var active = localStorage.getItem( STORAGE_KEY ) === '1'; | var active = localStorage.getItem( STORAGE_KEY ) === '1'; | ||
// Apply class immediately to prevent flash of edit elements on load | |||
if ( active ) { | if ( active ) { | ||
document.documentElement.classList.add( 'viewer-mode' ); | document.documentElement.classList.add( 'viewer-mode' ); | ||
| 第39行: | 第40行: | ||
a.textContent = '⚙ 编辑者模式'; | a.textContent = '⚙ 编辑者模式'; | ||
a.title = '切换回编辑者模式(当前:读者视图)'; | a.title = '切换回编辑者模式(当前:读者视图)'; | ||
a.style.fontWeight = 'bold'; | |||
} else { | } else { | ||
a.textContent = '👁 读者模式'; | a.textContent = '👁 读者模式'; | ||
a.title = ' | a.title = '切换到读者模式,预览访客视图'; | ||
a.style.fontWeight = ''; | |||
} | } | ||
} | } | ||
mw.loader.using( [ 'mediawiki.util' ] ).then( function () { | mw.loader.using( [ 'mediawiki.util' ] ).then( function () { | ||
var portletLink = mw.util.addPortletLink( | // Vector 2022 uses p-vector-user-menu-overflow for extra user menu items | ||
var portlets = [ 'p-vector-user-menu-overflow', 'p-personal' ]; | |||
var portletLink = null; | |||
for ( var i = 0; i < portlets.length; i++ ) { | |||
portletLink = mw.util.addPortletLink( | |||
portlets[ i ], | |||
'#', | |||
active ? '⚙ 编辑者模式' : '👁 读者模式', | |||
'pt-viewer-mode', | |||
active ? '切换回编辑者模式(当前:读者视图)' : '切换到读者模式,预览访客视图' | |||
); | |||
if ( portletLink ) { | |||
break; | |||
} | |||
} | |||
if ( portletLink ) { | if ( portletLink ) { | ||
2026年3月12日 (四) 10:01的版本
/**
* ViewerMode gadget — toggles a "visitor view" for editors.
* Hides all editing UI so editors can preview the site as a regular visitor.
*/
( function () {
'use strict';
if ( !mw.user.isAllowed( 'edit' ) ) {
return;
}
var STORAGE_KEY = 'mw-viewer-mode';
var active = localStorage.getItem( STORAGE_KEY ) === '1';
// Apply class immediately to prevent flash of edit elements on load
if ( active ) {
document.documentElement.classList.add( 'viewer-mode' );
}
function applyMode() {
if ( active ) {
document.documentElement.classList.add( 'viewer-mode' );
} else {
document.documentElement.classList.remove( 'viewer-mode' );
}
updateButton();
}
function toggle() {
active = !active;
localStorage.setItem( STORAGE_KEY, active ? '1' : '0' );
applyMode();
}
function updateButton() {
var link = document.getElementById( 'pt-viewer-mode' );
if ( !link ) { return; }
var a = link.querySelector( 'a' );
if ( active ) {
a.textContent = '⚙ 编辑者模式';
a.title = '切换回编辑者模式(当前:读者视图)';
a.style.fontWeight = 'bold';
} else {
a.textContent = '👁 读者模式';
a.title = '切换到读者模式,预览访客视图';
a.style.fontWeight = '';
}
}
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
// Vector 2022 uses p-vector-user-menu-overflow for extra user menu items
var portlets = [ 'p-vector-user-menu-overflow', 'p-personal' ];
var portletLink = null;
for ( var i = 0; i < portlets.length; i++ ) {
portletLink = mw.util.addPortletLink(
portlets[ i ],
'#',
active ? '⚙ 编辑者模式' : '👁 读者模式',
'pt-viewer-mode',
active ? '切换回编辑者模式(当前:读者视图)' : '切换到读者模式,预览访客视图'
);
if ( portletLink ) {
break;
}
}
if ( portletLink ) {
portletLink.querySelector( 'a' ).addEventListener( 'click', function ( e ) {
e.preventDefault();
toggle();
} );
}
applyMode();
} );
}() );