javascriptgoogle-chrome-extensioncontent-script

chrome.runtime.onMessage.addListener getting invoked on every tab open


I am building an extension in which content script is injected on context menu click. This functionality is working. Now the problem I am facing is that, whenever I open a new tab, the

chrome.runtime.onMessage.addListener

will get triggered without context menu click. How can I invoke the onMessage.addListener only on context menu click.

background.js



on_message = async(message, sender, sendResponse) => {
    console.log("bg.on_message");
    sendResponse("from bg");

    chrome.storage.local.get("list_url", function (data) {
        if (typeof data.list_url != "undefined") {
            urls = data.list_url
        }
    });
    chrome.storage.local.get("list_ip", function (data) {
        if (typeof data.list_ip != "undefined") {
            ips = data.list_ip
        }
    });
    chrome.storage.local.get("list_hash", function (data) {
        if (typeof data.list_hash != "undefined") {
            hashes = data.list_hash;
        }
    });

    if (hashes){
        hash_report = await createHashReport(hashes)
        hash_table = await createHashTable(hash_report)
        chrome.storage.local.set({
            "scanHash": true,
            "hash_table": hash_table
          }, () => {});
    }
    if (ips){
        ip_report = await createIpReport(ips)
        ip_table = await createIpTable(ip_report)
        chrome.storage.local.set({
            "scanIp": true,
            "ip_table": ip_table
          }, () => {});
    }
    if (urls){
        url_report = await createUrlReport(urls)
        url_table = await createUrlTable(url_report)
        chrome.storage.local.set({
            "scanUrl": true,
            "url_table": url_table
          }, () => {});
    }
    if ( hashes.length>0 || urls.length>0 || ips.length>0 ){
        chrome.windows.create({url: "output.html", type: "popup", height:1000, width:1000});
    }
}

chrome.runtime.onMessage.addListener(on_message);

genericOnClick = async () => {

    // Inject the payload.js script into the current tab after the backdround has loaded
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.scripting.executeScript({
            target: { tabId: tabs[0].id },
            files: ["payload.js"]
        },() => chrome.runtime.lastError);
    });
    
}

payload.js

function extract() {
    
    htmlInnerText = document.documentElement.innerText;
    url_exp = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
    regex =  new RegExp(url_exp)
    list_url = htmlInnerText.match(url_exp)

    ip_exp = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/;
    list_ip = htmlInnerText.match(ip_exp)

    hash_exp = /\b[A-Fa-f0-9]{32}\b|\b[A-Fa-f0-9]{40}\b|\b[A-Fa-f0-9]{64}\b/g
    list_hash = htmlInnerText.match(hash_exp)

    chrome.storage.local.set({ list_url: removeEmails(removeDuplicates(list_url)), list_ip: removeDuplicates(list_ip), list_hash: removeDuplicates(list_hash) });

}

chrome.runtime.sendMessage( extract())

Solution

  • Remove the following definition from manifest.json.

      "content_scripts": [
        {
          "js": [
            "payload.js"
          ],
          "matches": [
            "<all_urls>"
          ]
        }
      ]