Note: There are a few cases of duplicates in user autocompletion which are being worked on.

It should be possible to send data to content script of context menu on context event

RESOLVED DUPLICATE of bug 767076

Status

Add-on SDK
General
RESOLVED DUPLICATE of bug 767076
5 years ago
5 years ago

People

(Reporter: Siavash, Assigned: irakli)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

5 years ago
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.
Assignee: nobody → rFobic
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.
Status: UNCONFIRMED → NEW
Ever confirmed: true
(Reporter)

Comment 2

5 years ago
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?

Updated

5 years ago
Whiteboard: [triage:followup]
(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.
(Reporter)

Comment 4

5 years ago
(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.
(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 ?
(Reporter)

Comment 6

5 years ago
(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.
Closing this in favor of Bug 767076 that will implement more generic solution to this problem.
Status: NEW → RESOLVED
Last Resolved: 5 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 767076
Whiteboard: [triage:followup]
You need to log in before you can comment on or make changes to this bug.