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.