Actually you can connect directly from your toolstrip to the content
script'ed page.
In your content script:

chrome.extension.onConnect.addListener(function (port) {
  port.onMessage.addListener(function (data) {
    // do something with data here
  }
}

In your toolstrip (sending a message to the currently selected tab):

function sendMessage(message) {
  chrome.tabs.getSelected(null, function(tab) {
    var port = chrome.tabs.connect(tab.id);
    port.postMessage(message);
  });
}

Depending on your needs, you might want to keep the port around instead of
using it only once in this example, or connect to more than just the
currently selected tab.

Also note that in the content script, you might need to use the workaround
mentioned in crbug.com/17410 until it's fixed.


On Thu, Sep 10, 2009 at 4:16 PM, Daniel Wagner-Hall <[email protected]>wrote:

>
> You want to be doing this in a content script
> [http://code.google.com/chrome/extensions/content_scripts.html] rather
> than a toolstrip.  A content script has access to the page's DOM, and
> can interact with it.  Toolstrips are more for general UI for the
> extension.
>
> You can see the results of console.log from a toolstrip if you go to
> the page chrome://extensions and click the Inspect link next to the
> toolstrip.  The results of console.log from a content script will be
> in the page's log.
>
> You can communicate between toolstrips and content scripts (so that
> pressing a button causes an action in the content script).  This can
> (I believe only, though I may be wrong) be done by making a background
> page, having the content script and toolstrip each connect to it
> (using chrome.extension.connect), and passing messages over these
> ports.
>
> On Thu, Sep 10, 2009 at 10:25 PM, Anatoly Yakovenko
> <[email protected]> wrote:
> >
> > So i thought this might work:
> >
> > <script type="text/javascript">
> > function quoteText()
> > {
> >   if(document.getSelection) {
> >      document.getSelection.toString().replace(/^/g, ">");
> >   }
> > }
> > </script>
> >
> > <div class="toolstrip-button"  onclick="quoteText()">
> >   <span>Quote</span>
> > </div
> >
> > but its not.  How come i can't get the selection?  also, console.log
> > isn't working, my script isn't showing up in the debugger window as an
> > option under scripts.  But if i use window.open("here"); it opens a
> > window.
> >
> > >
> >
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to