0

Click event not working for gmail pages on content script of a chrome extension that I am creating. Here is my manifest.json file:

{
  "name": "Append Test Text",
  "description": "Add test123 to body",
  "version": "1.0",
  "permissions": ["activeTab"],
  "content_scripts": [
    {
      "matches": ["https://mail.google.com/*"],
      "js": ["jquery-3.4.1.min.js", "content-script.js"],
      "all_frames": true
    }
  ],
  "browser_action": {
    "default_title": "Append Test Text"
  },
  "manifest_version": 2
}

content-script.js:

var composeBtn = true;
function loop() {
  setTimeout(function() {
    if ($(".T-I.J-J5-Ji.T-I-KE.L3").length && composeBtn) {
      $(".T-I.J-J5-Ji.T-I-KE.L3").on("click", function() {
         alert("hello");
      });
      composeBtn = false;
    }
    loop();
  }, 5000);
}

loop();

$(".T-I.J-J5-Ji.T-I-KE.L3") is the Compose button in gmail. Cicking on it should show an alert.

Nafis
  • 1,394
  • 3
  • 14
  • 32
  • The manifest looks fine. You need to use **devtools** to inspect the situation. See if the content script runs at all. If not, maybe you have the official Google's app for Offline documents or Gmail, and so on. – wOxxOm Sep 10 '19 at 12:29
  • @wOxxOm, content script works but click event doesn't work. – Nafis Sep 10 '19 at 12:40
  • In that case you need to add [MCVE](/help/mcve) in the question, which means the actual code. – wOxxOm Sep 10 '19 at 12:48
  • You can try with [this approach](https://stackoverflow.com/a/51893135/3930351). – Iván Nokonoko Sep 10 '19 at 14:25
  • Works for me. Still it's better to use [event delegation on `document`](/q/12230057/) which is more reliable than setTimeout. – wOxxOm Sep 12 '19 at 14:50

0 Answers0