收听网页chrome扩展中的点击事件

kar*_*thi 5 code-injection google-chrome-extension

正在开发 chrome 扩展。我使用 contentscript 在 wbpage 中注入脚本,如下所示:

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

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

   (document.head||document.documentElement).appendChild(s);
Run Code Online (Sandbox Code Playgroud)

我的脚本.js:

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


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


 });
Run Code Online (Sandbox Code Playgroud)

现在,我想收听网页 DOM 中的按钮单击事件?如何实现这一目标?

Ale*_*tri 5

你可以听

document.body.addEventListener('click', yourListenerFunction, true);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您正在以一种非常奇怪的方式执行您的内容脚本。通常是在您的 background.js 中使用一些代码,例如:

chrome.tabs.executeScript(tabId, {file:"yourScriptFile.js"});
Run Code Online (Sandbox Code Playgroud)

我推荐这个https://developer.chrome.com/extensions/overview