1

I'm developing a very simple application, with only one button. When I press the button a WebView load the url of my router-modem ("1.1.1.1" or "192.168.1.0" or "http://vodafone.station/main.cgi?page=index.html") and it has to simulate a click on the button with id "wifi.enabled" to disable wifi. Browsing the web (stackoverflow) I found a javascript code that it's perfect to this purpose. If I load the URL of my gateway and paste the code in the js console of chrome the wifi is disabled.

Here the code (source: stackoverflow)

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
}

After I run the following command and the wifi is switched off:

    simulate(document.getElementById("wifi.enabled"), "click");

In an Android project I tried to create a webView and load the javascript code in the following way

WebView doc = new WebView(this);
doc.loadUrl("http://vodafone.station/main.cgi?page=index.html"); //Load the router URL (control panel)
doc.getSettings().setJavaScriptEnabled(true);

In the methods onClick

doc.loadUrl("javascript:" + /* ALL the javascript code above */);

But nothing happens.

I have never used javascript before (Instead I have experience with python, C/C++, Java, C#, SQL and a bit of HTML and CSS).

Thank you for your help.

Community
  • 1
  • 1
  • I remember I tried to achieve the same thing a while ago, unfortunately I didn't manage to. I think this option was deliberately disabled by android to prevent illegal automatic web-operations – Sahar Avr Sep 03 '15 at 22:33

0 Answers0