User:Wb user/attachwb.js

var debugMode = false;

if(debugMode) {	unsafeWindow.wb = function { this.baseUrl='http://localhost/wikiBabel/';this.targetLanguageCode='en';s=document.createElement('div');s.id='wikiBabelLoadDiv';s.innerHTML='Loading... ';document.body.appendChild(s);s=document.createElement('script');s.setAttribute('language', 'javaScript');s.setAttribute('src','http://localhost/wikiBabel/scripts/jquery-1.3.2.min.js');document.body.appendChild(s);s=document.createElement('script');s.setAttribute('language', 'javaScript');s.setAttribute('src','http://localhost/wikiBabel/scripts/wikiBabel.startup.js');document.body.appendChild(s);} } else {	window.wb = function { this.baseUrl='http://localhost/wikiBabel/';this.targetLanguageCode='en';s=document.createElement('div');s.id='wikiBabelLoadDiv';s.innerHTML='Loading... ';document.body.appendChild(s);s=document.createElement('script');s.setAttribute('language', 'javaScript');s.setAttribute('src','http://localhost/wikiBabel/scripts/jquery-1.3.2.min.js');document.body.appendChild(s);s=document.createElement('script');s.setAttribute('language', 'javaScript');s.setAttribute('src','http://localhost/wikiBabel/scripts/wikiBabel.startup.js');document.body.appendChild(s);} }

var wbGadget = { addHandler : function(element, signal, callback) {		if(debugMode) {			unsafeWindow.addHandler(element, signal, callback); }		else {			window.addHandler(element, signal, callback); }	},

// wbLoadGadget is executed after the page finishes loading. It looks for specific // indicators and determines how to instrument the page with different options to launch // the application wbLoadGadget : function {		// Option 1. Check if there's a parameter in the URL wbAutoLaunch = true // This is going to be used by the main application when the user launches it from an article // page, and will prevent the need for the user to click on the bookmarklet/button again

if(wbGadget.getUrlParam("wbAutoLaunch") == "true") {			// check for the GET parameter "wbAutoLaunch=true" wbGadget.startWB; }

// looks for the Toolbar on edit pages, creates and appends a simple button in the format // expected by the toolbar var isInIFrame = (window.location != window.parent.location) ? true : false; if(document.getElementById('toolbar') && wbGadget.getUrlParam("action") == "edit" && !isInIFrame) {			wbButton = document.createElement("a"); wbButton.title = "Launch WikiBabel"; wbButton.href="#"; wbButton.addEventListener('click', wbGadget.startWB, true) //this.addHandler(wbButton, 'click', wbGadget.startWB); wbImage = document.createElement('img'); // This image is a placeholder for the icon of the button to launch WikiBabel // Currently, it points to the "Blockquote" button which is already existing in the toolbar wbImage.src = "http://upload.wikimedia.org/wikipedia/en/f/fd/Button_blockquote.png"; wbButton.appendChild(wbImage); // Append the launch button to the toolbar var toolbar = document.getElementById('toolbar'); toolbar.appendChild(wbButton); }

// Checks for the "toolbox" section, usually present on lower left side of the page. This is displayed only if the // article page is being viewed and not if it's on an edit page. else if(typeof(document.getElementById('p-tb').getElementsByTagName("div")[0].getElementsByTagName("ul")[0]) != "undefined" && document.getElementById("ca-edit")) {			var toolboxUL = document.getElementById('p-tb').getElementsByTagName("div")[0].getElementsByTagName("ul")[0]; // construct the DOM as expected by the toolbox div liElem = document.createElement('li'); anchorElem = document.createElement('a'); anchorElem.title = "Launch WikiBabel"; anchorElem.href = "#"; anchorElem.innerHTML = "wikiBabel"; // Check for a click and hook into starting WikiBabel anchorElem.addEventListener('click', wbGadget.startWB, true); //this.addHandler( anchorElem, 'click', wbGadget.startWB);

liElem.appendChild(anchorElem); toolboxUL.appendChild(liElem); }	},	// This function launches the application by executing the same code as the bookmarklet. // Please note that any change to the bookmarklet must be reflected here startWB : function(e) {		if(debugMode) {		if(!(document.getElementById('wikiBabelBlockParentUI'))){if (document.readyState) {if (document.readyState == 'loaded' || document.readyState == 'complete') { unsafeWindow.wb; }} else {unsafeWindow.wb;}} }		else {		if(!(document.getElementById('wikiBabelBlockParentUI'))){if (document.readyState) {if (document.readyState == 'loaded' || document.readyState == 'complete') { window.wb; }} else {window.wb;}} }

},

// retrieve the GET parameters from the URL. // Arguments: //    name - the name of the parameter // Returns: //    string value of the parameter if it exists. null if it doesn't.	getUrlParam: function(name) {		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = ""; if(debugMode) {			results = regex.exec( unsafeWindow.location.href); }		else {			results = regex.exec( window.location.href ); }		if( results == null ) return ""; else return results[1]; },	// This is a helper function for the interval loader. Checks for "hookEvent" as an indicator // that the page has been loaded. //	// Useful if the script is injected externally or running standalone, and not as a gadget // NOTE: this function is called only in debug mode CheckLoad : function {		if(typeof(unsafeWindow.hookEvent) == "function") {			window.clearInterval(intervalID); wbGadget.wbLoadGadget; }	} };

// Entry point of the script if(debugMode) {	// to be used for development, greasemonkey scripts, etc // wherever you might expect window.hookEvent to not be defined when the script is	// being executed var intervalID = unsafeWindow.setInterval(wbGadget.CheckLoad, 100); } else {	addOnloadHook(wbGadget.wbLoadGadget); }