Search code examples
javascriptgoogle-chrome-extensiondom-events

How to capture Mouse and Keyboard data across all websites for Chrome


Just as the title implies. How would I go about capturing general event data across websites? This is supposed to be a strictly client-side application. Would a chrome extension work? If so, how would I go about it? Does one already exist, or at least an alternative method?


Solution

  • In chrome extension, Content scripts are scripts that run in the context of web pages. You could listent to specific events (such as MouseEvent and KeyboardEvent ) just like normal scripts.

    manifest.json

    {
      "manifest_version": 2,
      "name": "Test extension",
      "version": "1.0",
      "content_scripts": [
        {
          "matches": [
            "<all_urls>"
          ],
          "js": [
            "content.js"
          ],
          "all_frames": true
        }
      ]
    } 
    

    content.js

    document.addEventListener('mousedown', function (e) {
        console.log('mouse down');
    }, false);
    
    document.addEventListener('keydown', function (e) {
        console.log('key down');
    }, false);