Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to click event in web page chrome extension

Am developing a chrome extension. Am injecting a script in a wbpage using contentscript as follows:

  var s = document.createElement('script');

   s.src = chrome.extension.getURL("script.js");

   (document.head||document.documentElement).appendChild(s);

My script.js:

$.post("https://www.example.com/srv/example/", function(html){


    $("body").prepend(html);


 });

Now, i would like to listen to a button click event in the webpage DOM ? How to achieve this?

like image 568
karthi Avatar asked Jan 06 '15 10:01

karthi


1 Answers

You can listen with

document.body.addEventListener('click', yourListenerFunction, true);

BTW, you're executing your content script in a very odd way. The usual is using some code in your background.js, like:

chrome.tabs.executeScript(tabId, {file:"yourScriptFile.js"});

I recommend this https://developer.chrome.com/extensions/overview

like image 149
Alejandro Silvestri Avatar answered Nov 14 '22 15:11

Alejandro Silvestri