-1

I want to firstly understand what is content script and what is background page in extension development.

is the content Script the script which is in website content? the background page is extensions html page?

i am asking this because i want to send the selected text to a webservice in this way:

enter image description here

I have two problems here:

1) how do I get the selected text to show in the menu instead of '' , window.getSelection().toString() is showing nothing

2) how can I send this text to django backend.

here are my attemps:

function onClickHandler(info, tab) {
   var selectedtext = info['selectionText'];
   //how to send this text to django backend? 
};
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 '" + window.getSelection().toString() + "' to django backend";
     var id = chrome.contextMenus.create({"title": title, "contexts":[context],"id": "context1" + context});   
   } 
});
doniyor
  • 36,596
  • 57
  • 175
  • 260
  • in that answer, it is not said what content script is and what background page is, can someone please explain me these concepts? – doniyor Jun 09 '14 at 08:23
  • 2
    Your question IS essentially a duplicate. But yes, I'd gladly add those links: 1) The excellent [Architecture overview](https://developer.chrome.com/extensions/overview#arch), 2) [Content scripts documentation](https://developer.chrome.com/extensions/content_scripts) – Xan Jun 09 '14 at 08:25

1 Answers1

1

A content script is simply some javascript that you run on webpages you visit. (you specify which sites to run it on in your manifest)

A background page lets you keep your extension alive and interact between different parts of your extension.

You need to capture the selected text in your content script and send it from your content script to your background page. You're trying to do window.getSelection().toString() from your background page, which is a process running in the background! Of course it has no selected text.

I strongly recommend reading everything Xan linked for you. I hope I've given some basic understanding to get started..

James
  • 4,146
  • 1
  • 20
  • 35
  • Thanks, now i understand. But can you explain me where content script and background script should reside? Do they live in extension folder? – doniyor Jun 09 '14 at 09:52
  • Oh i think now i got it. All files live in extension folder and i define in manifest how and where they should act. Right? – doniyor Jun 09 '14 at 10:01
  • Yep, you've got it :) – James Jun 09 '14 at 13:26
  • :) can you please take a look at my another question pls :( http://stackoverflow.com/questions/24120575/chrome-extension-stack-trace-typeerror-cannot-read-property-selectedtext-of – doniyor Jun 09 '14 at 13:30