MediaWiki:Gadget-ViewerMode.js:修订间差异

Use confirmed-available icons: watchlist for reader, wikiText for editor
Simulate full visitor view: hide user menu + custom buttons, add escape pill
 
(未显示同一用户的3个中间版本)
第1行: 第1行:
/**
/**
  * ViewerMode gadget — lets editors preview the site as a regular visitor.
  * ViewerMode gadget — simulates a genuine visitor's view as closely as possible.
  * Also fixes the missing icon on the "My blog" menu item.
  * In reader view: hides ALL editor UI (edit buttons, user menu, custom admin buttons).
* An "退出阅读视图" escape pill is fixed bottom-left so you can always get back.
  */
  */
( function () {
( function () {
第9行: 第10行:


     var STORAGE_KEY = 'mw-viewer-mode';
     var STORAGE_KEY = 'mw-viewer-mode';
     var READER_ICON  = 'vector-icon mw-ui-icon-watchlist mw-ui-icon-wikimedia-watchlist';
     var active      = localStorage.getItem( STORAGE_KEY ) === '1';
     var EDITOR_ICON  = 'vector-icon mw-ui-icon-wikiText mw-ui-icon-wikimedia-wikiText';
     var btn        = null;   // header pill
     var active = localStorage.getItem( STORAGE_KEY ) === '1';
     var exitBtn    = null;  // escape pill (bottom-left, only visible in reader view)


     if ( active ) {
     if ( active ) { document.documentElement.classList.add( 'viewer-mode' ); }
        document.documentElement.classList.add( 'viewer-mode' );
    }


     function toggle() {
     function setActive( state ) {
         active = !active;
         active = state;
         localStorage.setItem( STORAGE_KEY, active ? '1' : '0' );
         localStorage.setItem( STORAGE_KEY, active ? '1' : '0' );
         document.documentElement.classList.toggle( 'viewer-mode', active );
         document.documentElement.classList.toggle( 'viewer-mode', active );
         updateButton();
         updateUI();
     }
     }


     function updateButton() {
     function toggle() { setActive( !active ); }
        var label = document.querySelector( '#pt-viewer-mode .viewer-mode-label' );
 
        var icon  = document.querySelector( '#pt-viewer-mode .vector-icon' );
    function updateUI() {
         if ( !label ) { return; }
         if ( btn ) {
        if ( active ) {
            if ( active ) {
            label.textContent = 'Editor mode';
                btn.textContent = '📖 阅读视图';
            if ( icon ) { icon.className = EDITOR_ICON; }
                btn.classList.add( 'vm-active' );
        } else {
            } else {
            label.textContent = 'Reader view';
                btn.textContent = '📖 阅读视图';
            if ( icon ) { icon.className = READER_ICON; }
                btn.classList.remove( 'vm-active' );
            }
         }
         }
     }
     }


     $( function () {
     $( function () {
         // Fix missing icon on "My blog"
         /* ── Fix missing icon on "My blog" ─────────────────────── */
         var blogLink = document.querySelector( '#pt-simpleblog_myblog a' );
         var blogLink = document.querySelector( '#pt-simpleblog_myblog a' );
         if ( blogLink && !blogLink.querySelector( '.vector-icon' ) ) {
         if ( blogLink && !blogLink.querySelector( '.vector-icon' ) ) {
             var blogIcon = document.createElement( 'span' );
             var blogIcon = document.createElement( 'span' );
             blogIcon.className = 'vector-icon mw-ui-icon-article mw-ui-icon-wikimedia-article';
             blogIcon.className = 'vector-icon mw-ui-icon-article mw-ui-icon-wikimedia-article';
            blogLink.insertBefore( document.createTextNode( ' ' ), blogLink.firstChild );
             blogLink.insertBefore( blogIcon, blogLink.firstChild );
             blogLink.insertBefore( blogIcon, blogLink.firstChild );
         }
         }


         // Add viewer-mode toggle at end of p-personal list
         /* ── Header toggle pill ─────────────────────────────────── */
        // (logout is in its own separate portlet: p-user-menu-logout)
         btn = document.createElement( 'button' );
         var menuList = document.querySelector( '#p-personal .vector-menu-content-list' );
         btn.id       = 'vm-toggle';
        if ( !menuList ) { return; }
         btn.className = 'vm-toggle';
 
         btn.textContent = '📖 阅读视图';
        var li = document.createElement( 'li' );
         btn.title    = '模拟访客视图(隐藏所有编辑工具)';
         li.id = 'pt-viewer-mode';
         btn.addEventListener( 'click', toggle );
         li.className = 'mw-list-item';
         if ( active ) { btn.classList.add( 'vm-active' ); }
 
         var a = document.createElement( 'a' );
         a.href = '#';
 
         var iconSpan = document.createElement( 'span' );
         iconSpan.className = active ? EDITOR_ICON : READER_ICON;
 
        var textSpan = document.createElement( 'span' );
        textSpan.className = 'viewer-mode-label';
        textSpan.textContent = active ? 'Editor mode' : 'Reader view';


         a.appendChild( iconSpan );
         var target = document.querySelector( '.vector-user-links-main' ) ||
        a.appendChild( document.createTextNode( ' ' ) );
                    document.querySelector( '.vector-user-links' );
         a.appendChild( textSpan );
         if ( target ) { target.insertBefore( btn, target.firstChild ); }
        a.addEventListener( 'click', function ( e ) {
            e.preventDefault();
            toggle();
        } );


         li.appendChild( a );
         /* ── Escape pill (bottom-left, only shown in reader view) ─ */
         menuList.appendChild( li );
        exitBtn = document.createElement( 'button' );
        exitBtn.id        = 'vm-exit';
        exitBtn.className = 'vm-exit';
        exitBtn.innerHTML = '✏️ 退出阅读视图';
        exitBtn.title    = '切换回编辑视图';
        exitBtn.addEventListener( 'click', toggle );
         document.body.appendChild( exitBtn );
     } );
     } );
}() );
}() );