Jump to content

User:FT2/scripts/previewrefs.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/VirtualReferences.js]] V0.02b
 
Firefox 2.0.0.12 tested
    [[de:Benutzer:ParaDox/VirtualReferences.js]]
 
More info:
    [[de:Benutzer_Diskussion:ParaDox/VirtualReferences.js]]
 
By [[Benutzer:ParaDox]]
    [[de:Benutzer:ParaDox]]
 
---------------------------------------------------------------------------------------------------
 
Translation note:
 
   "Vorschau zeigen" = "Show preview"
   "Nur die folgenden 4 Zeilen in die eigene monobook.js einfügen:" = 
                      "This is what you put in monobook.js to enable the script"
 
In English Wikipedia, add this to your monobook.js to use the ENWP version of the script:
    importScript('User:FT2/scripts/previewrefs.js');
 
=================================================================================================*/
 
 
var VRefStr0 = '<!-- Start of Preview References -->';
var VRefStr = '\n' + VRefStr0 + '\n'
    + '<div id="PreviewReferences" style="border:1px solid red; background-color:#FFEEEE;padding:2px 5px 0 5px;">\n'
    + '<tt>Preview <nowiki><references/></nowiki></tt> '
    + '(based on [[:de:Benutzer:ParaDox/VirtualReferences.js|VirtualReferences.js]] by [[:de:Benutzer:ParaDox|:de:User:ParaDox]]):\n'
    + '<references/></div>\n';
var VRefBox, VRefLower, VRefOL;
var VRefPos1, VRefPos2;
 
 
window.addEventListener('click', function(event) {
	if ( (event.target.value == 'Show preview') && ( event.target.id == 'wpPreview') ) {
        if ( document.getElementById('wpTextbox1') ) {
            VRefBox   = document.getElementById('wpTextbox1');
            VRefLower = VRefBox.value.toLowerCase();   
            VRefPos1  = VRefLower.indexOf('<ref>');
            VRefPos2  = VRefLower.indexOf('<ref ');
            if ( (VRefPos1 >= 0) || (VRefPos2 >= 0) ) {
                if ( ( VRefLower.indexOf('<references/>')  < 0 )
                  && ( VRefLower.indexOf('<references />') < 0 )
                   ) 
                {
                    VRefBox.value += VRefStr;
                }
            }
        }
	}
}, true);
 
window.addEventListener('load', function(event) {
    if ( document.getElementById('wpTextbox1') ) {
        VRefBox = document.getElementById('wpTextbox1');
        VRefPos1 = VRefBox.value.indexOf(VRefStr0) - 1;
        VRefPos2 = VRefBox.value.indexOf("var VRefStr0 = '" + VRefStr0);
 
 
/* Debug code and a bugfix workaround hack
alert("Page URL (NB using 2nd method): " + window.location + "\nand: " + self.location.href);
alert("Regex trial: " + self.location.href.match("title=.+\.js&action=(edit|submit)") );
alert("Full string search result: " + VRefBox.value.indexOf(VRefStr) );
alert("Start token searchresult: " + VRefBox.value.indexOf(VRefStr0) );
alert("Full text box contents on loading:\n\n>" + VRefBox.value + '<' );
 
if ( VRefBox.value.indexOf(VRefStr) >= 0 ) {
alert("Proposed writeback based on full string search:\n\n>" + VRefBox.value.substr(0, VRefBox.value.indexOf(VRefStr)) + "<" );
}
if ( VRefBox.value.indexOf(VRefStr0) >= 0 ) {
alert("Proposed writeback based on start token search:\n\n>" + VRefBox.value.substr(0, VRefBox.value.indexOf(VRefStr0)) + "<" );
}

// End of debug code */

        if ( VRefPos1 >= 0 && VRefPos2 < 0 ) {
            VRefBox.value = VRefBox.value.substr(0, VRefPos1);
            if ( document.getElementById('PreviewReferences') ) {
                VRefOL = document.getElementById('PreviewReferences').getElementsByTagName('OL');
                if ( VRefOL.length < 1 ) {
                    document.getElementById('PreviewReferences').style.display = 'none';
                } else { 
                    if ( VRefOL[0].className == 'references' ) {
                        document.getElementById('PreviewReferences').style.backgroundColor = '#EEFFEE'
                    }
                }
            }
        }
    }
}, false);