1

that's my popUp.js.

window.onload = function() {
    chrome.extension.sendMessage({
         type: "login-check"
     });
};

and that's my Background script.

chrome.runtime.onMessage.addListener(
 function(request, sender, sendResponse) {
  switch(request.type){
    case "login-check":
     checkLogin();
    break;
  }
});

function checkLogin() {
 chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ 
  chrome.tabs.sendMessage(tabs[0].id, {type: "login"}, function(response){
   console.log(response.farewell)
  }); 
 });
}

and that's my content-script.

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type == "login")
     sendResponse({farewell: "goodbye"});
});

My message is sending from popUp.js to background.js but not sending from background to content script , showing me an error "Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined " Please answer this question i am new to Chrome extension development and please don't send me link to others answers i have already implement all the possible answers in stack overflow. Thanks in Advance. and that's my Manifest.json

{
  "manifest_version": 2,

  "name": "Time Tracking Extension",
  "description": "This Extension is for Time Tracking",
  "version": "2.1",
  "browser_action": {
   "default_icon": "logo.png",
   "default_popup": "popUp.html"
  },
  "permissions": ["storage", "tabs", "cookies"],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "content_scripts": [
      {
          "matches": ["<all_urls>"],
          "css": ["css/mystyles.css", "css/jquery-ui.css", "css/bootstrap.min.css"],
          "js": ["js/jquery.min.js", "js/jquery-ui.js", "js/bootstrap.min.js", "js/content-script.js"]
      }
  ]
}
  • Does it help if you reload the tab you're activating your extension on before clicking? – Xan Jul 20 '16 at 07:10
  • when i click on extension icon certainly it show me an error which i describe in question. – Hassan Shafique Jul 20 '16 at 07:39
  • @HaibaraAi That would then complain about `tabs[0].id`. I'm 99% certain it's https://stackoverflow.com/questions/23895377/sending-message-from-a-background-script-to-a-content-script-then-to-a-injected/23895822 but I wait for symptom confirmation. – Xan Jul 20 '16 at 07:55
  • @Xan, yes you're right, I forgot the specific error info. – Haibara Ai Jul 20 '16 at 07:56
  • yeah may b i got your point at first it shows me an error but when i open new tab than i again click in extension than it shows me desire result. but i also follows the link which u shared it also show me an error "Error in event handler for (unknown): TypeError: Cannot read property 'pong' of undefined" – Hassan Shafique Jul 20 '16 at 08:35

0 Answers0