Last Comment Bug 753696 - It should be possible to send data to content script of context menu on context event
: It should be possible to send data to content script of context menu on conte...
Status: RESOLVED DUPLICATE of bug 767076
:
Product: Add-on SDK
Classification: Client Software
Component: General (show other bugs)
: unspecified
: x86_64 Linux
: -- normal (vote)
: ---
Assigned To: Irakli Gozalishvili [:irakli] [:gozala] [@gozala]
:
:
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-05-10 02:57 PDT by Siavash
Modified: 2012-07-09 21:58 PDT (History)
4 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---


Attachments

Description Siavash 2012-05-10 02:57:05 PDT
If I'm not mistaken, you can only send data to content script(of a context menu item) using data property of context menu item, which sends data only once.

I want to send new data to content script every time user right-clicks on an element.

For example, whenever user right-clicks on a text field, add-on code sends clipboard content to content script and content script puts that content into the text field.
Comment 1 Hernán Rodriguez Colmeiro (:peregrino) 2012-05-12 06:36:27 PDT
You're completely right. There's no way to get hold of the contentScript object to pass messages to it.

While this could be worked around by updating the `data` property of the contextMenuItem when clicking it, you enter a vicious cycle: to listen for click events on the context-menu you need a contextScript to listen to 'click' and then posting a message back.
So if what you want to do is as follows: "Update the `data` property of the contextMenuItem on click, with something not related to the clicked node, and then use that data to do some operation on the clicked node", you can't.
Comment 2 Siavash 2012-05-12 07:43:36 PDT
My problem is fixed(thanks to Hernán) by updating the "data" property.

So now the issue is posting messages to content script(for sending objects or arrays for example). Maybe a "postMessage" method for context-menu like "panel" module?
Comment 3 Irakli Gozalishvili [:irakli] [:gozala] [@gozala] 2012-05-24 10:54:13 PDT
(In reply to Siavash from comment #0)
> If I'm not mistaken, you can only send data to content script(of a context
> menu item) using data property of context menu item, which sends data only
> once.
> 
> I want to send new data to content script every time user right-clicks on an
> element.
> 
> For example, whenever user right-clicks on a text field, add-on code sends
> clipboard content to content script and content script puts that content
> into the text field.

From your description, I got an impression that what you're looking for is a page-mod rather than a context-menu. If you just need to paste code from a clipboard to text field on right click, you could use a page mode to set up an event listener that would sent message to add-on to asking for clipboard data.

As of sending arrays and JSON objects you could still serialize them to string and use `data` property to deliver it to a content script. Either way it feels to me that what you're trying to do is not something that context-menu should be doing.
Comment 4 Siavash 2012-05-24 13:10:14 PDT
(In reply to Irakli Gozilalishvili [:irakli] [:gozala] from comment #3)
> From your description, I got an impression that what you're looking for is a
> page-mod rather than a context-menu. If you just need to paste code from a
> clipboard to text field on right click, you could use a page mode to set up
> an event listener that would sent message to add-on to asking for clipboard
> data.
> 
> As of sending arrays and JSON objects you could still serialize them to
> string and use `data` property to deliver it to a content script. Either way
> it feels to me that what you're trying to do is not something that
> context-menu should be doing.

I forgot to mention that "paste" would occur on clicking a context menu item.
And as I said, my problem is solved. here are the related links:
https://groups.google.com/forum/#!topic/mozilla-labs-jetpack/AZ8YuxLacF0
https://gist.github.com/2667093

So I don't have any example now! But what about this scenario:

When a user clicks on a context menu item , we want to send something from content script to add-on script, then after doing something, send some data back to content script.

In this scenario, we need a way to send a message from add-on script and receive the message in content script.
Comment 5 Irakli Gozalishvili [:irakli] [:gozala] [@gozala] 2012-05-25 16:48:45 PDT
(In reply to Siavash from comment #4)
> (In reply to Irakli Gozilalishvili [:irakli] [:gozala] from comment #3)
> > From your description, I got an impression that what you're looking for is a
> > page-mod rather than a context-menu. If you just need to paste code from a
> > clipboard to text field on right click, you could use a page mode to set up
> > an event listener that would sent message to add-on to asking for clipboard
> > data.
> > 
> > As of sending arrays and JSON objects you could still serialize them to
> > string and use `data` property to deliver it to a content script. Either way
> > it feels to me that what you're trying to do is not something that
> > context-menu should be doing.
> 
> I forgot to mention that "paste" would occur on clicking a context menu item.
> And as I said, my problem is solved. here are the related links:
> https://groups.google.com/forum/#!topic/mozilla-labs-jetpack/AZ8YuxLacF0
> https://gist.github.com/2667093
> 
> So I don't have any example now! But what about this scenario:
> 
> When a user clicks on a context menu item , we want to send something from
> content script to add-on script, then after doing something, send some data
> back to content script.
> 
> In this scenario, we need a way to send a message from add-on script and
> receive the message in content script.

I think my point is that content script of context-menu is not really designed to perform changes it's designed to to provide more generalized way to assert if current context is a match for the context menu item. Making page modifications is task of page mod.

Now that being said, I do see room for a confusion and desire to reuse content-scripts. I have start exploring how this picture can be improved: https://github.com/mozilla/addon-sdk/wiki/JEP-Content-scripts Let me know if such change would make it for you.

> As I said, my problem is solved.

Do you think we could close this bug then ?
Comment 6 Siavash 2012-05-26 02:47:35 PDT
(In reply to Irakli Gozilalishvili [:irakli] [:gozala] from comment #5)
> (In reply to Siavash from comment #4)
> > (In reply to Irakli Gozilalishvili [:irakli] [:gozala] from comment #3)
> > > From your description, I got an impression that what you're looking for is a
> > > page-mod rather than a context-menu. If you just need to paste code from a
> > > clipboard to text field on right click, you could use a page mode to set up
> > > an event listener that would sent message to add-on to asking for clipboard
> > > data.
> > > 
> > > As of sending arrays and JSON objects you could still serialize them to
> > > string and use `data` property to deliver it to a content script. Either way
> > > it feels to me that what you're trying to do is not something that
> > > context-menu should be doing.
> > 
> > I forgot to mention that "paste" would occur on clicking a context menu item.
> > And as I said, my problem is solved. here are the related links:
> > https://groups.google.com/forum/#!topic/mozilla-labs-jetpack/AZ8YuxLacF0
> > https://gist.github.com/2667093
> > 
> > So I don't have any example now! But what about this scenario:
> > 
> > When a user clicks on a context menu item , we want to send something from
> > content script to add-on script, then after doing something, send some data
> > back to content script.
> > 
> > In this scenario, we need a way to send a message from add-on script and
> > receive the message in content script.
> 
> I think my point is that content script of context-menu is not really
> designed to perform changes it's designed to to provide more generalized way
> to assert if current context is a match for the context menu item. Making
> page modifications is task of page mod.
> 
> Now that being said, I do see room for a confusion and desire to reuse
> content-scripts. I have start exploring how this picture can be improved:
> https://github.com/mozilla/addon-sdk/wiki/JEP-Content-scripts Let me know if
> such change would make it for you.
> 
> > As I said, my problem is solved.
> 
> Do you think we could close this bug then ?

I understand your point, but context-menu module gives us the node, if we want to use page-mod for making modifications, we need to give the node to page-mode. But last time I tried to do this, it gave a sandbox error.

> Now that being said, I do see room for a confusion and desire to reuse
> content-scripts. I have start exploring how this picture can be improved:
> https://github.com/mozilla/addon-sdk/wiki/JEP-Content-scripts Let me know if
> such change would make it for you.

Seems interesting. This can solve a lot of communication problems and make communicating easier. It also solves the communication problem(mentioned above) between context-menu and page-mod.

> Do you think we could close this bug then ?

If content-script API will be implemented, yes.
Comment 7 Irakli Gozalishvili [:irakli] [:gozala] [@gozala] 2012-06-21 11:39:27 PDT
Closing this in favor of Bug 767076 that will implement more generic solution to this problem.

*** This bug has been marked as a duplicate of bug 767076 ***

Note You need to log in before you can comment on or make changes to this bug.