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?
Yes, there is a Pointer Lock API to control mouse, check this code as a reference for Pointer Lock API.
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.
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>"]
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With