拖动JavaScript - 外部脚本

时间:2015-04-11 20:32:39

标签: javascript jquery mouseevent

我面临的问题相当简单但难以解决。 我正在尝试使用一个引擎来创建一个下棋的脚本,该引擎决定我需要移动到哪一块(车,主教,国王,女王)。

如何移动工具?移动工具是通过拖动完成的,这是我现在的主要问题,我无法将一块拖到它的位置。

我在我的代码中尝试了以下序列:

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

这些函数正在使用另一个名为triggerMouseEvent的函数,它取自以下问题: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);
};

在我的代码中 sq1elem 是一个指向源方块的HTML元素(我想从哪里移动片段) sq2elem 是目标广场

鼠标功能:

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

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

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

我正在测试的网站名为:http://multiplayerchess.com/ 右键单击并查看元素标签中的事件监听器时,我只能看到以下事件:

  • 点击
  • 文本菜单
  • 错误
  • 负载
  • 鼠标按下
  • 鼠标松开
  • touchstart

通过查看来源标签,我还可以看到 touchend touchmove mousemove 事件

他们每个人都调用一个函数: touchend和mouseup调用drop:

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

而touchmove和mousemove正在调用move:

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

  callback(eventArgs);
  select();
}

我的问题是,由于我没有任何其他事件需要处理,我将如何能够模拟这些事件的阻力,以及我如何能够“给”拖动到哪里去掉或者从哪里开始只使用元素?

我的最终目标是一个函数,它有2个参数可供使用,2个元素。 在该函数中应该是代码开始从element1(第一个参数)拖动到element2(第二个参数)

感谢您的时间。

编辑:

我已设法使用以下函数调用move和drop函数:

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");
}

但是没有任何事情发生,传递给函数的eventArgs是MouseEvent但是clientX,clientY和其他一些字段仍然是空的,这可能是它无法工作的原因吗?

以下是我在代码中使用它的方法:

mouseDragStart(sq1elem);
mouseDragEnd(sq2elem);

sq1elem和sq2elem再次是sq1elem是我要拖动的元素的元素,sq2elem是我想要拖动的元素。

问题似乎是元素被拖动到屏幕的左上角而不是正确的位置,通过在drop函数中设置一个断点,该断点在移动函数之后直接发生我现在可以看到元素是在正确的地方,这就是为什么它不起作用。

无论如何,我可以获取元素坐标并将它们发送到dispatchEvent,这样拖动就会在正确的位置完成吗?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

解决: 通过使用指定的事件,我能够使用一些函数来模拟拖动:

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
}

此功能取自另一个我现在无法找到的问题。(抱歉。)

这是我如何使用它:

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})
}

我能够使用那些仅适用于元素的事件来模拟mousedrag。