1

I have written a short extension that captures my mouse movements in Chrome, and I want use those coordinates in Python as soon as Javascript has captured them, see below for the short Javascript code in the content file:

var actualCode = `
document.onmousemove = function(e){
  var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
  console.log(pageCoords);
};
`;

var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();

Currently Javascript just prints them in the console, I was thinking about printing to a text file and then make python read that, but it seems like a terribly slow process full of potential issues.

Any better way of doing transferring data from Chrome browser generated by a javascript extension to a Python script?

Edit: This is different from How to send data from JavaScript to Python as that focuses on web development, here both scripts are being run on my local machine.

no nein
  • 631
  • 3
  • 10
  • 27
  • Possible duplicate of [How to send data from JavaScript to Python](https://stackoverflow.com/questions/46889772/how-to-send-data-from-javascript-to-python) – Mike Scotty Aug 23 '18 at 09:08
  • You're gonna have a bad time because sending client data to server is not an instant operation. And seeing that you aim to send this every mouse move, you are looking to yank out double digit requests every fraction of a second. `mousemove` fires faster than Teddy Flood. Manipulating stuff via mouse interactions and the cartesian coordinate plane is a client-side work for a reason. I suggest doing this only at the end of the mouse movement. – Abana Clara Aug 23 '18 at 09:08
  • I don't know the specifics of chrome extensions, however, to be able to "listen" for data comming from "somewhere", in python (and any other language), you basically need a web server that listens for connections and passes data it to python code. For a simple start, in Python, I recommend the flask framework ( http://flask.pocoo.org/ ). You don't seem to have lots of experience in this field, but basically, you'll have to: 1) Make your chrome extension talk to your web server; 2) Make your python code on the server do something with the data, and optionally return a response – vlad-ardelean Aug 23 '18 at 09:09
  • Also i suggest to do not send data every mouse moviment, but instead add coordinates to a json and then send it every x time or x moviments. – Nico Aug 23 '18 at 09:11
  • As both are being run on my local machine is it not possible to send the information directly? It seems like taking the long way around to first send it to a server and then make python grab that – no nein Aug 23 '18 at 09:18

1 Answers1

1

I am not a Python developper but I can help you get a clue on how to get that working.

In theory you can make a POST HTTP request to a local (or remote) Python server who would get the mouse data every x seconds.

Example on how to do the client with JS (as I don't know how to do the server part with Python):

let pageCoords = [];
let isNewDataCaptured = false;

// make a request every 1s
setInterval(() => {
    // checks if the browser captured new mouse data
    if (isNewDataCaptured) {
        // create a new vanilla XHR request but can be simplified
        // with the Axios package.
        const XHR = new XMLHttpRequest();

        // listener for the request completion
        // resets everything after the data was sent to the Python server
        XHR.addEventListener( 'load', () => {
            isNewDataCaptured = false;
            pageCoords = []
        });

        // the server URL and port is an example,
        // you can replace them with whatever suits you the most
        XHR.open('POST', 'http://localhost:3000')
        XHR.send(JSON.stringify(pageCoords))
    }
}, 1000)

document.onmousemove = (e) => {
  isNewDataCaptured = true;
  pageCoords.push({
     x: e.pageX,
     y: e.pageY
  })
};

All you have to do now is figure out a way to get this data on your Python server and save them via Python as an array or whatever you like to do with them. :D

AymericM
  • 101
  • 4