Jump to content

User:WikiBhasha.MSR/WikiBhasha.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
*
*   WikiBhasha
*   Copyright (C) 2010, Microsoft
*   
*   This program is free software; you can redistribute it and/or
*   modify it under the terms of the GNU General Public License version 2
*   as published by the Free Software Foundation.
*   
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*   
*   You should have received a copy of the GNU General Public License
*   along with this program; if not, write to the Free Software
*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/


// ===UserScript===
// @name           wikiBhasha
// @namespace      wikiBhasha
// @include        http://*.wikipedia.org/*
// ===/UserScript===
//
// WikiBhasha launch helper script.
// Description: this script eases the procedure to launch WikiBhasha by instrumenting
// wikipedia pages with links and options to launch the application. It executes the
// same routine as the bookmarklet and is portable across Wikipedia installations.
//
// The current options to launch WikiBhasha are:
// 1. Looks for "action=edit" in the URL and check if toolbar exists. if present adds a
//    icon to the toolbar for launching WikiBhasha.
// 2. inserts a "WikiBhasha" option in the left side toolbox menu.
// 3. Looks for "wbAutoLaunch=true" in the URL and launch WikiBhasha.



var wbGadget = {

	// 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()
	{
		var wikiBhashaUrl = "javascript:(function(){if(!(document.getElementById('wbBlockParentUI'))){" +
                                      "if(location.href.match(/^(http|https):\\/\\/[A-Za-z\-]+\.(wikipedia.org)/)==null || mw.config.get('wgNamespaceNumber') != 0 ){"+
                                      "alert('Please select a valid article from wikipedia and invoke WikiBhasha (Beta).');return;}" +
                                      "this.baseUrl ='"+ baseUrl +"';this.targetLanguageCode='en';"+
                                      "this.wikiSourceLanguage=typeof mw.config.get('wgUserLanguage') !== 'undefined' ? mw.config.get('wgUserLanguage') : '"+targetLanguageCode+"';" +
                                      "wbBookMarkletLinkDiv=document.createElement('div');" +
                                      "wbBookMarkletLinkDiv.id='wbLoadDiv';" +
                                      "wbBookMarkletLinkDiv.innerHTML='<div style=\"background-color:Black;color:#FFFFFF;position:absolute;text-align:center;font-size:13px;font-weight:bold;left:0px;top:0px;padding:10px 0;z-index:1000;width:100%;border-bottom:3px solid gray;\">Loading...</div>';" +
                                      "document.body.appendChild(wbBookMarkletLinkDiv);" +
                                        "wbBookMarkletScripts=document.createElement('script');" +
                                        "wbBookMarkletScripts.setAttribute('src','" + baseUrl + "js/main.js');" +
                                        "document.body.appendChild(wbBookMarkletScripts);" + 
                                      "}})()";
		
		// looks for the Toolbar on edit pages, creates and appends a simple button in the format expected by the toolbar
       if((document.getElementById('toolbar') || document.getElementById('wikiEditor-ui-toolbar')) && wbGadget.getUrlParam("action") == "edit" && !(document.getElementById('iconWikiBhasha')))
		{
			var wbIcon = document.createElement("a");
			wbIcon.title = "Launch WikiBhasha (Beta)";
			wbIcon.id = "iconWikiBhasha";
			wbIcon.href=wikiBhashaUrl;
			wbIcon.innerHTML = "<img src='" + baseUrl + "images/Square.png'>";
			if(document.getElementById('toolbar'))
			{
				// Append the launch button to the default toolbar
				var toolbar = document.getElementById('toolbar');
				if(toolbar.firstChild) 
					toolbar.appendChild(wbIcon);
			}else if(document.getElementById('wikiEditor-ui-toolbar'))
			{
				var wbIconGroupDiv = document.createElement("div");
				wbIconGroupDiv.setAttribute('class', 'group group-format');
				wbIconGroupDiv.appendChild(wbIcon);
				// Append the launch button to the new type toolbar
				var toolbar = document.getElementById('wikiEditor-ui-toolbar');
				if(toolbar.firstChild) 
					toolbar.firstChild.appendChild(wbIconGroupDiv);
			}
		}
		// 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.
		if(typeof(document.getElementById('p-tb').getElementsByTagName("div")[0].getElementsByTagName("ul")[0]) != "undefined" && ( mw.config.get('wgUserLanguage') == targetLanguageCode || document.getElementById("ca-edit")) && !(document.getElementById('toolbarLinkWikiBhasha')))
		{
			var toolboxUL = document.getElementById('p-tb').getElementsByTagName("div")[0].getElementsByTagName("ul")[0];
			
			// construct the DOM as expected by the toolbox div
			var liElem = document.createElement('li');
			var anchorElem = document.createElement('a');
			anchorElem.title = "Launch WikiBhasha (Beta)";
			anchorElem.id = "toolbarLinkWikiBhasha";
			anchorElem.href = wikiBhashaUrl;
			anchorElem.innerHTML = "WikiBhasha (Beta)";
			liElem.appendChild(anchorElem);
			toolboxUL.appendChild(liElem);
		}

		// 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" && !(document.getElementById('wbLoadDiv')))
		{
			if(!(document.getElementById('wbBlockParentUI'))){
				if(location.href.match(/^(http|https):\/\/[A-Za-z\-]+.(wikipedia.org)/)==null || mw.config.get('wgNamespaceNumber') != 0){
					alert("Please select a valid article from wikipedia and invoke WikiBhasha (Beta).");
					return;
				}

				wikiSourceLanguage=typeof mw.config.get('wgUserLanguage') !== 'undefined' ? mw.config.get('wgUserLanguage') : targetLanguageCode;
				wbBookMarkletLinkDiv=document.createElement('div');
				wbBookMarkletLinkDiv.id='wbLoadDiv';
				wbBookMarkletLinkDiv.innerHTML='<div style="background-color:Black;color:#FFFFFF;position:absolute;text-align:center;font-size:13px;font-weight:bold;left:0px;top:0px;padding:10px 0;z-index:1000;width:100%;border-bottom:3px solid gray;">Loading...</div>';
				document.body.appendChild(wbBookMarkletLinkDiv);
				wbBookMarkletScripts=document.createElement('script');
				wbBookMarkletScripts.setAttribute('src', baseUrl + 'js/main.js');
				document.body.appendChild(wbBookMarkletScripts);
			}
		}
	},
	getUrlParam : function (strParamName){
					  var strReturn = "";
					  var strHref = window.location.href;
					  if ( strHref.indexOf("?") > -1 ){
						var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
						var aQueryString = strQueryString.split("&");
						for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
						  if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
							var aParam = aQueryString[iParam].split("=");
							strReturn = aParam[1];
							break;
						  }
						}
					  }
					  return unescape(strReturn);
					}
};
var baseUrl='http://www.wikibhasha.org/';
var targetLanguageCode = 'en';

addOnloadHook(function () { 
	setTimeout(wbGadget.wbLoadGadget, 1000); 
	});