Difference between revisions of "User:Moussekateer/vector.js"

From Team Fortress Wiki
Jump to: navigation, search
m
m (fix POST requests)
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
importScriptURI('https://raw.github.com/Moussekateer/TFWiki-scripts/master/spambot_killer.js', 'text/javascript');
+
//// START SPAMBOT KILLER ///
  
importScriptURI('https://raw.github.com/Moussekateer/TFWiki-scripts/master/wikifi_toolbox.js', 'text/javascript');
+
// create mw API object for wiki queries
 +
var api = new mw.Api();
  
 +
function deletePage(title) {
 +
    // query to get deletetoken
  
/*!
+
    var promise = api.get({ action: 'query', prop: 'info', intoken: 'delete', titles: title });
* jQuery UI Touch Punch 0.2.2
 
*
 
* Copyright 2011, Dave Furfero
 
* Dual licensed under the MIT or GPL Version 2 licenses.
 
*
 
* Depends:
 
*  jquery.ui.widget.js
 
*  jquery.ui.mouse.js
 
*/
 
(function ($) {
 
  
  // Detect touch support
+
    promise.then(function (res) {
  $.support.touch = 'ontouchend' in document;
+
        console.log('LOG: deletetokenres=' + res);
 +
        var pages = res.query.pages;
  
  // Ignore browsers without touch support
+
        for (var key in pages) {
  if (!$.support.touch) {
+
            var deletetoken = pages[key].deletetoken;
    return;
 
  }
 
  
  var mouseProto = $.ui.mouse.prototype,
+
            // use deletetoken in POST request to delete page
      _mouseInit = mouseProto._mouseInit,
+
            api.post({ action: 'delete', title: title, reason: 'Spam', token: deletetoken }).then(function (res) {
      touchHandled;
+
                console.log('LOG: deleted=' + title);
 +
            });
 +
        }
 +
    });
 +
}
  
  /**
+
function killContributions(contribs) {
  * Simulate a mouse event based on a corresponding touch event
+
    console.log('LOG: usercontribs=' + contribs);
  * @param {Object} event A touch event
 
  * @param {String} simulatedType The corresponding mouse event
 
  */
 
  function simulateMouseEvent (event, simulatedType) {
 
  
     // Ignore multi-touch events
+
     for (var edit in contribs) {
    if (event.originalEvent.touches.length > 1) {
+
         // only delete contribution if new page
      return;
 
    }
 
 
 
    event.preventDefault();
 
 
 
    var touch = event.originalEvent.changedTouches[0],
 
         simulatedEvent = document.createEvent('MouseEvents');
 
   
 
    // Initialize the simulated mouse event using the touch event's coordinates
 
    simulatedEvent.initMouseEvent(
 
      simulatedType,    // type
 
      true,            // bubbles                   
 
      true,            // cancelable               
 
      window,          // view                     
 
      1,                // detail                   
 
      touch.screenX,    // screenX                   
 
      touch.screenY,    // screenY                   
 
      touch.clientX,    // clientX                   
 
      touch.clientY,    // clientY                   
 
      false,            // ctrlKey                   
 
      false,            // altKey                   
 
      false,            // shiftKey                 
 
      false,            // metaKey                   
 
      0,                // button                   
 
      null              // relatedTarget             
 
    );
 
 
 
    // Dispatch the simulated event to the target element
 
    event.target.dispatchEvent(simulatedEvent);
 
  }
 
 
 
  /**
 
  * Handle the jQuery UI widget's touchstart events
 
  * @param {Object} event The widget element's touchstart event
 
  */
 
  mouseProto._touchStart = function (event) {
 
 
 
    var self = this;
 
 
 
    // Ignore the event if another widget is already being handled
 
    if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
 
      return;
 
    }
 
 
 
    // Set the flag to prevent other widgets from inheriting the touch event
 
    touchHandled = true;
 
 
 
    // Track movement to determine if interaction was a click
 
    self._touchMoved = false;
 
 
 
    // Simulate the mouseover event
 
    simulateMouseEvent(event, 'mouseover');
 
 
 
    // Simulate the mousemove event
 
    simulateMouseEvent(event, 'mousemove');
 
 
 
    // Simulate the mousedown event
 
    simulateMouseEvent(event, 'mousedown');
 
  };
 
  
  /**
+
        if ('new' in contribs[edit]) {
  * Handle the jQuery UI widget's touchmove events
+
            console.log('LOG: usercontribtodelete=' + contribs[edit]);
  * @param {Object} event The document's touchmove event
 
  */
 
  mouseProto._touchMove = function (event) {
 
  
    // Ignore event if not handled
+
            deletePage(contribs[edit].title);
    if (!touchHandled) {
+
        }
      return;
 
 
     }
 
     }
 +
}
  
     // Interaction was not a click
+
function blockUser(user) {
     this._touchMoved = true;
+
     // query to get blocktoken
 
+
     var promise = api.get({ action: 'query', prop: 'info', intoken: 'block', titles: 'User:' + user });
    // Simulate the mousemove event
 
    simulateMouseEvent(event, 'mousemove');
 
  };
 
  
  /**
+
    promise.then(function (res) {
  * Handle the jQuery UI widget's touchend events
+
        console.log('LOG: blocktokenres=' + res);
  * @param {Object} event The document's touchend event
+
        var pages = res.query.pages;
  */
 
  mouseProto._touchEnd = function (event) {
 
  
    // Ignore event if not handled
+
        for (key in pages){
    if (!touchHandled) {
+
            // use blocktoken in POST request to block user
      return;
+
            var blocktoken = pages[key].blocktoken;
    }
+
            console.log('LOG: blocktoken=' + blocktoken);
  
    // Simulate the mouseup event
+
            api.post({ action: 'block', user: user, expiry: 'never', nocreate: '', autoblock: '', reason: 'Spamming links to external sites', token: blocktoken }).then(function (res) {
    simulateMouseEvent(event, 'mouseup');
+
                console.log('LOG: blocked=' + user);
 +
            });
 +
        }
 +
    });
 +
}
  
     // Simulate the mouseout event
+
function killUser(user) {
     simulateMouseEvent(event, 'mouseout');
+
     // query user details
 +
     var promise = api.get({ action: 'query', list: 'usercontribs', ucuser: user });
  
     // If the touch interaction did not move, it should trigger a click
+
     promise.then(function (res) {
    if (!this._touchMoved) {
+
        var userContributions = res.query.usercontribs;
  
      // Simulate the click event
+
        if (userContributions.length > 5){
      simulateMouseEvent(event, 'click');
+
            alert('User:' + user + ' has more than 5 edits');
    }
+
        } else {
 +
            // hit it doc
 +
            blockUser(user);
 +
            killContributions(userContributions);
  
    // Unset the flag to allow other widgets to inherit the touch event
+
            // vas gud
     touchHandled = false;
+
            alert('User:' + user + ' has been terminated. Good day');
  };
+
        }
 +
     });
 +
}
  
  /**
+
function pootSecretSauce() {
  * A duck punch of the $.ui.mouse _mouseInit method to support touch events.
+
     // insert blockdelete link in new page revision details
  * This method extends the widget with bound touch event handlers that
+
     $('.mw-usertoollinks a:last-child').after(" | <a href=# id='blockdelete'>blockdelete</a>");
  * translate touch events to mouse events and pass them to the widget's
 
  * original mouse event handling methods.
 
  */
 
  mouseProto._mouseInit = function () {
 
      
 
     var self = this;
 
  
     // Delegate the touch handlers to the widget's element
+
     $('a#blockdelete').click(function() {
    self.element
+
        // grab username from revision details
      .bind('touchstart', $.proxy(self, '_touchStart'))
+
        var user = $('.diff-ntitle .mw-userlink').text();
      .bind('touchmove', $.proxy(self, '_touchMove'))
+
        console.log('LOG: User=' + user);
      .bind('touchend', $.proxy(self, '_touchEnd'));
 
  
    // Call the original $.ui.mouse init method
+
        killUser(user);
     _mouseInit.call(self);
+
     });
  };
+
}
  
})(jQuery);
+
$(pootSecretSauce);
  
$(document).ready(function() {
+
/// END SPAMBOT KILLER ///
  $('.viewer-3d').draggable();
 
});
 

Latest revision as of 23:37, 3 July 2016

//// START SPAMBOT KILLER ///

// create mw API object for wiki queries
var api = new mw.Api();

function deletePage(title) {
    // query to get deletetoken

    var promise = api.get({ action: 'query', prop: 'info', intoken: 'delete', titles: title });

    promise.then(function (res) {
        console.log('LOG: deletetokenres=' + res);
        var pages = res.query.pages;

        for (var key in pages) {
            var deletetoken = pages[key].deletetoken;

            // use deletetoken in POST request to delete page
            api.post({ action: 'delete', title: title, reason: 'Spam', token: deletetoken }).then(function (res) { 
                console.log('LOG: deleted=' + title);
            });
        }
    });
}

function killContributions(contribs) {
    console.log('LOG: usercontribs=' + contribs);

    for (var edit in contribs) {
        // only delete contribution if new page

        if ('new' in contribs[edit]) {
            console.log('LOG: usercontribtodelete=' + contribs[edit]);

            deletePage(contribs[edit].title);
        }
    }
}

function blockUser(user) {
    // query to get blocktoken
    var promise = api.get({ action: 'query', prop: 'info', intoken: 'block', titles: 'User:' + user });

    promise.then(function (res) {
        console.log('LOG: blocktokenres=' + res);
        var pages = res.query.pages;

        for (key in pages){
            // use blocktoken in POST request to block user
            var blocktoken = pages[key].blocktoken;
            console.log('LOG: blocktoken=' + blocktoken);

            api.post({ action: 'block', user: user, expiry: 'never', nocreate: '', autoblock: '', reason: 'Spamming links to external sites', token: blocktoken }).then(function (res) {
                console.log('LOG: blocked=' + user);
            });
        }
    });
}

function killUser(user) {
    // query user details
    var promise = api.get({ action: 'query', list: 'usercontribs', ucuser: user });

    promise.then(function (res) {
        var userContributions = res.query.usercontribs;

        if (userContributions.length > 5){
            alert('User:' + user + ' has more than 5 edits');
        } else {
            // hit it doc
            blockUser(user);
            killContributions(userContributions);

            // vas gud
            alert('User:' + user + ' has been terminated. Good day');
        }
    });
}

function pootSecretSauce() {
    // insert blockdelete link in new page revision details
    $('.mw-usertoollinks a:last-child').after(" | <a href=# id='blockdelete'>blockdelete</a>");

    $('a#blockdelete').click(function() {
        // grab username from revision details
        var user = $('.diff-ntitle .mw-userlink').text();
        console.log('LOG: User=' + user);

        killUser(user);
    });
}

$(pootSecretSauce);

/// END SPAMBOT KILLER ///