so if i add the tabs permissions to my extension i am able to post
messages, but i am not receiving them in my content script.  anyone
have any idea on what i need to do to get my content script to
"receive" messages.

On Tue, Sep 15, 2009 at 1:37 PM, Anatoly Yakovenko
<[email protected]> wrote:
> No luck,
> $ cat manifest.json
> { "name": "Quote"
> , "version": "1.0"
> , "description": "Quote the selected text."
> , "toolstrips": [ "quote.html" ]
> , "content_scripts" : [ { "js" : [ "quote.js"]
>                        , "matches" : ["http://*/*";, "https://*/*";]
>                        }]
> }
> $ cat quote.js
> function quoteText() {
>   window.open('quoteText')
>   var t = window.getSelection().toString().replace(/./g, ">");
>   var range = window.getSelection().getRangeAt(0);
>   range.deleteContents();
>   range.insertNode(document.createTextNode(t));
> }
> chrome.extension.onConnect.addListener(function (port) {
>   port.onMessage.addListener(function (data) {
>      window.alert(data)
>      window.open('onmessage')
>      quoteText();
>   });
> }
> $ cat quote.html
> <script>
> function sendMessage(message) {
>   window.open('sendmessage')
>   chrome.tabs.getSelected(null, function(tab) {
>      var port = chrome.tabs.connect(tab.id);
>      port.postMessage(message);
>   });
> }
> </script>
> <div class="toolstrip-button"  onclick="sendMessage('quoteText');">
>   <span>Quote</span>
> </div>
>
> It seems like i am only getting do the sendMessage function, but
> postMessage never gets executed
>
> Anatoly
>
>
>
> On Thu, Sep 10, 2009 at 4:39 PM, Antony Sargent <[email protected]> wrote:
>> 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