0

I cannot send message from my background script to content script except i remove the popup.html from the browser action in the manifest.json. Someone help me

background.js

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tabs) {
  // Send a message to the active tab
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var activeTab = tabs[0];
    chrome.tabs.sendMessage(tabs[0].id, {"message": "clicked_browser_action"});
  });
});

// This block is new! and will be used later
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

content.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message == "clicked_browser_action" ) {
   console.log("Congratulations you can now work!");
     
      // This line is new!
      chrome.runtime.sendMessage({"message": "open_new_tab", "url": firstHref});
    }
  }
);

manifest.json

 {
    "name": "Wowprezi lead tool",
    "version": "1.0",
    "description": "Extension to find leads and add to sales force!",
	"author": "Djouonang Landry",
    "manifest_version": 2,
	
	 "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",

   "background": {
   "persistent": true,
    "scripts": ["js/background.js"]
  },
  
   "content_scripts": [
        {
		    "matches": ["<all_urls>"],
		    "js": ["js/jquery-3.3.1.min.js","js/content.js"],
		    "all_frames": true
        }
    ],
	
  
  "browser_action": {
   "default_popup": "html/popup.html",
   "default_title": "Find leads"
 }
 
}

popup.html - Except this is removed from the manifest.json i cannot send message from background to content.js. Please note i use the console function to check if the message was sent across to the content.js

1
  • Please post the code you are trying, as it stands we have nothing to go on here. Commented Apr 29, 2019 at 15:08

1 Answer 1

2

According to documentation, the chrome.browserAction.onClicked listener is

Fired when a browser action icon is clicked. Does not fire if the browser action has a popup.

(bold type added).

Either remove the browserAction popup or move the code you currently have inside the listener to the popup page script.

Sign up to request clarification or add additional context in comments.

1 Comment

nokonoko, i see your point but moving it to pop up will make the script shortlived since it runs as long as the pop up is alive and once closed it stops running. I will need the process to continue even if the pop up is closed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.