Can't click a button using javascript without mouse pointer hovering the element

381 views
Skip to first unread message

ari fazola

unread,
Feb 19, 2020, 2:47:52 PM2/19/20
to Chromium Extensions
So i'm developing an extension to do an automate task on a website. But i'm having a trouble because my script can't click a single button on that web. I do believe for security reason or something, i can't click the buttons without actual mouse pointer hovering the button and trigger left click manually. Is there any way to simulate that?

Here is my first script 
//function goToActivityTab() {
 //  var activityTab = document.getElementsByClassName("btn-standard")[0];
//}

//goToActivityTab();

that simply just click an element. 

And here is my second script that i get from a forum

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
}

simulate(document.getElementsByClassName("btn-standart")[0], "click");

That second script tries to move the mouse on a specific X and Y, and then click it. But still not working.


ari fazola

unread,
Feb 19, 2020, 2:50:03 PM2/19/20
to Chromium Extensions
FYI, i use those two scripts on different site, and it can click button.

Hr Gwea

unread,
Feb 24, 2020, 7:25:32 PM2/24/20
to Chromium Extensions

On Wednesday, February 19, 2020 at 4:47:52 PM UTC-3, ari fazola wrote:
I do believe for security reason or something, i can't click the buttons without actual mouse pointer hovering the button and trigger left click manually.

Not true. Extensions can trigger a click on any element just as you can do it with Javascript on any page.

document.querySelector('.btn-standard').click() ;

Sarsaparilla Sunset

unread,
Feb 24, 2020, 8:18:30 PM2/24/20
to Chromium Extensions
It's possible that the website checks the isTrusted flag on the mouse event before taking action, explicitly to prevent scripting of their website
Reply all
Reply to author
Forward
0 new messages