User:WikiBhasha.MSR/WikiBhasha.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:WikiBhasha.MSR/WikiBhasha. |
/*
*
* 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);
});