Arne, Aaron, Thanks so much -- this works great!
Jay On Nov 24, 2:52 pm, Arne Roomann-Kurrik <kur...@google.com> wrote: > As a note, you'll need to convert the result of getSelection() to a > string or else it won't serialize correctly for the sendRequest call. > window.getSelection().toString() is probably easiest. > > If you want to get access to the selected text in a popup, you'll need > to pass it onward from the background page. There's probably a better > way to do this, but here's a background page which forwards the > message: > > <html> > <head> > <script type="text/javascript"> > var selection_callbacks = []; > > function getSelection(callback) { > selection_callbacks.push(callback); > chrome.tabs.executeScript(null, { file: > "contentscript.js" }); > }; > > chrome.extension.onRequest.addListener(function (request) { > var callback = selection_callbacks.shift(); > callback(request); > }); > </script> > </head> > <body> > </body> > </html> > > and here's a popup which displays it (it's probably not a great idea > to just innerHTML the text, but this is just an example): > > <html> > <head> > <script type="text/javascript"> > function onSelection(text) { > document.getElementById("output").innerHTML = text; > } > chrome.extension.getBackgroundPage().getSelection(onSelection); > </script> > </head> > <body> > <div id="output"> > This should be replaced with the selected text > </div> > </body> > </html> > > The content script is almost identical to Aaron's: > > chrome.extension.sendRequest(window.getSelection().toString()); > > ~Arne > > On Nov 24, 11:08 am, Aaron Boodman <a...@google.com> wrote: > > > You can setup a content script that does it for you that your popup > > communicates with. I do it with three files: > > > background.html (register this in your manifest with the background_page > > key): > > ============= > > function getSelection() { > > chrome.tabs.executeScript(null, // by default, executes in current tab > > { file: "content_script.js"}); > > > } > > > chrome.extension.onRequest.addListener(function(request) { > > alert("got selection: " + request); > > > }); > > > content_script.js > > ============ > > chrome.extension.sendRequest(window.getSelection()); > > > popup.html > > ======== > > chrome.extension.getBackgroundPage().getSelection(); > > > I haven't tested any of this, but I think it should work :) > > > - a -- You received this message because you are subscribed to the Google Groups "Chromium-extensions" group. To post to this group, send email to chromium-extensi...@googlegroups.com. To unsubscribe from this group, send email to chromium-extensions+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/chromium-extensions?hl=en.