4

my contentscript.js:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
   if (request.ask === "selectedtext"){
    sendResponse({selectedtext: window.getSelection().toString()});
   }      
});

my background.js:

function onClickHandler(info, tab) {
 chrome.tabs.sendMessage(tab.id, {ask: "selectedtext"}, function(response) {
   console.log(response.selectedtext);
 });
};
chrome.contextMenus.onClicked.addListener(onClickHandler);

chrome.runtime.onInstalled.addListener(function() {
  var contexts = ["selection"];
  for (var i = 0; i < contexts.length; i++){
    var context = contexts[i];    
    var title = "send the word to background.js";
    var id = chrome.contextMenus.create({"title": title, "contexts":[context],"id": "context1" + context});   
  } 
});

UPDATE:

{
"name" : "Send Data Plugin",
"version" : "1.1",
"description" : "A trivial usage example.",
"permissions": [
  "browsingData", "contextMenus", "http://chromeplugin.sites.djangoeurope.com/"
],
"browser_action": {
  "default_icon": "icon.png",
  "default_popup": "popup.html"
},  
"background": {
  "persistent": false,
  "scripts": ["background.js"]
},
"manifest_version": 2,
"content_scripts": [{
  "matches": ["<all_urls>"],
  "js": ["contentscript.js"]
 }]
}

but, once I click on contextMenu send the word to background.js, i am getting the error in console:

Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

what am i doing wrong? i googled and read some q&a's in here, but none seems to be helping..

doniyor
  • 36,596
  • 57
  • 175
  • 260

1 Answers1

3

Well your error means that you're getting undefined as your response. i.e no data is coming back from the context page.

Your content.js looks fine, I think the most likely thing is there is no content script communicating from the other end. Put a console.log in your content script and make sure that's being hit.

You may just need to refresh the page that you're communicating with as the content script may not be the current one?

James
  • 4,146
  • 1
  • 20
  • 35
  • thanks man, i refreshed the page where i am selecting text. no success. i will show you my manifest.json so you can see if content_script is defined correctly. – doniyor Jun 09 '14 at 13:48
  • somehow, my content_script listener is not getting any message from background. – doniyor Jun 09 '14 at 13:53
  • You have your content script in as contentscript.js whereas above you have it titled content.js... Not sure if that was just an accident in the question or if there is a file name mismatch there. – James Jun 09 '14 at 14:00
  • 1
    See [this answer](http://stackoverflow.com/a/23895822/934239). It may explain why it's not loaded. – Xan Jun 09 '14 at 14:06
  • yeah sorry it was an accicent while posting the question, i updated the question. ok thanks i will see this link – doniyor Jun 09 '14 at 14:07
  • i got it working. the issue was that i didnot add ``""`` to permissions. :) thanks a lot – doniyor Jun 09 '14 at 14:16