User:Jeff G./monobook/VirtualReferences.js

From Wikipedia, the free encyclopedia
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.
/*=================================================================================================
[[Benutzer:ParaDox/monobook/VirtualReferences.js]] V0.31 19. März 2008 (V0.31b 19. Juni 2008)
    http://de.wikipedia.org/w/index.php?title=Benutzer:ParaDox/monobook/VirtualReferences.js
Bisher mit folgenden Browsern getestet:
    Firefox 2.0.0.14
    Opera 9.27
    Ab V0.30 ist der "Microsoft Internet Explorer" nicht mehr vorgesehen.
Für mehr Info siehe Diskussionsseite:
    http://de.wikipedia.org/w/index.php?title=Benutzer_Diskussion:ParaDox/monobook/VirtualReferences.js
By [[Benutzer:ParaDox]]
    http://de.wikipedia.org/wiki/Benutzer:ParaDox
---------------------------------------------------------------------------------------------------
Nur die folgenden 4 Zeilen in die eigene „monobook.js“ einfügen:
// [[Benutzer:ParaDox/monobook/VirtualReferences.js]]
document.write('<SCRIPT SRC="http://de.wikipedia.org/w/index.php?title='
    + 'Benutzer:ParaDox/monobook/VirtualReferences.js'   
    + '&action=raw&ctype=text/javascript&dontcountme=s"><\/SCRIPT>');
=================================================================================================*/

$(function () {

    function addEvent(obj, eventName, functionName, useCaption) {
        var retVal;
        if ( obj.addEventListener ) {
            obj.addEventListener(eventName, functionName, useCaption);
            return true;
        } else {
            return false;
        }
    }
    
    function VRef_click(event) {
        // ggf. id="VirtualReferences" vor preview-upload an Quelltext anhängen
        VRefLower = VRefTextBox.value.toLowerCase();   
        VRefPos1  = VRefLower.indexOf('<ref>');
        VRefPos2  = VRefLower.indexOf('<ref ');
        if ( (VRefPos1 >= 0) || (VRefPos2 >= 0) ) {
            if ( ( VRefLower.indexOf('<references/>')  < 0 )
              && ( VRefLower.indexOf('<references />') < 0 )
               ) 
            {
                VRefTextBox.value += VRef_VirtDiv;
            }
        }
    }
    
    if ( document.getElementById('wpTextbox1') ) {

        var VRefArr, VRef_temp, VRef_FoundStr, i, VRef_neuDIV;
        var VRefTextBox, VRefLower, VRefOL;
        var VRefPos1, VRefPos2;
    
        var newLineLength     = 1;      // norm='\n', aber bspw. MSIE='\r\n'
        var VRefQuellTextOpt  = true;   // default für id="FoundReferences" zeigen
        var VRef_OL_inPreview = false;  // <ol className='references'> vorhanden?
        
        ////
        // Virtual <references/> Vorschau
        var VRef_VirtDiv = '\n<!-- Virtual <references/> by Benutzer:ParaDox/monobook/VirtualReferences.js -->\n'
            + '<div id="VirtualReferences" style="border:1px solid red; background-color:#FFEEEE;'
            + ' padding:2px 5px 0 5px;">\n'
            + '[[Benutzer:ParaDox/monobook/VirtualReferences.js|Virtual <tt><b><nowiki><references/></nowiki></b></tt>]]'
            + '<b>:</b>\n<references/></div>\n';
        ////
    
        ////
        // Einzelnachweis-DefinitionsVorschau
        var VRef_FoundDiv1 = '<div id="FoundReferences" style="border:1px solid darkgreen;'
            + ' background-color:#EEEEFF; padding:2px 5px 3px 5px;'
            + ' margin-bottom:5px;">\n'
            + '<a href="http://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style_(footnotes)" title="Wikipedia:Manual of Style (footnotes)">'
            + 'References</a> - '
            + '<a href="http://en.wikipedia.org/wiki/User:Jeff_G./monobook/VirtualReferences.js"'
            + ' title="User:Jeff G./monobook/VirtualReferences.js">'
            + 'Preview</a>:'
            ;
        var VRef_FoundDiv2 = '</div>\n';
        ////

        ////
        // init
        //
        VRefTextBox = document.getElementById('wpTextbox1');
        if ( !addEvent(document.getElementById('wpPreview'), 'click', VRef_click, false) ) {
            alert('"VirtualReferences.js" konnte nicht initialisiert werden!');
            return false;
        }
        // <ol className='references'> vorhanden?
        VRefOL = document.getElementsByTagName('OL');
        for ( i=0; i<VRefOL.length; i++ ) {
            if ( VRefOL[i].className == 'references' ) {
                VRef_OL_inPreview = true;
            }
        }
        ////

        ////
        // ggf. id="VirtualReferences" aus Quelltext entfernen,
        // und ggf. ganz unsichtbar oder hintergrund hell-grün machen
        VRefPos1 = VRefTextBox.value.indexOf('<!-- Virtual <references/> by Benutzer:ParaDox/monobook/VirtualReferences.js -->');
        // nicht in der ScriptSeite bitte :-))
        VRefPos2 = document.location.href.indexOf('Benutzer:ParaDox/monobook/VirtualReferences.js');
        //
        if ( (VRefPos1 >= 0) && (VRefPos2 == -1) ) {
            if ( VRefTextBox.value.indexOf('\r\n') >= 0 ) {
                newLineLength = 2;
            } else if ( VRefTextBox.value.indexOf('\n\r') >= 0 ) {
                newLineLength = 2;
            }
            VRefTextBox.value = VRefTextBox.value.substr(0, VRefPos1 - newLineLength );
            if ( document.getElementById('VirtualReferences') ) {
                VRefOL = document.getElementById('VirtualReferences').getElementsByTagName('OL');
                VRefQuellTextOpt = false;
                if ( VRefOL.length < 1 ) {
                    // id='VirtualReferences' ist quasi leer (evtl. wegen "ungültiger"-refs?)
                    document.getElementById('VirtualReferences').style.display = 'none';
                } else { 
                    if ( VRefOL[0].className == 'references' ) {
                        document.getElementById('VirtualReferences').style.backgroundColor = '#EEFFEE'
                    }
                }
            }
        }
        ////

        ////
        // ggf. Einzelnachweis-DefinitionsVorschau
        if ( document.location.href.indexOf('Benutzer:ParaDox/monobook/VirtualReferences.js') < 0 ) {
            VRefArr = VRefTextBox.value.match(/<ref.+?\/ref>/gi);
            if ( VRefArr && VRefQuellTextOpt && !VRef_OL_inPreview ) {
                VRef_FoundStr = '<ul style="font-size:8.25pt; line-height:9.75pt; font-family:Sans-serif;">'
                for ( i=0; i<VRefArr.length; i++ ) {
                    VRef_temp = VRefArr[i];
                    VRef_temp = VRef_temp.replace(/</g, '&lt;');
                    VRef_temp = VRef_temp.replace(/>/g, '&gt;');
                    VRef_FoundStr += '<li><tt>' 
                        + ('00' + String(i+1)).substr(-2)
                        + '</tt>: ' + VRef_temp + '</li>\n';
                }
                VRef_neuDIV = document.createElement('div');
                if ( document.getElementById('toolbar') ) {
                    document.getElementById('bodyContent').insertBefore(VRef_neuDIV, document.getElementById('toolbar'));
                } else {
                    document.getElementById('editform').insertBefore(VRef_neuDIV, document.getElementById('wpTextbox1'));
                }
                VRef_neuDIV.id = 'VRef_neuDIV';
                document.getElementById('VRef_neuDIV').innerHTML = VRef_FoundDiv1 
                    + VRef_FoundStr + '</ul>\n' + VRef_FoundDiv2;
            }
        }
        ////
    }
});