Toolbar Button On Install

Talk about add-ons and extension development.
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

I see now. It only happens when startup page isn't blank (I set it to blank every time and didn't mention it in steps to not-reproduce, my bad; actually it's a part of 2, create a new profile for me ;) ). I think the problem is that you're doing this on startup. Try using setTimeout or something. I'm not in the mood to debug this myself.
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post by TheBaker »

It's currently in the load event handler. Is there an event that fires after that, when everything has finished loading?
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post by TheBaker »

Ok, here's something weird. When you remove the button that is added, the address bar suddenly works again. Just like that!
And as for the timeout, I just tried it. All that happens is that it waits a couple of seconds before breaking.
Judging by this, I reckon it's not the fact that it's doing it on load, it must be something else.
Are there anyother extensions that add buttons to the toolbar?

Edit: I got the user confirm dialogue to work for adding the button
User avatar
Sephirot
Posts: 247
Joined: June 15th, 2004, 7:56 am

Post by Sephirot »

asqueella wrote:I see now. It only happens when startup page isn't blank (I set it to blank every time and didn't mention it in steps to not-reproduce, my bad; actually it's a part of 2, create a new profile for me ;) ). I think the problem is that you're doing this on startup. Try using setTimeout or something. I'm not in the mood to debug this myself.

:shock: I made the same mistake.
But it seems to work with the insertItem() method. Unfortunately the button will than be placed at the end of the nav-bar. :( Is there something like a moveItem() method?
Author of Bookmarks Menu Button, Autoclose Bookmark&History Folders and more

Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.19pre) Gecko/20110701 Firefox/3.6.19pre <-- build with MS VC++ 2010 SP1 and PGO on Win 7 x64
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post by TheBaker »

I don't mind where it is on the navbar, as long as it is on there. The reason why I switched from insertItem(), was that it didn't persist.
Know how I can make it persist?
User avatar
Sephirot
Posts: 247
Joined: June 15th, 2004, 7:56 am

Post by Sephirot »

TheBaker wrote:I don't mind where it is on the navbar, as long as it is on there. The reason why I switched from insertItem(), was that it didn't persist.
Know how I can make it persist?

http://forums.mozillazine.org/viewtopic.php?p=1103506#1103506

Code: Select all

document.getElementById("nav-bar").insertItem("smxtra-button); 
var currentset = document.getElementById("nav-bar").currentSet;
document.getElementById("nav-bar").setAttribute("currentset",currentset);
document.persist("nav-bar","currentset");
Author of Bookmarks Menu Button, Autoclose Bookmark&History Folders and more

Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.19pre) Gecko/20110701 Firefox/3.6.19pre <-- build with MS VC++ 2010 SP1 and PGO on Win 7 x64
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post by TheBaker »

You mean the answer was there all the time? Damn it! The one piece of code I didn't try, just happens to be the one that works. Thanks a lot!
User avatar
Sephirot
Posts: 247
Joined: June 15th, 2004, 7:56 am

Post by Sephirot »

Sephirot wrote:Unfortunately the button will than be placed at the end of the nav-bar. :( Is there something like a moveItem() method?


man, I always used the ID of the beforeElement which did not work ....

Code: Select all

var aBefore = document.getElementById("urlbar-container");
document.getElementById("nav-bar").insertItem("smxtra-button,aBefore);
var currentset = document.getElementById("nav-bar").currentSet;
document.getElementById("nav-bar").setAttribute("currentset",currentset);
document.persist("nav-bar","currentset");
Author of Bookmarks Menu Button, Autoclose Bookmark&History Folders and more

Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.19pre) Gecko/20110701 Firefox/3.6.19pre <-- build with MS VC++ 2010 SP1 and PGO on Win 7 x64
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

Is this line necessary?

Code: Select all

var currentset = document.getElementById("nav-bar").currentSet;
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post by TheBaker »

I looked in customniseToolbar.js and they had something similiar, so I assume so.
User avatar
Morac
Posts: 2519
Joined: February 9th, 2004, 8:20 pm
Contact:

Post by Morac »

Sephirot wrote:man, I always used the ID of the beforeElement which did not work ....


This is what I came up with which works. The only problems I've found is that if you use a preference to test if this is the first time the extension is run, the preference doesn't get cleared when the extension is uninstalled. This isn't a problem as long as the user doesn't modify the toolbar before reinstalling the extension. If they don't the button will reappear where it was last. If they do modify the toolbar, the button won't appear by default.

Code: Select all

// Add toolbar icon on first run (Firefox only)
function onInitToolbar()
{
   // Read preferences
   var nsIPrefServiceObj = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
   var nsIPrefBranchObj = nsIPrefServiceObj.getBranch("yourbranch.");

   //First Run?
   if(!nsIPrefBranchObj.prefHasUserValue("firstrun"))
   {

      // find address bar
      var urlbarNode = document.getElementById("urlbar-container");

      // Address bar found and button not already in a toolbar
      if ( (urlbarNode != null) && (document.getElementById("your-button") == null) )
      {
         // get address bar's toolbar
         var toolbarNode = urlbarNode.parentNode;

         // add your button in front of address bar
         if (toolbarNode.insertItem("your-button",urlbarNode,null,false))
         {
            // get toolbar's currentset and remove all instances of your button if already stored
            var currentset = toolbarNode.currentSet.replace(/your-button,?/gi,"");
            
            // remove end of line comma is there is one
            if (currentset.charAt(currentset.length-1) == ",") currentset = currentset.substring(0, currentset.length - 1);

            // add your button before address bar in currentset and make it persistent
            currentset = currentset.replace(/urlbar-container/i,"your-button,urlbar-container");
            toolbarNode.setAttribute("currentset",currentset);
            toolbarNode.currentSet = currentset;
            document.persist(toolbarNode.id,"currentset");
         }
      }
   }

   // set first run preference so we don't do this again
   nsIPrefBranchObj.setBoolPref("firstrun",true);
   
   // stop listening since no longer firstrun
   window.removeEventListener("load", onInitToolbar, false);
}

window.addEventListener("load", onInitToolbar, false);
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

The only thing is that removing event listener in a listener is a Bad Thing due to a bug. It's better to check/set a global boolean variable.
AnonEmoose
Posts: 2031
Joined: February 6th, 2004, 11:59 am

Post by AnonEmoose »

TheBaker wrote:Ok, I started a fresh profile, installed my extension, restarted. The button appeared on the toolbar like it should, but the still address bar wouldn't work (all the other buttons on the toolbar did, but the addressbar was blank and didn't change when i went to sites (via bookmarks, because the address bar wasn't working). It wouldn't allow me to type in and click go either. Which means I couldn't enter the URL you gave me. But you mentioned that it was called localstore.rdf, so I'm assuming you meant the file localstore.rdf that's in my profile directory, which contained:
<edit>
Hope that's what you wanted.
probably cause your <profile>hrome\overlayinfo\browser\content\overlays.rdf got messed up or the install didn't overlay it properly....

edited to add: u should still probably overlay the customizeToolbar.xul to be safe (and allow a user to remove the button)

as asquella said due to the event listner bug removing the listner as u have it is likeley to be ornery

Code: Select all

// stop listening since no longer firstrun
setTimeout("window.removeEventListener('load', onInitToolbar, false);", 0);


should work
wanderingstan
Posts: 9
Joined: September 15th, 2004, 3:08 am
Location: Osnabrück, Germany
Contact:

Post by wanderingstan »

I was happy to find this thread, thanks for the tips.
The function below puts your toolbar button there (after a specified element) if it's not already there, but leaves things alone otherwise. It just uses the code from above, and it's a bit of a hack, but it works for now:

Code: Select all

function displayToolbarButton(myButtonID, insertAfterID )
{
   // http://forums.mozillazine.org/viewtopic.php?t=189667
   var navToolbar = document.getElementById("nav-bar");
   if (navToolbar.currentSet.indexOf(myButtonID) == -1)
   {
      var set = navToolbar.currentSet
      var i = set.indexOf(insertAfterID);
      if (i == -1)
      {
         // can't find insertAfterID, so just insert at end of toolbar
         navToolbar.insertItem(myButtonID , null, null, false);
      }
      else
      {
         i += insertAfterID.length; // add _after_ given id
         set = set.slice(0,i) + "," + myButtonID + set.slice(i);
         navToolbar.currentSet = set;
      }
   }
}

displayToolbarButton("my-button","home-button");


The unfortunate side-effect is that it becomes impossible for the user to get rid of your button -- even if they drag it off, it will re-appear on the next window-launch. But then, if they don't want your extension, I guess they can just uninstall it? :)

-stan
wanderingstan
Posts: 9
Joined: September 15th, 2004, 3:08 am
Location: Osnabrück, Germany
Contact:

Disregard that last code...

Post by wanderingstan »

Looks like I spoke too soon: When you mess with currentSet directly as a string, the address bar might stop working.
And I'm an idiot: <a href="http://www.xulplanet.com/references/elemref/ref_toolbar.html#prop_insertItem">insertItem</a> already has a parameter for specifying where you want your button to appear. So my current workaround is to call this little function on each window launch:

Code: Select all

function displayToolbarButton(myButtonID, afterItemID)
{
   var navToolbar = document.getElementById("nav-bar");
   var afterItem = document.getElementById(afterItemID);
   if (navToolbar.currentSet.indexOf(myButtonID) == -1)
      navToolbar.insertItem(myButtonID, afterItem, null, false);
}
Post Reply