User:Jfd34/filterlog.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.
if ( mw.config.get("wgPageName") === "Special:AbuseLog" ) {
 
  var abuse_log_entries = document.getElementById("bodyContent").getElementsByTagName("ul")[0].getElementsByTagName("li");
  // The list of log entries has no class or id, so the only way is to detect its position
  // Currently, the filter log is the first <ul> element of bodyContent
  // This may change with updates to the interface or a newer version of MediaWiki
  // Such changes may cause the script to stop working or even cause unexpcted results
  // Should anything unexpected happen in the filter log, disable this script (remove it from your common.js/skin.js subpage) and see if this fixes the problem
 
  var i;
 
  var abuse_log_user_name; 
  var abuse_log_user_tools;
  var abuse_log_page_name;
  var abuse_log_page_link;
  var abuse_log_contribs_link;
  var user_editcount_request;
  var user_editcount_xml;
  var user_editcount;
 
  function target_page_link_position(x) {
    var default_target_page_link_position = 3;
    if ( abuse_log_entries[x].innerHTML.search(/<a href\=\"\/wiki\/Special\:AbuseFilter\/[0-9]+?\"/) !== -1 ) {
      default_target_page_link_position += 1;
    };
    if ( abuse_log_entries[x].innerHTML.search(/<a href\=\"\/(wiki\/|w\/index\.php\?title\=)User\:[^#<>\[\]\|\{\}]+?(&amp;action\=edit&amp;redlink\=1)?\" class\=\"(new )?mw-userlink\"/) !== -1 ) {
      default_target_page_link_position += 1;
    };
    return default_target_page_link_position;
  };
 
  for ( i = 0; i < abuse_log_entries.length; i++ ) {
 
    // Adds an edit filter log link after the talk and contribs links of each user
 
    abuse_log_user_name = abuse_log_entries[i].getElementsByClassName("mw-userlink")[0].getAttribute("href").replace(/.*?(Special\:Contributions\/|User\:)([^#<>\[\]\|\{\}\/]+?)(&action\=edit&redlink\=1)?$/, "$2");
    // User links are to contribs page (for IP) or to user page (for account)
    // Usernames cannot contain # < > [ ] | { } / (from [[WP:NCTR]])
    // For accounts without a user page, &action=edit&redlink=1 appears at the end of the username
 
    abuse_log_user_tools = abuse_log_entries[i].getElementsByClassName("mw-usertoollinks")[0];
    abuse_log_user_tools.innerHTML = abuse_log_user_tools.innerHTML.replace(/\)$/, " | <a href=\"/w/index.php?title=Special:AbuseLog&amp;wpSearchUser=" + abuse_log_user_name + "\">filter log</a>)");
 
 
    // Adds a history link after the page name
 
    // The target page name has no class or id on it, so again, it must be found by position
    // For users who can see private filters, links to all filters are displayed
    // For others, only links to public filters are displayed, so for private filter hits there is one <a> less from the start of the log entry to the target page
    // This also depends on whether the user who triggered the filter is an IP or not, is IPs don't have a "contribs" link
 
    abuse_log_page_name = abuse_log_entries[i].getElementsByTagName("a")[target_page_link_position(i)].getAttribute("href").replace(/^\/wiki\/([^#<>\[\]\|\{\}]+?)$/, "$1");
    // Uses the target_page_link_position() function defined earlier to get the position of the link
    
    abuse_log_page_link = abuse_log_entries[i].getElementsByTagName("a")[target_page_link_position(i)];
    abuse_log_page_link.outerHTML = abuse_log_page_link.outerHTML + " (<a href=\"/w/index.php?title=" + abuse_log_page_name + "&amp;action=history\">history</a>)";

    // Shows the user's contributions as a red link if there are no edits
    // This can't be done for IPs as MediaWiki does not record their edit counts (API queries return the invalid=""attribute)
    
    // Determine the user contributions link

    abuse_log_contribs_link = abuse_log_user_tools.getElementsByTagName("a")[1];
    
    // Since user edit counts are not displayed in the log itself, they need to be retrieved using the API
    if ( window.XMLHttpRequest ) { // For compatibility with IE 6 and earlier
      user_editcount_request = new XMLHttpRequest();
    }
    else {
      user_editcount_request = new ActiveXObject("Microsoft.XMLHTTP");
    };
    
    // Get the user's edit count from the API
    user_editcount_request.open( "GET", "/w/api.php?format=xml&action=query&list=users&usprop=editcount&ususers=" + abuse_log_user_name, true);
    user_editcount_request.send();
    user_editcount_request.onreadystatechange = function() {
      if ( user_editcount_request.readyState === 4 && user_editcount_request.status === 200 ) {
        user_editcount_xml = user_editcount_request.responseXML;
        user_editcount = user_editcount_xml.getElementsByTagName("user")[0].getAttribute("editcount");
        user_editcount = new Number(user_editcount);
        
        if ( user_editcount === 0  && abuse_log_entries[i].innerHTML.search(/<a href\=\"\/(wiki\/|w\/index\.php\?title\=)User\:[^#<>\[\]\|\{\}]+?(&amp;action\=edit&amp;redlink\=1)?\" class\=\"(new )?mw-userlink\"/) !== -1 ) {
            abuse_log_contribs_link.setAttribute("className", "new");
        };
      };
    };
  };
};