// ==UserScript==
// @name           Copy Tab Info
// @namespace      http://www.xuldev.org/
// @description    Enables to copy title and URL from right-click menu of a tab.
// @include        main
// @compatibility  Firefox 3.0, 3.5, 3.6b4
// @author         Gomita
// @version        1.0.20080201
// @homepage       http://www.xuldev.org/misc/ucjs.php
// ==/UserScript==

(function() {
	var htmlEscape = function(s) {
		s = s.replace(/&/g, "&amp;");
		s = s.replace(/>/g, "&gt;");
		s = s.replace(/</g, "&lt;");
		s = s.replace(/"/g, "&quot;");
		return s;
	};
	var copyTabInfo = function (aAsHTML) {
		var tab = document.popupNode;
		var title = tab.label;
		var url = gBrowser.getBrowserForTab(tab).contentWindow.location.href;
		var txt = aAsHTML ?
		          '<a href="' + htmlEscape(url) + '">' + htmlEscape(title) + '</a>' :
		          title + "\n" + url;
		Cc["@mozilla.org/widget/clipboardhelper;1"]
		.getService(Ci.nsIClipboardHelper)
		.copyString(txt);
	};
	var menuitem1 = document.createElement("menuitem");
	menuitem1.setAttribute("label", "Copy Title + URL");
	menuitem1.addEventListener("command", function() { copyTabInfo(false); }, false);
	var menuitem2 = document.createElement("menuitem");
	menuitem2.setAttribute("label", "Copy Title + URL (HTML)");
	menuitem2.addEventListener("command", function() { copyTabInfo(true); }, false);
	setTimeout(function() {
		gBrowser.mStrip.childNodes[1].appendChild(document.createElement("menuseparator"));
		gBrowser.mStrip.childNodes[1].appendChild(menuitem1);
		gBrowser.mStrip.childNodes[1].appendChild(menuitem2);
	}, 0);
})();
