Jump to content

User:Anilande/dr.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anilande (talk | contribs) at 12:22, 13 January 2011. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// <pre><nowiki>
//  _________________________________________________________________________________________
// |                                                                                         |
// |                    === WARNING: GLOBAL GADGET FILE ===                                  |
// |                  Changes to this page affect many users.                                |
// | Please discuss changes on the talk page or on [[Wikipedia_talk:Gadget]] before editing. |
// |_________________________________________________________________________________________|
//
// Imports [[User:Anilande/dRin.js]]
// </nowiki></pre>

// ===UserScript===
// @name           DRin
// @namespace      DRin
// @include        http://*.wikipedia.org/*
// ===/UserScript===
//
// DRin launch helper script.
// Description: this script eases the procedure to launch DRin by instrumenting
// wikipedia pages with links and options to launch the application. 
//
// The current options to launch DRin are:
// click the 'View Visually' option in the left side toolbox menu and 'View Visually' tab on top tabs list.

var ajaxRequest = {
	createXMLHTTPObject : function (){
		var activexmodes=["Msxml2.XMLHTTP", "Msxml3.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
		if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
			for (var i=0; i<activexmodes.length; i++){
				try{
					return new ActiveXObject(activexmodes[i])
				}
				catch(e){
					continue;
				}
				break;
			}
		}else if (window.XMLHttpRequest){
			return new XMLHttpRequest();
		}else{
			return false;
		}
	},
	sendAjaxRequest : function(url, callback, method) {
		var req = ajaxRequest.createXMLHTTPObject();
        if (!req) {
            return;
        }
        req.open("GET","http://msri-digitalpro/rin/Cache/Foobar.xml",true);
		req.setRequestHeader("Cache-Control", "no-cache");
		req.setRequestHeader("Pragma", "no-cache");
        req.onreadystatechange = function () {
            var response = {
                status: req.status,
                statusText: req.statusText,
                readyState: req.readyState,
                responseText: req.responseText,
                responseHeaders: req.responseHeaders,
                finalUrl: url
            };

            if ((req.status !== 200 && req.status !== 304)  || req.readyState !== 4) {
                return;
            }

            //callback(response);
        };
        if (req.readyState === 4) {
            return;
        }
        req.send(null);
	}
};


var drGadget = {
	
	drUrl: null,
	ulEle: null,

	// 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
	drLoadGadget : function()
	{
		drGadget.naritiveViewTab();
		// looks for the Toolbar on edit pages, creates and appends a simple button in the format expected by the toolbar
       if(document.getElementById('p-views') || document.getElementById('p-cactions'))
		{
		    if(document.getElementById('p-views')){
				drGadget.ulEle = document.getElementById('p-views').getElementsByTagName("ul")[0];
			}else if(document.getElementById('p-cactions')){
				drGadget.ulEle = document.getElementById('p-cactions').getElementsByTagName("ul")[0];
			}
			drGadget.showNaritiveEditTab();
			drGadget.showNaritiveViewTab();
		}
	},
	showNaritiveViewTab : function(){
		var liVElem = document.createElement('li');
		liVElem.id = 'tabVDRin';
		var anchorVElem = document.createElement('a');
		anchorVElem.title = "View Narrative";
		anchorVElem.id = "toolbarLinkDRin";
		anchorVElem.innerHTML = "<span>View Narrative</span>";
		anchorVElem.href = drGadget.drUrl;
		liVElem.appendChild(anchorVElem);
		drGadget.ulEle.insertBefore(liVElem, drGadget.ulEle.firstChild);
		return true;
	},
	showNaritiveEditTab : function(){
		var liEElem = document.createElement('li');
		liEElem.id = 'tabEDRin';
		var anchorEElem = document.createElement('a');
		anchorEElem.title = "Edit Narrative";
		anchorEElem.id = "toolbarLinkDRin";
		anchorEElem.innerHTML = "<span>Edit Narrative</span>";
		anchorEElem.href = "javascript:editNaritive(rinXMLData);";
		liEElem.appendChild(anchorEElem);
		if(document.getElementById('p-views')){
			var liElem = document.getElementById('p-views').getElementsByTagName("ul")[0].getElementsByTagName("li")[2];
		}else if(document.getElementById('p-cactions')){
			var liElem = document.getElementById('p-cactions').getElementsByTagName("ul")[0].getElementsByTagName("li")[2];
		}
		drGadget.ulEle.insertBefore(liEElem, liElem);
		//ajaxRequest.sendAjaxRequest("http://msri-digitalpro/rin/Cache/Foobar.xml", drGadget.populateRTEditor, 'GET');
		return true;
	},
	getCurrentArticleTitle: function() {      
		if (typeof wgTitle !== "undefined") {
			if (wgAction === "view" && wgArticleId === 0 && wgIsArticle === false) {
				return false;
			}
			//in addition to the page title, get the name space also if it exists.
			if (typeof wgCanonicalNamespace !== "undefined" && wgCanonicalNamespace.length > 0) {
				return wgCanonicalNamespace + ":" + wgTitle;
			}
			return wgTitle;
		}
	},
	naritiveViewTab : function(){
		var title = drGadget.getCurrentArticleTitle();
		if(title){
			docHeight = ((document.height - 100)<700)?(document.height - 100):700;
			drGadget.drUrl = 'javascript:(function(){ document.getElementById("content").innerHTML = "<iframe src=\'http://msri-digitalpro/rin/?PlayerMode=Dynamic&NarrativeId='+title+'\' width=\'100%\' height=\''+docHeight+'px\'></iframe>"; selectTab("tabVDRin");})();';
			return true;
		}else{
			return false;
		}
	},
	populateRTEditor : function(responce){
		rinXMLData = responce.responseText;
	}
};

var rinXMLData = '';

selectTab = function(tabObj) {      
	document.getElementById("tabEDRin").className=""; 
	document.getElementById("tabVDRin").className=""; 
	document.getElementById("ca-view").className=""; 
	document.getElementById("ca-edit").className=""; 
	document.getElementById("ca-history").className="";
	document.getElementById(tabObj).className="selected"; 
};

editNaritive = function(rinXMLData){
	document.getElementById("content").innerHTML = "<textarea>"+rinXMLData+"</textarea>";
	selectTab('tabEDRin');
};

setTimeout(drGadget.drLoadGadget,1000);