0

The problem I'm facing is fairly simple but hard to solve. I am trying to create a script that will play chess for me using an engine that decides which piece (rook, bishop , king , queen)I need to move where.

How to move a tool? Moving a tool is done by dragging which is my main problem right now I am not able to drag a piece to its place.

I have tried the following sequence in my code:

mouseOver(sq1elem);
mouseDown(sq1elem);
mouseOver(sq2elem);
mouseUp(sq2elem);

Those function are using another function called triggerMouseEvent which is taken from the following question : Simulating a mousedown, click, mouseup sequence in Tampermonkey?

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
};

in my code sq1elem is an HTML element pointing to the source square (Where i want to move the piece from) sq2elem is the destination square

the mouse functions:

function mouseDown(node){
    triggerMouseEvent (node, "mousedown");
};

function mouseOver(node){
    triggerMouseEvent (node, "mouseover");
};

function mouseUp(node){
    triggerMouseEvent (node, "mouseup");
};

The website I'm testing this on is called : http://multiplayerchess.com/ when right clicking and looking at the Event Listeners in the Elements tab I can only see the following events:

  • click
  • contextmenu
  • error
  • load
  • mousedown
  • mouseup
  • touchstart

And by taking a look at the Sources tab I could also see that there are touchend, touchmove and mousemove events

each one of them calls a function: touchend and mouseup calls drop which is:

function drop(eventArgs){
  preventEvent(eventArgs);
  callback && callback(eventArgs);
  select();
}

while touchmove and mousemove are calling move:

function move(eventArgs){
  if(!selection || !callback){
    return;
  }

  callback(eventArgs);
  select();
}

My question comes to this , how will I be able to simulate the drag with those events only since I don't have any other events to deal with and how will I be able to "give" the drag where to drop or where to start from with the elements only?

My ultimate goal is a function that is given 2 arguments to work with, 2 elements. In that function should be code to start dragging from element1 (first argument) to element2 (second argument)

Thank you for your time.

EDIT :

I have managed to call the move and the drop functions using the following functions:

function mouseDragStart(node){
    console.log("Starting drag...");
    triggerMouseEvent(node, "touchstart")//change to mouseDown if doesn't work
}

function mouseDragEnd(node){
    console.log("Ending drag...");
    triggerMouseEvent(node, "touchmove");
    triggerMouseEvent(node,  "touchend");
}

Nothing happens though , the eventArgs being passed to the function is MouseEvent but the clientX, clientY and some other fields remain remain empty , could it be the reason for it not working?

Here is how I use it in my code:

mouseDragStart(sq1elem);
mouseDragEnd(sq2elem);

again sq1elem and sq2elem are elements which sq1elem is the element i want to drag and sq2elem is where i want to drag it to.

The problem seems to be that the element is dragged to the top left corner of the screen and not the right place, by setting a breakpoint in the drop function which occurs directly after the move function i can now see that the element isn't in the right place which is why it doesn't work.

Is there anyway i can get the elements coordinates and send them to dispatchEvent so the drag will finish in the right spot?

Thank you for your time.

Community
  • 1
  • 1
NotGI
  • 458
  • 9
  • 21
  • don't simulate the drag, call the program's handler for drag events directly, passing the info they need. – dandavis Apr 11 '15 at 23:04
  • they need the EventsArgs with clientX and clientY and I don't quite know gow to get them using my info which is only those two elements – NotGI Apr 12 '15 at 09:06

1 Answers1

0

SOLVED: By using the specified events i was able to use a few functions to simulate the drag:

function simulate(element, eventName)
{
    var options = extend(defaultOptions, arguments[2] || {});
    var oEvent, eventType = null;

    for (var name in eventMatchers)
    {
        if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
        throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent)
    {
        oEvent = document.createEvent(eventType);
        if (eventType == 'HTMLEvents')
        {
            oEvent.initEvent(eventName, options.bubbles, options.cancelable);
        }
        else
        {
            oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
            options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
            options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
        }
        element.dispatchEvent(oEvent);
    }
    else
    {
        options.clientX = options.pointerX;
        options.clientY = options.pointerY;
        var evt = document.createEventObject();
        oEvent = extend(evt, options);
        element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}

function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
}

var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
}

This function is taken from another question which i can't find it right now.(sorry.)

here's how i used it:

function mouseDragStart(node){
    console.log("Starting drag...");
    //console.log(node.offsetTop);
    //console.log(node.offsetLeft);
    triggerMouseEvent(node,"mousedown")
}

function mouseDragEnd(node){
    console.log("Ending drag...");
    //console.log(node.offsetTop);
    //console.log(node.offsetLeft);
    simulate(node, "mousemove" , {pointerX: node.offsetLeft + 5 , pointerY: node.offsetTop + 5})
    simulate(node, "mouseup" , {pointerX:  node.offsetLeft + 5 , pointerY: node.offsetTop + 5})
}

I was able to simulate a mousedrag using those event that works only on elements.

NotGI
  • 458
  • 9
  • 21