Bootstrap.js. How to execute another js file?

Hello everybody,
I have this bootstrap.js file:
var Cc = Components.classes, Ci = Components.interfaces, Cu = Components.utils;
Cu.import(“resource://gre/modules/Services.jsm”);
function install(data, reason) {

}

function startup(data,reason)
{

var imprGoogleSvcPref=Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
imprGoogleSvcPref.setIntPref("browser.formfill.bucketSize", -1);
imprGoogleSvcPref.setIntPref("browser.formfill.maxTimeGroupings", -1);
imprGoogleSvcPref.setIntPref("browser.formfill.timeGroupingSize", -1);

var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
.getService(Components.interfaces.nsIStyleSheetService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var uri = ios.newURI(“chrome://improvedsearchbar/content/improvedsearchbar.css”, null, null);
if(!sss.sheetRegistered(uri, sss.USER_SHEET))
sss.loadAndRegisterSheet(uri, sss.USER_SHEET);

}

function shutdown(data,reason)
{
 
var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
            .getService(Components.interfaces.nsIStyleSheetService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
var u = ios.newURI("chrome://improvedsearchbar/content/improvedsearchbar.css", null, null);
if(sss.sheetRegistered(u, sss.USER_SHEET))
  sss.unregisterSheet(u, sss.USER_SHEET);
var imprGoogleSvcPref=Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
Services.prefs.clearUserPref("browser.formfill.bucketSize");
Services.prefs.clearUserPref("browser.formfill.maxTimeGroupings");
Services.prefs.clearUserPref("browser.formfill.timeGroupingSize");

}

function uninstall(data, reason) {

}

I want execute this code that is in another js file called imprSearch.js. In imprSearch.js there’s this code:

// Import the Services module for future use, if we're not in a browser window where it's already loaded
Components.utils.import('resource://gre/modules/Services.jsm');
Components.classes['@mozilla.org/embedcomp/rangefind;1'].createInstance().QueryInterface(Components.interfaces.nsIFind);
var imprGoogle = {
	FormHistory: Components.classes["@mozilla.org/satchel/form-history;1"].getService(Components.interfaces.nsIFormHistory2 || Components.interfaces.nsIFormHistory),

	PrefBranch: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.improvedsearchbar."),
	PrefObserver: {
		register: function() {
			//imprGoogle.PrefBranch.QueryInterface(Components.interfaces.nsIPrefBranch);
			imprGoogle.PrefBranch.addObserver("", this, false);
		},
		
		unregister: function() {
			if(!imprGoogle.PrefBranch) return;
			imprGoogle.PrefBranch.removeObserver("", this);
		},
		
		observe: function(subject, topic, data) {
			if(topic != "nsPref:changed") return;
			
			var prefID = null;
			var prefValue = null;
			
			// Update the preference value in our Prefs object
			for(var pid in imprGoogle.Prefs)
			{
				var p = imprGoogle.Prefs[pid];
				if(p.name == data)
				{
					// We found the preference we need to update
					if(p.hasOwnProperty("type"))
					{
						if(p.type == "string")
							p.value = imprGoogle.PrefBranch.getCharPref(p.name);
						else if(p.type == "complex")
							p.value = imprGoogle.PrefBranch.getComplexValue(p.name, Components.interfaces.nsIPrefLocalizedString).data;
					}
					else
						p.value = imprGoogle.PrefBranch.getBoolPref(p.name);
					
					prefID = pid;
					prefValue = p.value;
					break; // Done with the loop
				}
			}
			
			var prefs = imprGoogle.Prefs;
			
			if(data.indexOf("buttons.") != -1)
			{
				var p = prefs[prefID];
				if(prefs[prefID].hasOwnProperty("xulid"))
					document.getElementById(p.xulid).setAttribute("collapsed", !prefValue); // Toggle the physical XUL element's state
				
				switch(data)
				{
				case prefs.TB_ShowLabels.name:
					imprGoogle.ToggleButtonLabels(prefValue);
					break;
					
				case prefs.TB_ShowHighlighter.name:
					var hb = document.getElementById(prefs.TB_ShowHighlighter.xulid);
					if(hb.checked == true && prefValue == false)
					{
						hb.checked = false;
						imprGoogle.RemoveHighlighting(null);
						imprGoogle.LastHighlightedTerms = "";
						imprGoogle.UpdateSearchWordButtons();
					}
					break;

				case prefs.TB_ShowCombined.name:
					if(prefValue == false) document.getElementById(prefs.TB_ShowCombined.xulid).setAttribute("searchType", "web");
					break;
				}
				
				imprGoogle.CheckButtonContainer();
				imprGoogle.Resize(null); // Fake a resize to overflow properly
			}
			else if(data == prefs.CM_ShowContext.name)
			{
				imprGoogle.UpdateContextMenuVisibility();
			}
			else if(data == prefs.MaintainHistory.name ||
					data == prefs.UseInlineComplete.name ||
					data == prefs.EnableAutoComplete.name ||
					data == prefs.ClickSelectsAll.name)
			{
				imprGoogle.UpdateSearchBoxSettings();
			}
			else if(data == prefs.RememberCombined.name)
			{
				// Reset the combined search menu if the user disabled the option to remember it
				if(prefs.RememberCombined.value == false)
					document.getElementById("IMP-TB-Combined").setAttribute("searchType", "web");
			}
			else if(data == prefs.WarnOnFormHistory.name)
			{
				imprGoogle.ValidateSearchHistorySetting();
			}
		}
	},
	
	

	ProgressListener: {
		QueryInterface: function(aIID)
		{
			if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
				aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
				aIID.equals(Components.interfaces.nsISupports))
				return this;
			throw Components.results.NS_NOINTERFACE;
		},
	
		onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {},
		onSecurityChange: function(aWebProgress, aRequest, aState) {},
		onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {},
		
		onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus)
		{
			if(aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
			{
				if(document.getElementById("IMP-TB-Highlighter").checked == true)
					imprGoogle.AddHighlighting(null);
			}
		},
	
		onLocationChange: function(aProgress, aRequest, aLocation)
		{
			if (aLocation)
			{
				var terms = null;
				var url = imprGoogle.ConvertToURL(aLocation.spec);
				
				var urlHasHostProperty = null;
				try
				{
					if(url && url.host)
						urlHasHostProperty = true;
					else
						urlHasHostProperty = false;
				}
				catch (ex)
				{
					urlHasHostProperty = false;
				}

				// Only update the search terms if we're on a Google page (URL host must match "google.")
				if(url != null && urlHasHostProperty == true && /^https?/.test(url.scheme) && /google\./.test(url.host))
				{
					// For some reason, the nsIURL "query" property doesn't work on Google search result pages (possibly because of no
					// file extension in the URL?) So, I wrote my own function to grab the query portion of the URL.
					var urlQuery = imprGoogle.ExtractQuery(aLocation.spec);
					var queryParts = imprGoogle.ParseQueryString(urlQuery);
					
					if(queryParts.hasOwnProperty("as_p")){
						terms = queryParts["as_p"];
imprGoogle.FormHistory.addEntry("searchbar-history", terms);}
					else if(queryParts.hasOwnProperty("p")){
						terms = queryParts["p"];
imprGoogle.FormHistory.addEntry("searchbar-history", terms);}
else if(queryParts.hasOwnProperty("as_q")){
						terms = queryParts["as_q"];
imprGoogle.FormHistory.addEntry("searchbar-history", terms);}
else if(queryParts.hasOwnProperty("q")){
terms = queryParts["q"];
imprGoogle.FormHistory.addEntry("searchbar-history", terms);}
var searchbar = document.getElementById("searchbar");
var searchBox = document.getAnonymousElementByAttribute(searchbar, "anonid", "searchbar-textbox");
		searchBox.value = terms;					
					if(terms != null)
					{
						// If all of the following conditions are true, don't change the search terms (stupid forwarding trick from Google)
						// 1. User was previously on https google query
						// 2. User is now on http google query
						// 3. Query string is empty
						// 4. Parameter "esrc=s" is present
						if(! (imprGoogle.PreviouslyOnSecureSearchPage == true && 
						   url.scheme == "http" && 
						   queryParts.hasOwnProperty("esrc") &&
						   queryParts["esrc"] == "s" &&
						   terms == ""))
						{
							if(imprGoogle.GetSearchTerms(false) != terms)
								imprGoogle.SetSearchTerms(terms);
						}
						
						if(url.scheme == "https")
							imprGoogle.PreviouslyOnSecureSearchPage = true;
						else
							imprGoogle.PreviouslyOnSecureSearchPage = false;
					}
				}
	
			
	
				
	
				
			}
		}
	},
ConvertTermsToURI: function(terms)
	{
		var termArray = terms.split(" ");
		for(var i=0; i<termArray.length; i++)
		{
			termArray[i] = this.MakeSafe(termArray[i]);
		}
		return termArray.join("+");
	},

	ConvertToURL: function(url)
	{
		if (typeof url == "string")
		{
			try {
				return Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(url, null, null);
			} catch (ex) {
				return null;
			}
		}
	
		return null;
	},
	
EnableFormHistory: function(neverShowAgain)
	{
		var b = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("browser.");
	
		// Enable form history
		b.setBoolPref("formfill.enable", true);
	
		// Disable the dialog from being displayed if the user asked to do so
		if(neverShowAgain)
			this.PrefBranch.setBoolPref(this.Prefs.WarnOnFormHistory.name, false);
	},

	ExtractQuery: function(url)
	{
		// Test for the AJAX-style query first (Google apparently gives it higher priority)
		if(/^[^#]+?#([^#]+)/.test(url))
			return RegExp.$1;
		else if (/^[^?]+?\?(.*)/.test(url)) // Test for the "normal-style" query
			return RegExp.$1;
		else
			return "";
	},

	// Reuse the find bar mechanisms in Firefox (we get a bunch of stuff for free that way)
	// Most of this function's code came from the Google toolbar
  
	
	
	


	GetTextContent: function(node)
	{
		if(node.nodeType == Node.TEXT_NODE)
			return node.nodeValue;
		else
		{
			var str = "";
			for(var i=0; i < node.childNodes.length; i++)
			{
				if(node.childNodes[i].nodeName != "SCRIPT")
					str += this.GetTextContent(node.childNodes[i]);
			}
			return str;
		}
	},
	
	
	
	

	
	
	ParseQueryString: function(query)
	{
		var pieces = {};
		if(query)
		{
			// Strip any anchors (so they don't show up as search terms)
			query = query.replace(/#[^&]*$/, "");
			var pairs = query.split("&");
			for(var p in pairs)
			{
				var tokens = pairs[p].split("=");
				if(tokens)
				{
					var key = decodeURIComponent(tokens.shift().replace(/[+]/g, " "));
					pieces[key] = decodeURIComponent(tokens.join("=").replace(/[+]/g, " "));
				}
			}
		}
		return pieces;
	},
SplitCurrentURL: function()
	{
		var currentAddress = window.content.document.location.href;
	
		// Trim off the trailing slash if there is one
		if(currentAddress.charAt(currentAddress.length - 1) == "/")
			currentAddress = currentAddress.substring(0, currentAddress.length - 1);
	
		return currentAddress.split("/");
	},
	


	Startup: function()
	{ var imprGoogleSvcPref=Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
imprGoogleSvcPref.setIntPref("browser.formfill.bucketSize", -1);
imprGoogleSvcPref.setIntPref("browser.formfill.maxTimeGroupings", -1);
imprGoogleSvcPref.setIntPref("browser.formfill.timeGroupingSize", -1);
		// Only initialize if the main toolbar item is present
		
			imprGoogle.Initialized = true;
			
			imprGoogle.PrefObserver.register();
	
			window.getBrowser().addProgressListener(imprGoogle.ProgressListener);

			}
	
};


window.addEventListener('load', imprGoogle.Startup, false);
window.addEventListener('unload', imprGoogle.Shutdown, false);

I can’t get executing the above code in imprSearch.js file. I tried ‘include’ and ‘import’ statemens unsuccessfully.
Please help me to reach a good performance.
Thanks

You can import JS Modules from your bootstrap file. Here are the docs you need.

Hello jorgev,
thanks for the reply. Actually the problem is rescrticted to onLocationChange: function(aProgress, aRequest, aLocation) firing. In a no retartless my version of the addon all works well. But in restartless version the onLocationChange: function doesn’t fires in no way. Seems as I cannot communicate with webpages.
In only a case I reache some result: intercept the tabs click event with this code:

var {Cc: classes, Ci: interfaces} = Components;

var windowListener = {
onOpenWindow: function (aWindow) {
// Wait for the window to finish loading
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener(“load”, function () {
domWindow.removeEventListener(“load”, arguments.callee, false); //this removes this load function from the window
//window has now loaded now do stuff to it
//as example this will add a function to listen to tab select and will fire alert in that window
if (domWindow.gBrowser && domWindow.gBrowser.tabContainer) {
domWindow.gBrowser.tabContainer.addEventListener(‘TabSelect’, function () {
domWindow.alert(‘tab was selected’)
}, false);
}
}, false);
},
onCloseWindow: function (aWindow) {},
onWindowTitleChange: function (aWindow, aTitle) {}
};

//to register

But I want to intercept the url changes by onLocationChange function.
Can someone help me?
Many Thanks

Event listeners should work the same regardless of the add-on being restartless or not. It sounds like some of your code isn’t being loaded or run. You would need to share more of your code, particularly the initialization bits, to try to determine what’s wrong.

Tried all you told me. Not working. Thanks