Uit Hack42
Ga naar: navigatie, zoeken
k (Onderstaande wijzigingen in oktober 2022: wgUserName en meer wgVariabelen moeten expliciet opgehaald worden met mw.config.get, zitten niet meer in top scope)
 
(48 tussenliggende versies door 4 gebruikers niet weergegeven)
Regel 1: Regel 1:
/* JavaScript die hier wordt geplaatst heeft invloed op alle pagina's voor alle gebruikers */
+
/* JavaScript die hier wordt geplaatst heeft invloed op alle pagina's voor alle gebruikers! */
 
jQuery().ready(function() {
 
jQuery().ready(function() {
 +
    euros();
 +
    createiFrameMap();
 
     getEditToken();
 
     getEditToken();
     user = wgUserName;
+
     user = mw.config.get( 'wgUserName' );
 +
    pagename = mw.config.get( 'wgPageName' );
 
     if (user) {
 
     if (user) {
 
         var titles = jQuery('#Ik_kom_wel,#Ik_kom_niet,#Ik_kom_misschien,#Animo,#Aanwezig,#Afwezig,#Wie,#Wie_niet,#Wie_misschien');
 
         var titles = jQuery('#Ik_kom_wel,#Ik_kom_niet,#Ik_kom_misschien,#Animo,#Aanwezig,#Afwezig,#Wie,#Wie_niet,#Wie_misschien');
Regel 19: Regel 22:
 
     }
 
     }
 
});
 
});
 +
 +
function createiFrameMap(){
 +
  my_div = document.getElementById("iframe_example");
 +
  if (my_div === undefined || my_div === null)
 +
    return;
 +
  var ifrm = document.createElement("iframe");
 +
  ifrm.setAttribute("src", "https://hack42.nl/");
 +
  ifrm.style.width = "640px";
 +
  ifrm.style.height = "480px";
 +
  my_div.appendChild(ifrm);
 +
}
 +
 +
function euros(){
 +
    var body = document.body;
 +
    var textContent = body.textContent || body.innerText;
 +
 +
    var myRegex = /(€)[0-9]{0,4}(,?\.?[0-9]{0,2})?/g;
 +
    var myAmount = textContent.match(myRegex);
 +
 +
    if (myAmount){
 +
   
 +
    var myValue = 0.00;
 +
    for(var i=0; i<myAmount.length; i++){
 +
        myValue += parseFloat(myAmount[i].replace(",",".").replace("€",""));
 +
    }
 +
 +
    if (document.getElementById("euros")){
 +
        document.getElementById("euros").innerHTML = "€" + (Math.round(myValue * 100) / 100);
 +
    }
 +
    }
 +
};
  
 
function getEditToken() {
 
function getEditToken() {
 
     jQuery.getJSON(
 
     jQuery.getJSON(
         wgScriptPath + '/api.php?',
+
         '/mediawiki/api.php?',
 
         {
 
         {
 
             action: 'query',
 
             action: 'query',
Regel 43: Regel 77:
 
     var button = jQuery('<input type="button" value="'+user+'"/>');
 
     var button = jQuery('<input type="button" value="'+user+'"/>');
 
     var textField = jQuery('<input type="text" value="+soep, oid" />');
 
     var textField = jQuery('<input type="text" value="+soep, oid" />');
     $ul.append('<li>').append(button, textField);
+
     $ul.append(jQuery('<li>').append(button, textField));
  
 
     button.click(function() {
 
     button.click(function() {
         var comment = textField.attr('value');
+
         var comment = textField.val();
 
         if (comment == '+soep, oid')
 
         if (comment == '+soep, oid')
 
             comment = '';
 
             comment = '';
 
         ajaxSubmitUserAndComment($title, comment);
 
         ajaxSubmitUserAndComment($title, comment);
 
     });
 
     });
 +
    textField.keypress(function (e) {
 +
      if (e.which == 13) {
 +
        var comment = textField.val();
 +
        if (comment == '+soep, oid')
 +
            comment = '';
 +
        ajaxSubmitUserAndComment($title, comment);
 +
    }
 +
});
  
 
     textField
 
     textField
Regel 65: Regel 107:
  
 
function ajaxSubmitUserAndComment($title, comment) {
 
function ajaxSubmitUserAndComment($title, comment) {
     var section_id = $title.children('span a').attr('href').match(/section=([0-9]+)/)[1];
+
     var $editButton = $title.find('span a');
    jQuery.ajax({
+
    if ($editButton.length) {
        url: wgScriptPath + '/api.php',
+
        var section_id = $editButton.attr('href').match(/section=([0-9]+)/)[1];
        data: {
+
        jQuery.ajax({
            format: 'json',
+
            url: '/mediawiki/api.php',
            action: 'edit',
+
            data: {
            title: wgPageName,
+
                format: 'json',
            minor: true,
+
                action: 'edit',
            summary: 'Naam toegevoegd via button',
+
                title: pagename,
            section: section_id,
+
                minor: true,
            appendtext: '\n* {{Mentioneduserbutton|' + wgUserName + '}}'  + comment,
+
                summary: 'Naam toegevoegd via button',
            token: wgEditToken
+
                section: section_id,
        },
+
                appendtext: '\n* {{Mentioneduserbutton|' + user + '}}'  + ' ' + comment,
        dataType: 'json',
+
                token: wgEditToken
        type: 'POST',
+
            },
        success: function(data) {
+
            dataType: 'json',
            window.location.href = window.location.href + '?' + new Date().getTime();
+
            type: 'POST',
            window.location.reload(true);
+
            success: function(data) {
        },
+
                window.location.href = window.location.href + '?' + new Date().getTime();
        error: function(xhr) {
+
                window.location.reload(true);
            alert('Error: iets is mis gegaan, herlaad pagina en probeer opnieuw.');
+
            },
         }
+
            error: function(xhr) {
     });
+
                alert('Error: iets is mis gegaan, herlaad pagina en probeer opnieuw.');
 +
            }
 +
         });
 +
     } else {
 +
        alert('Error: iets is mis gegaan, herlaad pagina en probeer opnieuw.');
 +
    }
 
}
 
}

Huidige versie van 12 okt 2022 om 19:53

/* JavaScript die hier wordt geplaatst heeft invloed op alle pagina's voor alle gebruikers! */
jQuery().ready(function() {
    euros();
    createiFrameMap();
    getEditToken();
    user = mw.config.get( 'wgUserName' );
    pagename = mw.config.get( 'wgPageName' );
    if (user) {
        var titles = jQuery('#Ik_kom_wel,#Ik_kom_niet,#Ik_kom_misschien,#Animo,#Aanwezig,#Afwezig,#Wie,#Wie_niet,#Wie_misschien');
        titles.each(function() {
            var $title = jQuery(this).parent();
            var $siblings = $title.nextUntil('h1,h2,h3,h4,h5,h6');
            var $ul = $siblings.filter('ul:first');
            var alreadyInList = $ul.find('li a:contains("'+user+'")').length > 0;
            if ( ! alreadyInList) {
                if ( ! $ul.length) {
                    $ul = jQuery('<ul>').insertAfter($title);
                }
                addAddUserButton($title, $ul);
            }
        });
    }
});

function createiFrameMap(){
  my_div = document.getElementById("iframe_example");
  if (my_div === undefined || my_div === null)
    return;
  var ifrm = document.createElement("iframe");
  ifrm.setAttribute("src", "https://hack42.nl/");
  ifrm.style.width = "640px";
  ifrm.style.height = "480px";
  my_div.appendChild(ifrm);
}

function euros(){
    var body = document.body;
    var textContent = body.textContent || body.innerText;

    var myRegex = /(€)[0-9]{0,4}(,?\.?[0-9]{0,2})?/g;
    var myAmount = textContent.match(myRegex);

    if (myAmount){
    
    var myValue = 0.00;
    for(var i=0; i<myAmount.length; i++){
         myValue += parseFloat(myAmount[i].replace(",",".").replace("€",""));
    }

    if (document.getElementById("euros")){
        document.getElementById("euros").innerHTML = "€" + (Math.round(myValue * 100) / 100);
    }
    }
};

function getEditToken() {
    jQuery.getJSON(
        '/mediawiki/api.php?',
        {
            action: 'query',
            prop: 'info',
            intoken: 'edit',
            titles: 'Main Page',
            indexpageids: '',
            format: 'json'
        },
        function( data ) {
            if ( data.query.pages && data.query.pageids ) {
                var pageid = data.query.pageids[0];
                wgEditToken = data.query.pages[pageid].edittoken;
            }
        }
    )
}

function addAddUserButton($title, $ul) {
    var button = jQuery('<input type="button" value="'+user+'"/>');
    var textField = jQuery('<input type="text" value="+soep, oid" />');
    $ul.append(jQuery('<li>').append(button, textField));

    button.click(function() {
        var comment = textField.val();
        if (comment == '+soep, oid')
            comment = '';
        ajaxSubmitUserAndComment($title, comment);
    });
    textField.keypress(function (e) {
      if (e.which == 13) {
        var comment = textField.val();
        if (comment == '+soep, oid')
            comment = '';
        ajaxSubmitUserAndComment($title, comment);
    }
});

    textField
        .css({color: 'lightgray'}) // TODO: use CSS class instead?
        .focus(function() {
            if (this.value == '+soep, oid')
                jQuery(this).val('').css({color: 'black'});
        })
        .blur(function() {
            if (this.value == '')
                jQuery(this).val('+soep, oid').css({color: 'lightgray'});
        });
}

function ajaxSubmitUserAndComment($title, comment) {
    var $editButton = $title.find('span a');
    if ($editButton.length) {
        var section_id = $editButton.attr('href').match(/section=([0-9]+)/)[1];
        jQuery.ajax({
            url: '/mediawiki/api.php',
            data: {
                format: 'json',
                action: 'edit',
                title: pagename,
                minor: true,
                summary: 'Naam toegevoegd via button',
                section: section_id,
                appendtext: '\n* {{Mentioneduserbutton|' + user + '}}'  + ' ' + comment,
                token: wgEditToken
            },
            dataType: 'json',
            type: 'POST',
            success: function(data) {
                window.location.href = window.location.href + '?' + new Date().getTime();
                window.location.reload(true);
            },
            error: function(xhr) {
                alert('Error: iets is mis gegaan, herlaad pagina en probeer opnieuw.');
            }
        });
    } else {
        alert('Error: iets is mis gegaan, herlaad pagina en probeer opnieuw.');
    }
}