Chrome Extension + window + selection of text + passing + to popupwindow

1,673 views
Skip to first unread message

gopinath m

unread,
Feb 10, 2010, 6:02:23 AM2/10/10
to Chromium-extensions
hi experts,

I tried one of this scripts from this url:
http://groups.google.com/group/chromium-extensions/browse_thread/thread/eab847f0a32ec25c/c6dd63ca74efad4b?#c6dd63ca74efad4b


-------background.html:-----------

<html>
<head>
<script type="text/javascript">
/*
var selection_callbacks = [];
function getSelection(callback) {
selection_callbacks.push(callback);
chrome.tabs.executeScript(null, { file:"content_script.js" });
};
chrome.extension.onRequest.addListener(function (request) {
var callback = selection_callbacks.shift();
callback(request);
});
*/
function getSelection() {
chrome.tabs.executeScript(null, {file: "content_script.js"});
}
chrome.extension.onRequest.addListener(function(request) {
alert("got selection: " + request);
});
</script>
</head>
<body></body>
</html>

-----------content_script.js---------------------

chrome.extension.sendRequest(window.getSelection());


---------------manifest.json-------------------------------------

{
"name": "xyz",
"description": "xyz",
"version": "1.2.5",
"background_page": "background.html",
"icons": { "128": "xyz.png" },
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_title": "xyz",
"default_icon": "xyz.png",
"popup" : "popup.html"
}
}


-----------------------popup.html-------------------


<html>
<head>
<script type="text/javascript">
function onSelection(text) {
document.getElementById("output").innerHTML = text;
}
//
chrome.extension.getBackgroundPage().getSelection(onSelection);
alert("Selected Text is : " +
chrome.extension.getBackgroundPage().getSelection());
</script>
</head>
<body>
<div id="output">This should be replaced with the selected text</
div>
</body>
</html>

My OS: Ubuntu
Chrome Version: Chromium 5.0.321.0

i tried on windows as well
on windows i have chrome version 4.0.249.78

have no luck with me, is there any version conflict?

please help me guys,

Thanks,
Gopi

Arne Roomann-Kurrik

unread,
Feb 10, 2010, 10:55:17 AM2/10/10
to gopinath m, Chromium-extensions
Of the top of my head, you need to change 

    chrome.extension.sendRequest(window.getSelection());
to 
    chrome.extension.sendRequest(window.getSelection().toString());

Let me know if that works,
~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Juan Bosco Lopez Jasso

unread,
Feb 10, 2010, 2:07:38 PM2/10/10
to gopinath m, Chromium-extensions
did you try document.getSelection().toString() rather than window.getSelection() ?

On Wed, Feb 10, 2010 at 3:02 AM, gopinath m <gopi.off...@gmail.com> wrote:

gopinath m

unread,
Feb 11, 2010, 12:23:56 AM2/11/10
to Juan Bosco Lopez Jasso, Chromium-extensions
-----background.html----------

<html>
 <head>
   <script type="text/javascript">
   function getSelection() {
     chrome.tabs.executeScript(null, {file: "content_script.js"});
   }
   chrome.extension.onRequest.addListener(function(request) {
     alert("got selection: " + request);
   });
   </script>
 </head>
 <body></body>
</html>

------conten_script.js-------------

chrome.extension.sendRequest(window.getSelection().toString()); 

-------popup.html---------------

<html>
 <head>
   <script type="text/javascript">
     function onSelection(text) {
       document.getElementById("output").innerHTML = text;
     }
     chrome.extension.getBackgroundPage().getSelection(onSelection);
     alert("Selected Text is : " + chrome.extension.getBackgroundPage().getSelection());
   </script>
 </head>
 <body>
   <div id="output">This should be replaced with the selected text</div>
 </body>
</html>

thanks experts its working very fine.

Regards
Gopi
Reply all
Reply to author
Forward
0 new messages