'Sending a message from backround to content-script

I'm trying to send a simple message from the backround.js to the content-script.js but I'm not really successful.

here is the backround relevant code:

function sendStop(){
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {greeting: "stop"}, function(response) {
    });
  });
}

theScriptsAreinserted = false
chrome.contextMenus.onClicked.addListener(function(clickDate) {
    if (clickDate.menuItemId == 'select' && theScriptsAreinserted == false) {
      addJsScript()
      addCssScript()
      theScriptsAreinserted = true
    if (clickDate.menuItemId == 'select' && theScriptsAreinserted == true) {
      sendStop()
    }
  }
});

and here is the content-script relevant code (runs all the time):

function reset(){
    chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
          console.log(sender.tab);
          if (request.greeting === "stop"){
            finish == true;
            console.log("finish")
          }

        }
      );
}

I just managed to learn it so I hope I do not make a terrible mistake and waste your time. Any help will be appreciated :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source