Aaron's code uses chrome.tabs.executeScript to dynamically inject the content script when the popup is opened - it's not included by default through the manifest.
~Arne On Thu, Jan 21, 2010 at 1:53 PM, Bill.Keller <[email protected]> wrote: > Chromium 4.0.303.0 (36626) Mac > Chrome 4.0.295.0 dev Mac > Chrome 4.0.295.0 dev PC > > It seems as though the content_script only runs once on each page > load. It's not continuously sending the selected txt to the background > page. > > For example, the below content_script reveals that the only way to > actually capture the selected text is to quickly select some text > before a page finishes loading. > chrome.extension.sendRequest(window.getSelection().toString()); > console.log(window.getSelection().toString()); > > > > > > On Jan 21, 1:45 pm, Arne Roomann-Kurrik <[email protected]> wrote: > > The sample still works for me in 4.0.295.0 (Official Build 35884) > unstable > > > > Which version are you using? > > > > ~Arne > > > > > > > > On Thu, Jan 21, 2010 at 4:07 AM, soupenvy <[email protected]> wrote: > > > Is there any particular change in Chrome that prevents this from > > > working in the latest builds? > > > > > I can't seem to get this sample working. > > > > > On Nov 24 2009, 2:52 pm, Arne Roomann-Kurrik <[email protected]> > > > 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 <[email protected]> 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 > [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]<chromium-extensions%[email protected]><chromium-extensions%2Bunsu > [email protected]> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/chromium-extensions?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Chromium-extensions" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<chromium-extensions%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/chromium-extensions?hl=en. > > > >--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/chromium-extensions?hl=en.
