Jump to content

User:The Transhumanist/ViewAnnotationToggler.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by The Transhumanist (talk | contribs) at 12:59, 8 December 2016 (ANNOTATION TOGGLE version 0.1). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* anno.js: Adds a tab menu item and hot key to toggle annotations in lists.

Based on: https://en.wikipedia.org/w/index.php?title=User:PleaseStand/hide-vector-sidebar.js&oldid=580854231
and https://en.wikipedia.org/wiki/User:Thespaceface/MetricFirst.js  */

The hot key is Shift-Alt-a. 

I ran into trouble with local scope, and solved it by reinitializing variables locally and creating a local 2nd clone.

There is probably a much more efficient method than this.  If you happen to know of one, please let me know.
(The Transhumanist)

Currently, it is being tested on the use of regex within the ID element 'mw-content-text'.

Improvements needed:

     Anchor regexes on <li> element delimiters
     Find/replace annotations of other formats
          nbsp;
          comma
     Find/replace annotations of lead list entries (without bullets)
     Test on many pages of every page type
     Toggle the script based on setting, rather than for each article
          That is, make it so the script doesn't reset upon loading each page
          Have it remember its toggle position
          (WikEd has this feature, and a checkbox too)

( function ( mw, $ ) {
    var annoSwitch; 
    var cont = document.getElementById('mw-content-text');
    var orig = cont.cloneNode(true);

    function annoHide() {
        var cont = document.getElementById('mw-content-text');
        var whattohide = cont.innerHTML.replace(/
        whattohide = cont.innerHTML.replace(/
        cont.innerHTML = whattohide;
//        document.getElementById( 'bodyContent' ).style.visibility = 'hidden';
        if ( annoSwitch ) {
            annoSwitch.parentNode.removeChild(annoSwitch);
        }
        annoSwitch = mw.util.addPortletLink( 'p-cactions', '#', 'Show anno', 'ca-anno', 'Show the annotations', 'a' );
        $( annoSwitch ).click( function ( e ) {
            e.preventDefault();
            annoShow();
        } );
    }
   
    function annoShow() {
        var cont = document.getElementById('mw-content-text');
        var orig2 = orig.cloneNode(true);
        $(cont).replaceWith(orig2);
//        bodyContent.replaceChild(orig, cont);
//        document.getElementById( 'bodyContent' ).style.visibility = '';
        if ( annoSwitch ) {
            annoSwitch.parentNode.removeChild(annoSwitch);
        }
        annoSwitch = mw.util.addPortletLink( 'p-cactions', '#', 'Hide anno', 'ca-anno', 'Hide the annotations', 'a' );
        $( annoSwitch ).click( function ( e ) {
            e.preventDefault();
            annoHide();
        } );
    }
   
    // Only activate on Vector skin
    if ( mw.config.get( 'skin' ) === 'vector' ) {
        $( function() {
            // Change this if you want to show annotations by default
            annoHide();
        } );
    }
   
}( mediaWiki, jQuery ) );