MediaWiki:Gadget-ViewerMode.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/**
* 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();
} );
}() );