⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 toolbar.js

📁 南开火狐
💻 JS
📖 第 1 页 / 共 2 页
字号:


/**
 * Global: for debug, to see where my code stops
 * */
const __ENABLE_BUS_STOP__ = true;

/**
 * bus stop, similar as alert, but dump message to console
 * @param {String} where message you want to dump
 * */
function bus(where) {
	if (__ENABLE_BUS_STOP__){
		dump("Bus Stop: " + where + "\n");
	}
}

var uti = {
	description : "Common Utilities",
};

// get toolbar string bundle
uti.sb = function(){
	return document.getElementById("nku_string_bundle")
}

// Opens a toolbar button automatically if another toolbar button is open on the toolbar
uti.openToolbarButton = function(currentToolbarButton) {
	// If the toolbar button is set and is not open
	if (currentToolbarButton && !currentToolbarButton.open) {
		var toolbarButton = null;
		var toolbarButtons = currentToolbarButton.parentNode
				.getElementsByTagName("toolbarbutton");
		var toolbarButtonsLength = toolbarButtons.length;

		// Loop through the toolbar buttons
		for (var i = 0;i < toolbarButtonsLength; i++) {
			toolbarButton = toolbarButtons.item(i);

			// If the toolbar button is set, is not the same toolbar button and is open
			if (toolbarButton && toolbarButton != currentToolbarButton
					&& toolbarButton.open) {
				toolbarButton.open = false;
				currentToolbarButton.open = true;
				break;
			}
		}
	}
}

// compose or get hidden browser
uti.getHiddenBrowser = function(){
		var hiddenBrowser = document.getElementById("nankai-toolbar-browser");
		if (hiddenBrowser == null){
			hiddenBrowser = document.createElement("browser");
			hiddenBrowser.setAttribute("id", "nankai-toolbar-browser");
			//hiddenBrowser.setAttribute("hidden", "true");
			hiddenBrowser.style.height = "0px";
			document.documentElement.appendChild(hiddenBrowser);
		}
		var atom = Components.classes['@mozilla.org/atom-service;1']
			                  .getService(Components.interfaces.nsIAtomService);  
		hiddenBrowser.documentCharsetInfo.forcedCharset = atom.getAtom("GB2312");
		return hiddenBrowser;
};


// get current window
uti.getCurrWindow = function() {
	var windowService = Components.classes["@mozilla.org/appshell/window-mediator;1"]
			.getService(Components.interfaces.nsIWindowMediator);
	return windowService.getMostRecentWindow("navigator:browser");
}

// open one url
uti.openURL = function(url) {
	var win = this.getCurrWindow();
	var gBrowser = win.getBrowser();
	
	if (gBrowser) {
		// test tab
		var testCreateTab = true;
		if (gBrowser.browsers.length == 1) {
			if ((gBrowser.currentURI == null)
					|| ((gBrowser.currentURI.spec == "") && (win.opener
							.getBrowser().selectedBrowser.webProgress.isLoadingDocument == true))
					|| (gBrowser.currentURI.spec == "about:blank")) {
				testCreateTab = false;
			}
		}
		if (testCreateTab == true) {
			var newTab = gBrowser.addTab(url);
			gBrowser.selectedTab = newTab;
		} else {
			gBrowser.loadURI(url);
		}
	} else {
		win.opener.open(url);
	}
}

uti.makeRequest = function (method, url, action, error, contents, headers) {
	var content = "";
	var request = new XMLHttpRequest();
	request.open(method, url, true);
	request.overrideMimeType("text/html;charset=gb2312");
	if (method == "POST") {
		for (var i in contents) {
			content += contents[i][0] + "="
					+ contents[i][1] + "&";
		}
		request.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded");
		request.setRequestHeader("Content-Length", content.length);
	}
	if (headers != null) {
		for (var i in headers) {
			request.setRequestHeader(headers[i][0], headers[i][1]);
		}
	}
	request.onload = function() {
		action(request);
	}
	/*
	 * request.onreadystatechange = function() { if (request.readyState==4) {
	 * //if (request.status==200) { action(request); //} } }
	 */
	if (error != null)
		request.onerror = error;

	request.send(content);
}

/**
 * NK Toolbar Preference methods
 * */
var prefs = {
	/**
	 * toolbar preference name prefix
	 * */
	_PREF_NAMESPACE : "nktoolbar.",
	/**
	 * get default preference of certain locale from string bundle
	 * not useful for nktoolbar now
	 * @param {String} key key of the locale default values
	 * */
	getLocaleDefault : function(key){
		if (this._stringBundle == null) {	
			// nktoolbar-local-prefs would be added later
			this._stringBundle = document.getElementById("nktoolbar-local-prefs");			
		}
		try {
			var value = this._stringBundle.getString(key + "_default");
		} catch (e) {
			return null
		}
		if (!value){return null}
		if (value.toLowerCase() == "true" || value.toLowerCase == "t")	{return "true"}
		if (value.toLowerCase() == "false" || value.toLowerCase == "f") {return "false"}
		return value;		
	}, 
	/**
	 * Set Preference of sinatoolbar
	 * @param {String} key key
	 * @param {String} value value
	 * */
	setStringPref : function(key, value) {
		var pref = this._getPreferences();
		pref.setCharPref(this._PREF_NAMESPACE + key, value);
	},
		/**
	 * Set Preference of sinatoolbar
	 * @param {String} key key
	 * @param {Boolean} value value
	 * */
	setBoolPref : function(key, value) {
		var pref = this._getPreferences();
		pref.setBoolPref(this._PREF_NAMESPACE + key, value);
	},
		/**
	 * Set Preference of sinatoolbar
	 * @param {String} key key
	 * @param {Int} value value
	 * */
	setIntPref : function(key, value) {
		var pref = this._getPreferences();
		pref.setIntPref(this._PREF_NAMESPACE + key, value);
	},
	
	/**
	 * Get string prefs of sinatoolbar
	 * @param {String} key key
	 * @returns {String} value
	 * */
	getStringPref : function(key) {
		var pref = this._getPreferences();
		try {
			return pref.getCharPref(this._PREF_NAMESPACE + key);
		} catch (e) {
			var value = this.getLocaleDefault(this._PREF_NAMESPACE + key);
			if (value) {
				pref.setCharPref(this._PREF_NAMESPACE + key, value);
			}
			return value;
		}
	},
	/**
	 * Get bool prefs of sinatoolbar
	 * @param {String} key key
	 * @returns {Boolean} value
	 * */
	getBoolPref : function(key) {
		var pref = this._getPreferences();
		try {
			return pref.getBoolPref(this._PREF_NAMESPACE + key);
		} catch (e) {
			var value = this.getLocaleDefault(this._PREF_NAMESPACE + key);
			if (value) {
				pref.setBoolPref(this._PREF_NAMESPACE + key, value);				
			}
			return value;
		}
	},
	/**
	 * Get int prefs of sinatoolbar
	 * @param {String} key key
	 * @returns {Int} value
	 * */
	getIntPref : function(key) {
		var pref = this._getPreferences();
		try{
			return pref.getIntPref(this._PREF_NAMESPACE + key);
		} catch (e) {
			var value = this.getLocaleDefault(this._PREF_NAMESPACE + key);
			if (value) {
				pref.setIntPref(this._PREF_NAMESPACE + key, value);
			}
			return value;
		}
	},
	
	/**
	 * Get preference service
	 * @returns {nsIPrefBrance} pref service branch
	 * */
	_getPreferences : function(){
		var pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPrefBranch);
		return pref;
	}
}

uti.onLoginDialogLoad = function() {
	document.getElementById("login_username").value = window.arguments[0].inn.i_user;
	document.getElementById("login_password").value = window.arguments[0].inn.i_pwd;
	
	var message = uti.sb().getString("LoginMessage-" + window.arguments[0].inn.i_type);
	var textDescription  = document.createTextNode(message);
	document.getElementById("login_description").appendChild(textDescription)
}

uti.onLoginDialogOK = function() {
	var type = window.arguments[0].inn.i_type;
	var user = document.getElementById("login_username").value;
	var pwd = document.getElementById("login_password").value;
	// save login
	if (document.getElementById("remember_login").checked) {
		prefs.setBoolPref("login." + type + ".remember", true);
		prefs.setStringPref("login." + type + ".default-user", user);
		prefs.setStringPref("login." + type + ".default-pwd", pwd);
	} else {
		prefs.setBoolPref("login." + type + ".remember", false);
	}
	
	window.arguments[0].out = { o_user : user, o_pwd : pwd};	
	window.close();
	window.opener.focus();			
}

/**
 * NK Toolbar 
 * */
function NKToolbar() {
	this.description = "Nankai Univ Toolbar Class";
}

/**
 * when toolbar initialized
 * */
NKToolbar.prototype.init = function() {
	// do something
}


/**
 * When click our logo, open homepage url for now
 * */
NKToolbar.prototype.about = function() {
	bus("about clicked");
	var aboutUrl = prefs.getStringPref("about.url");
	bus(aboutUrl);
	uti.openURL(aboutUrl)
}

/**
 * Add one query item to search history
 * @param {String} query query keyword to be stored
 * */
NKToolbar.prototype.addToHistory = function(query) {
	try {
		var no = prefs.getIntPref("search.history.number");
	} catch(e) {
		bus(e);
	}
	var newEntry = null;
	var history = "";
	var searchhistory = document.getElementById("search_history");
	
	// if current query exists, remove this history item
	for (var i = searchhistory.childNodes.length - 1; i >= 0; i--) {
		var node = searchhistory.childNodes.item(i);	
		if (query == node.getAttribute("label")) {
			searchhistory.removeChild(node);
		}
	}
	
	// insert to top history item
	newEntry = document.createElement("menuitem");
	newEntry.setAttribute("label", query);
	searchhistory.insertBefore(newEntry, searchhistory.firstChild);
	
	// update search history to preference
	for (var i = 0; i < searchhistory.childNodes.length && i < no; i++) {
		if (history != "") history += "|";
		history += escape(searchhistory.childNodes.item(i).getAttribute("label"));
	}
	prefs.setStringPref("search.history", history);
}

/**
 * Clear search history
 * @param {Boolean} isAll if true, also clear preference settings (not only current menulist)
 * */
NKToolbar.prototype.removeAllHistory = function(isAll) {
		
	// clear child nodes
	var searchHistory = document.getElementById("search_history");
	
	for (var i = searchHistory.childNodes.length - 1; i >= 0; i--) {
		searchHistory.removeChild(searchHistory.childNodes.item(i));
	}

	// clear history items from preference
	if (isAll) {
		prefs.setStringPref("search.history", "");	
		// clear textbox
		var searchTextbox = document.getElementById("search_textbox");
		searchTextbox.value = "";
	}
}

/**
 * fill search history
 * */
NKToolbar.prototype.fillHistory = function() {
	this.removeAllHistory(false);
	try {
		var history = prefs.getStringPref("search.history").split("|");
	} catch(e) {
		bus(e);
	}
	
	var searchhistory = document.getElementById("search_history");	
	var newEntry = null;
	for (var i = 0; i < history.length; i++){
		if (history[i] != "") {
			newEntry = document.createElement("menuitem");
			newEntry.setAttribute("label", unescape(history[i]));
			searchhistory.appendChild(newEntry);
		}
	}	
}

NKToolbar.prototype.ifEnterPressed = function(evt){
	if(evt.keyCode == 13)
		document.getElementById("range_popup").showPopup(
				document.getElementById("do_search"), -1, -1, "popup");	
}


NKToolbar.prototype.search = function(which) {
	var query = document.getElementById("search_textbox").value;
	
	// save current query to history
	this.addToHistory(query);
	
	// if no keyword
	if (query == "") {
		alert(uti.sb().getString("AlertNoKeyword"));
		document.getElementById("search_textbox").focus();
		return null;
	}
	var bbsUrl = "http://nkbbs.org/cgi-bin/bbs/bbsfind?day=" 
					+ prefs.getIntPref("search.nkbbs.how-many-days").toString();
	var musicUrl = "http://music.nankai.edu.cn/search.asp";
	var movieUrl = "http://movie.nankai.edu.cn/movie/sresult.php";
	var softUrl = "http://soft.nankai.edu.cn/ftdms/search.asp";
	var clubUrl = "http://www.12club.cn/search.asp";
	var googleUrl = "http://www.google.cn/search?hl=zh-CN";
	//query = encodeURIComponent(query);
	//alert(query);
	
	var site = which.split("_")[1];
	var branch = which.split("_")[2];
	
	switch (site) {
		case "nkbbs" : {
			if (branch == "title") {
				bbsUrl += "&title=" + query;
			} else {
				bbsUrl += "&user=" + query;
			}
			uti.openURL(bbsUrl);
			break;			
		};
		case "music" : {
			// make post content
			var content;
			switch (branch) {
				case "singer" :
					content = [["type", "%B8%E8%CA%D6"],
							["searchstring", escape(query)],
							["ok", "%CC%E1%BD%BB%B2%E9%D1%AF"]];
					break;
				case "song" :
					content = [["type", "%B8%E8%C7%FA"],
							["searchstring", escape(query)],
							["ok", "%CC%E1%BD%BB%B2%E9%D1%AF"]];
					break;
				case "uploader" :
					content = [["type", "%C9%CF%B4%AB%D5%DF"],
							["searchstring", escape(query)],
							["ok", "%CC%E1%BD%BB%B2%E9%D1%AF"]];
					break;
			};
			// music.nankai cannot search by directly GET,
			// so we first POST and get search response, 
			// then write the response in the new tab
			uti.openURL("");
			var doc = uti.getCurrWindow().getBrowser().contentDocument;
			doc.write("<div>" + uti.sb().getString("Loading") + "</div>");
			function openMusicNK(req) {
				doc.getElementsByTagName("div")[0].setAttribute("style", "display: none");
				doc.write("<base href='http://music.nankai.edu.cn/index.html'>");
				doc.write(req.responseText);
				doc.close();				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -