How to programmatically invoke a Firefox button/addon - firefox

For a given button inside Firefox 4, how do I discover the appropriate way to simulate a click using XPCom/JavaScript? I'd like to programmatically invoke a dialog which is currently only reachable by clicking a toolbar button.
More info: I am happily using Mozilla's experimental F1 sharing extension from here:
http://f1.mozillamessaging.com
But I'd like to craft a custom keybinding or programmatic invocation for the share dialog (basically I want to hide my navigation bar but still invoke F1 easily).
I cannot find any straightforward way to do this? I suspect that I just need to peek at (the equivalent of) the onClick handler for the default button and then invoke that in my own XPCom code ... but this seems undiscoverable.
Any help would be much appreciated.

Peeking at the onclick handler might work, if they did it that way. But if they added the functionality using addEventListener then it would not. In that case, your best bet might be to use dispatchEvent to simulate the user clicking on that button.
https://developer.mozilla.org/en/DOM/element.dispatchEvent

Related

How can I inspect a XUL popup element without it disappearing?

Firefox 31 broke my Googlebar Lite extension, and I'm trying to debug why. My problem is that mouse clicks no longer register for search suggestions that appear in the auto-complete popup menu (which comes as a part of the Firefox autocomplete textbox control). I'd like to inspect these chrome elements with DOM Inspector, but the popup closes (destroying the anonymous children) before I'm able to inspect them.
How can I inspect a popup element (in this case it's a panel) without it disappearing? Is there a way I can force that kind of element to stay open so I can examine its children?
Inspecting the autocomplete DOM would require hacking deeply into the autocomplete code to avoid making it destroy items before inspecting.
While possible, I'd first look if the autocomplete code changed, so I tried:
Finding the result interface on MXR: http://mxr.mozilla.org/mozilla-central/source/toolkit/components/autocomplete/nsIAutoCompleteResult.idl
Checking the log: http://hg.mozilla.org/mozilla-central/filelog/de8c0f0e74a2/toolkit/components/autocomplete/nsIAutoCompleteResult.idl
Checking out the newest changeset(s) and bug(s).
And indeed, Bug 754265 amended the interface.
So I implemented the interface change, implementing the new API method, and after that the broken stuff works again:
getFinalCompleteValueAt: function(index) {
return this._results[index];
},
I made a pull-request for you.
Also try installing addon "Element Inspector" it allows you Shift + Right Click anything and it pops it up in "DOM Inspector"
https://addons.mozilla.org/en-US/firefox/addon/element-inspector/

! Simulating a button click - dealing with objects/classes/events

I have a python module abc.py which has numerous class and some class inherting from another classes. The entire file has classes that are connected to each other with objects/instantiation.
The function calls are all based off of GUI button clicks. So, functions get called in abc.py, when buttons are clicked.
I need to write a script which does exactly the same function of button clicking, but without me clicking on the actual GUI - you can imagine it automated.
The issue is, the button on the GUI, when clicked, an event is triggered and the button is associated with a function call "def button_press" which takes (self, event) as parameters.
How would I pass it self and event? I want to use the classes/functions in the module "abc.py" as is and also simulate the button click.. Could you give me some pointers on how I could do this?
Thanks
You can either:
Create an appropriate event, fill it with your test data and call self.button_press(your_test_event), from within your gui.
As above but from outside your gui call, app.topframe..button_press(your_test_event),
If your event handler does not use any data from the event then if you def button_press(self, evt=None) you can do a or b without creating an event first,
you can use any one of the excellent python GUI Test tools available - some are listed here.
You'll probably want to look at wx.PostEvent to send events to a button. Or if you're using wxPython 2.9.2 or better, then you can use the new UIActionSimulator, which you can read about here:
http://wxpython.org/Phoenix/docs/html/UIActionSimulator.html
I've heard a number of people who use sikuli for GUI testing. There's also COBRA, which I keep seeing listed on the Tested in Python user's group as a tool for GUI testing, although I'm not entirely sure if you can use it on a wxPython app.

In order to add new functionality to existing Firefox clients, do I need to create an extension or a plugin?

More specifically, the idea is to allow the user to open Firefox, highlight a word on a web page, right click on it, and have an additional option that, when selected, calls c++ code that does something with the input string (must call C++ code, unfortunately), and displays a dialog box showing the result.
I'm still not sure if in order to implement this functionality I need to create a Firefox plugin or an extension. Can someone point me in the right direction?
Also, if someone can show me sample code in order to get me started that would be appreciated. (XPCOM, which I'm not even sure is what I should be using, seems a bit complicated for this seemingly simple project.)
You need a regular Firefox extension. It can add an item to the context menu, NPAPI plugins cannot do this. When it is clicked it can get the selected text and send it to your binary library. The best way to call functions in this library is js-ctypes, XPCOM is not required.

Firefox Extension - Monitor refresh and change of tab

I need to know when a user refreshs the page and when he switches to another tab.
Does anyone has a clue how to capture this in a firefox extension?
Best regards
Christian
What you seem to want is knowing when the value in the location bar changes. This requires implementing nsIWebProgressListener interface. The only method you really need is onLocationChange, rest of them should be dummies. You can find the documentation along with code examples here: https://developer.mozilla.org/en/Code_snippets/Progress_Listeners. You can also use progress listeners to monitor page loads.

How to create a view for a single control?

What is the best way to create a view for a single control that I need to load into a Shell region in a Prism app. I know I can wrap the control in a UserControl, but I suspect there may be a better way.
I am working on a demo app to learn Prism 4. Each module will load a navigation button into an ItemsControl in a region in the Shell. These navigation buttons will function like the Mail, Calendar, and other buttons in Outlook.
I am creating the view in each module that will hold the module's navigation button. The simplest way to create the view seems to be to wrap it in a UserControl. My question is this: Is there a better way to do it? Thanks for your help.
If you need graphical control, what you are doing is the way to go. If you find yourself making all of the buttons look the same (copy - pasting) you might find that a menu registration service is the way to go.
You'd have a service like IMenuService that you register with your container and modules can come around and register menu items to. You can then create buttons for the module. I've provided a sample for this here:
http://dl.getdropbox.com/u/376992/CAGMenus.zip
Your question, though, seems to be about whether or not you need to wrap a control in a UserControl to register them with a region? If that's the question, I believe the answer is no, although you might amend your question to tell us what you are running up against that makes you think this.
I ended up wrapping the control in a UserControl, and it seems to work fine. I am still interested in seeing if there is a better way to load the button, so I will hold this question open for a few days.
Edit 02/22/2011: I tried using a control without a UserControl wrapper, and I got the following error:
Library project file cannot specify ApplicationDefinition element.
I wrapped the control in a UserControl and the error went away.

Resources