Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension to control mouse/keyword

Given that you cannot do this in JavaScript due to the security implications. Is there any other type of Google Extension/development API that will allow me to do this from Chrome?

like image 467
Cheetah Avatar asked Feb 15 '13 14:02

Cheetah


1 Answers

Yes, there is a Pointer Lock API to control mouse, check this code as a reference for Pointer Lock API.

Demonstration

Go to http://www.google.co.in/ and click on Logo of Google, from them you can customize this code to control mouse movements and actions.

manifest.json

Add a content script and put all permissions for the page.

{
    "name":"Pointer Lock",
    "description":"http://stackoverflow.com/questions/14896728/chrome-extension-to-control-mouse-keyword",
    "version":"1",
    "manifest_version":2,
    "content_scripts": [
        {
          "matches": ["http://www.google.co.in/"],
          "js": ["myscript.js"]
        }
    ],
    "permissions":["<all_urls>"]
}

myscript.js

This code locks pointer movement.

//Check whether browser supports locking or not
var havePointerLock = 'webkitPointerLockElement' in document;
//Get some random element in http://www.google.co.in/ page
var element = document.getElementById("hplogo");
//Bind an event Listener
element.addEventListener("click", function () {
    if (havePointerLock) {
        // Ask the browser to lock the pointer
        element.requestPointerLock = element.webkitRequestPointerLock;
        element.requestPointerLock();
        //Register lock change callback
        document.addEventListener('webkitpointerlockchange', changeCallback, false);
        //Register callback for all errors
        document.addEventListener('webkitpointerlockerror', errorCallback, false);
    } else {
        alert("Your browser does not support Pointer Lock, Please Upgrade it");
    }
});

function moveCallback(e) {
    //Track all mouse movements
    console.log("Mouse Moved ...");
    console.log(e.webkitMovementX);
    console.log(e.webkitMovementY);
}

function changeCallback() {
    //Check for element whether locked is expected element or not
    if (document.webkitPointerLockElement == element) {
        // Pointer was just locked
        // Enable the mousemove listener
        document.addEventListener("mousemove", moveCallback, false);
    } else {
        // Pointer was just unlocked
        // Disable the mousemove listener
        document.removeEventListener("mousemove", moveCallback, false);
    }
}

function errorCallback(e) {
    //Log Errors
    console.log(e);
}

Reference

  • Pointer Lock API
  • Content Scripts
like image 69
Sudarshan Avatar answered Oct 02 '22 07:10

Sudarshan