nelson687
nelson687

Reputation: 4503

Click event on chrome extension

I have a very simple question, I'm creating a chrome extension where I want to save all the interactions the user made on the site. To start I want to record all clicks that have been made. I couldn't find a chrome event to use for clicks (such as chrome.browserAction.onClicked) so I'm using normal javascript code

window.addEventListener('load', function() {
  document.addEventListener("click", function (e) {
    console.log("Clicked");
  });
});

that's what I'm doing on background.js, of course this has been registered on manifest.json, but somehow it's not working and I'm not logging anything in the console. I tried using document.addEventListener('ready'.. and just the click listener but couldn't make it work. Is there something I'm missing? thanks

Upvotes: 1

Views: 1329

Answers (1)

jakobinn
jakobinn

Reputation: 2032

If you want to save interactions the user makes on the site, you can use content scripts. Doing that allows you to interact with the websites DOM like you are trying to do.

In your manifest file:

 "content_scripts": [
    {
    "matches": [ //match patterns when the content script will be used
        "http://*/*",
        "https://*/*" 
        ],
    "js": ["content.js"],
    //specifies when the script should run
    "run_at": "document_end" 
    }
], 

Then you can create a file called content.js and copy your eventListener to that file. It should log to the browser console.

Upvotes: 1

Related Questions