1

I'm developing a Firefox extension and need some help with it.
My extension is overlayed and uses XBL binding to add new items to the user interface.
Is it possible to convert this extension to a bootstrap type?

I'm trying to add a button to the findbar.
Used XBL to override the findbar interface.

To start with the bootstrap I included "findbar{ -moz-binding:... }" rule to the style.css and register this sheet on startup()
(with nsIStyleSheetService.loadAndRegisterSheet()).
In this case my button is added to the bar without restart.

But when I disable or remove the addon I need to restart the browser so that the button disappear.
I tried to remove it on shutdown() in the bootstrap.js using:

var fb=window.gFindBar.getElement("findbar-container")  
var but=window.gFindBar.getElement("the-button")  
fb.removeChild(but)  

But this didn't remove it. I debugged the code and all the elements (fb, but) were found and removed but it didn't touch the real findbars in any tab I had or opened.

So I tried to unregister the stylesheet which bind my XBL to the findbar. This time the findbar just didn't open in the current tabs.
But in new tabs it opened and without the button (a little better...).

Also I've found that the findbar didn't open in the opened tabs because of a strange error: "this.browser is undefined".
This error pointed to the findbar.xml line 533 where the code tried to run _unpdateCaseSensitivity() but it couldn't get the "gFindBar._browser" prorperty.
Maybe it's because this property wasn't loaded for the original findbar object from the browser start (it was used by the overriden findbar object)...

So this is the point I stuck on...

And the question now is:
How can I delete the button without restart and so that the findbar opens?

mortalis
  • 2,060
  • 24
  • 34
  • 1
    I had same issue read this first, [ask.m.o :: Apply and remove XBL binding on runtime](https://ask.mozilla.org/question/297/apply-and-remove-xbl-binding-on-runtime/) then check out: [StackOverflow :: Dynamic way to unbind dynamically binded XBL](http://stackoverflow.com/questions/24403667/dynamic-way-to-unbind-dynamically-binded-xbl). You might find this helpful: [ask.m.o :: My XBL is not working on findbar](https://ask.mozilla.org/question/271/my-xbl-is-not-working-on-findbar/) – Noitidart Jan 07 '15 at 17:55
  • Thank you. It didn't solve my problem though added some new knowledge on this topic. – mortalis Jan 07 '15 at 19:25
  • But I noticed one interesting thing. If I start the browser and do not open the findbar and go to the Addon Manager and remove the addon and open the findbar in the first tab the button is disappeared... (I unregisted the stylesheet on remove, so the browser itself rebinds the findbar to its default .xml) – mortalis Jan 07 '15 at 19:26
  • The same thing happens with your MatchWord... So when the findbar is opening with the active addon it creates its objects (at least gFindBar) in the addon context. And after remove this context is unavailable but the findbar still refers to it. – mortalis Jan 07 '15 at 19:32
  • These are just my guesses. Maybe I'll finish these thoughts soon... – mortalis Jan 07 '15 at 19:33
  • And one more thing. I use the complete 'findbar' overriding so just copy its contents without methods to my .xml binding. So when I remove the addon with the previously opened findbar it just won't open in the same session (as I wrote above 'this.browser is null' message is in the Browser Console) – mortalis Jan 07 '15 at 19:36
  • And you use the 'findbar-container' rebind, so the button just isn't removed after the addon removing. But the findbar opens without problems... – mortalis Jan 07 '15 at 19:37
  • By the way. If you remove the addon and then open a new tab and open the findbar it doesn't contain the button. – mortalis Jan 07 '15 at 19:47
  • I tried to find another extension which uses XBL to add something to the findbar. For now found only Findbar Tweak. It removes its 'Find in tabs' button correctly. But there is a lot of modules which depend on each other. So it's hard to analyze the core of the button removal... – mortalis Jan 07 '15 at 19:51
  • Yeah I didn't implement the stuff from the topic. What the ask.m.o topic says is XBL is re-processed when one of those 3 actions happen. Then the StackOverflow topic says to add remove class to make it trigger the XBL re-process, this makes sense as that's one of the 3 actions that will trigger it. So I have to update MatchWord to apply class name and then remove it. Ill take a look at it right now actually. – Noitidart Jan 07 '15 at 20:54
  • 1
    Ok check out MatchWord shutdown-diff-binding branch it now removes the XBL on shtudown, its a hacky solution though. https://github.com/Noitidart/MatchWord/blob/shtudown-diff-binding/bootstrap.js#L37 see on shtudown it is registering another XBL binding that is blank, this forces the reflow to blank xbl which restores the original XBL. A quirk was that I had to delay the unregistering of the blank XBL by 500ms, using 0 or 10ms does not make it trigger the xbl reflow. can install addon from repo with this addon: https://addons.mozilla.org/en-US/firefox/addon/github-extension-installer/ – Noitidart Jan 07 '15 at 21:55
  • I also filed a bug here asking for a better way to force XBL reflow/reprocess: https://bugzilla.mozilla.org/show_bug.cgi?id=1118932 – Noitidart Jan 07 '15 at 21:57
  • Btw i really appreciate you sharing your thoughts on this, it shows me a lot, i thought a lot about this exact same situation too. Also i mentiond to install MatchWord from repo, you cando this with this addon: https://addons.mozilla.org/en-US/firefox/addon/github-extension-installer/ – Noitidart Jan 07 '15 at 23:15
  • Well your addon now works fine with the removal. But mine doesn't want to obey these instructions. Even with 2000 of delay... Maybe later I'll try to change my .xml so that it overrides the .findbar-container only, not the whole findbar. And then it should remove the button. – mortalis Jan 09 '15 at 10:33
  • Another thought is that the XBL and restartless extensions are not completely compatible. And XBL is for overlays and for bootstraps we have to use some direct methods to add/remove ui element (like document.createElement) – mortalis Jan 09 '15 at 10:35
  • I'll write the next comment in the answer section – mortalis Jan 09 '15 at 10:36

1 Answers1

1

From the Findbar Tweak addon I extracted this method to add new checkbox to the findbar (and changed it for my needs):

var findbar=window.gFindBar
var container = findbar.getElement("findbar-container")
var button = $('<toolbarbutton>')
button.setAttribute('anonid', 'test-find-tabs')
button.setAttribute('label', 'Test')
button.setAttribute('type', 'checkbox')
button.addEventListener('command', test,false)
container.insertBefore(button, findbar.getElement('find-case-sensitive').nextSibling)

But for my final purpose it needs some additional editings. So for now I'll leave this problem. Maybe a wise thought will occur me later. Cause a good idea often comes in mind after we change the focus from the problem and then return to it...

mortalis
  • 2,060
  • 24
  • 34
  • But this has to add add to every element manually and initialy the toolbar element doesnt exist until user opens it for first time in tab. Share with me your xml file you use and Ill get it to work in my MatchWord example. :) – Noitidart Jan 09 '15 at 14:18
  • Here I uploaded my simlified project: [Test Findbar](http://www.filedropper.com/testfindbar). Sorry cannot use GitHub cause have WindowsXP – mortalis Jan 09 '15 at 15:31
  • 2
    I added it to Github: [git - Test Findbar](https://github.com/mortalis13/Test-Findbar) – mortalis Feb 10 '15 at 12:46
  • I'll check it out today. – Noitidart Feb 10 '15 at 14:58
  • Why did you copy and paste the whole XBL of the original findbar instead of using ``? You can add your `find-test` check box in place of my Line 5 here: https://github.com/Noitidart/MatchWord/blob/shtudown-diff-binding/findbar.xml what was your reasoning for duplicating original XBL? – Noitidart Feb 10 '15 at 16:21