2

I am trying to execute a script which manipulates the page's html when a chrome extension is clicked. (No popups involed)

The problem is that I can't get the alert("test") working (located in content.js). What am I doing wrong?

Manifest.Json

{ "name": "test", "version": "1.0", "manifest_version" :2, "description": "test", "browser_action" : { "default_icon" :"icon.png" }, "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches": ["http://*/*","https://*/*"], "js": ["content.js"] }], "permissions": [ "http://*/*", "https://*/*", "contextMenus", "tabs" ] } 

background.js

chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.sendMessage(tab.id, { 'action': 'SwapNameAndSurname', 'data' : {'Name' : 'John', 'Surname' : 'Doe'} }); }); 

content.js

chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { alert("test"); console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension"); if (request.greeting == "hello") sendResponse({farewell: "goodbye"}); }); 
3
  • 2
    After reloading the extension, you must also reload the web page before the extension can talk with the content script. Commented Mar 29, 2014 at 0:40
  • Thanks! I had to restart chrome to work. Commented Mar 29, 2014 at 7:28
  • Sometimes you don't look at the right console (I was looking at my "background" console instead of the console of the tab...) Commented Jun 1, 2018 at 14:02

1 Answer 1

1

I was stuck on this for a while.

Refreshing both the extension and the page did the job.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.