It generates a context menu, and then fires depending on what menu is clicked.
If I use this in a text box, it will run once, returning “am I looping” to the console log once, but if I do this in a content editable html element it runs 3 times.
I have stripped down the content script to the minimum parts and run it, and I still get it running 3 times for a content editable html element.
For starters, that removeListener will not work that way. You’d want to pass CommandParse directly to addListener/removeListener. Plus it wouldn’t work either way, since it’s removing a function that’s not even added yet?
Same for removeEventListener btw.
Further, I’d make sure that you’re only injecting this script in the frame you want it to run in and only inject it once per page load.
Lastly, I’m not sure mousedown even goes for context menu clicks, but typically you’d use the contextmenu event when you want to deal with things in relation to a context menu.
So the main reason why you’d see this thrice is because you’re injecting/running the script multiple times, thus having multiple listeners.
Right now, I am generating the menus (100+ items) from a JSON file using browser.menus.create in the background script, and then using browser.menus.onClicked.addListener(info, tab, defaultMenu) to communicate this to the content script.
Would I need to completely rewrite my menu generation, or just change the listener section?
getTargetElement can be used in the handler by sending the target element ID to a content script (or using it directly like shown in the example on MDN) and then you can use the method to turn the target element ID into an actual DOM node.
OK, that seems to work, it generates a long integer number.
It is unclear to me how I am supposed to pass the ID via sendmessage, and what if anything, I have to do ensure that the listener will apply it to only 1 frame.
The browser.tabs.sendMessage documentagion notes that options are an object, and frameId is an integer, but I am unclear on how to structure this syntactically.
I understand what you are saying, that I should send a frameId object, and the listener should take that object from the message, and then apply only to that frame.
What I am unclear on is the syntax of the send and listen, and neither the docs, nor the examples that I have looked at, give me any insight into what the syntax should be.
Also, info.targetElementId and info.iframeId give different values, though both are integers, so I am unclear on what I should be doing from a syntax/variable perspective.
You should use the two values as different things in your sendMessage call. The targetElementId would correspond to wotElement in my example code, and the frameId to idOfTargetFrame.
It’s sendMessage that does the filtering based on the frame ID, not the listener.