0

I am building a chrome extension that will check if the current webpage has any malware. I have placed a button in popup.html to trigger the malware checking code. But it is not working.

manifest.json:

{
  "name": "Malware Detector",
  "version": "1.0",
  "permissions": ["activeTab", "declarativeContent", "storage"],
  "browser_action": {"default_popup": "popup.html"},
  "manifest_version": 2
}

app.js:

chrome.runtime.onInstalled.addListener(function() {
});

popup.html:

<body>
  <p>Malware Detector</p>
  <button id="populateBtn">Detect Malware</button>
  <script src="popup.js"></script>
</body>

popup.js:

const btn = document.getElementById('populateBtn');
btn.onclick = () => {
  chrome.tabs.query({active: true, currentWindow: true}, ([tab]) => {
    chrome.tabs.sendMessage(tab.id, {data}, () => {
      if (chrome.runtime.lastError) {
        chrome.tabs.executeScript({file: 'inject.js'}, () => {
          chrome.tabs.sendMessage(tab.id, {data});
        });
      }
    });
  });
};

inject.js:

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
    // this is where malware checking code will go
    alert("test"); 
    console.log("test");
});

The test code of alert and console.log in inject.js are not getting triggered.

  • Note that the popup is a separate window so it has its own separate devtools: right-click inside the popup and select "inspect" in the menu. You will see an error about `data` being undefined. You need to define it or remove it. – wOxxOm Mar 04 '22 at 04:49

0 Answers0