User:Hawkbot/global.js

/************************ Browser-based Bot API Interface ************************ * This file can be imported to any global.js file to enable use of the script. * * This bot is intended to be used for helping wikis - not destroying them. * * Please respect local rules when using this bot. * * - * * Written and maintained by Monchoman45. * *********************************************************************************/ console.log('Loaded: B3.js'); window.B3 = { extensions: {}, documentation: {}, onload: [], onbeforeunload: [], active: false, init: false }; //APIQuery importScriptURI('http://monchbox.wikia.com/index.php?title=MediaWiki:APIQuery.js&action=raw&ctype=text/javascript'); //Tasks importScriptURI('http://monchbox.wikia.com/index.php?title=MediaWiki:B3.js/Tasks.js&action=raw&ctype=text/javascript'); //CVN importScriptURI('http://monchbox.wikia.com/index.php?title=MediaWiki:B3.js/CVN.js&action=raw&ctype=text/javascript'); //Maintenance importScriptURI('http://monchbox.wikia.com/index.php?title=MediaWiki:B3.js/Maintenance.js&action=raw&ctype=text/javascript'); //Chatbot importScriptURI('http://monchbox.wikia.com/index.php?title=MediaWiki:B3.js/Chatbot.js&action=raw&ctype=text/javascript'); B3.setup = function { B3.init = true; if(wgCanonicalNamespace == 'Special' && wgTitle == 'B3') { document.title = 'B3 Terminal'; document.head.innerHTML += '\n#B3Sidebar {\n\twidth:122px;\n\tfloat:right;\n\tposition:relative;\n}\n#B3Sidebar a {\n\tcursor:pointer;\n}\n.skin-oasis #B3Windows {\n\twidth:840px;\n}\n.skin-monobook #B3Windows {\n\twidth:930px;\n}\n#doc-header {\n\tmargin-bottom:7px;\n}\n#doc-path {\n\tfloat:right;\n\ttext-transform:capitalize;\n\tmargin-top:1em;\n}\n '; if(document.body.className.indexOf('skin-oasis') != -1) { var body = 'WikiaArticle'; if(document.getElementById('AdminDashboardHeader')) { document.getElementById('WikiaArticle').getElementsByTagName('h1')[0].innerHTML = wgPageName; }			else { document.getElementById('WikiaPageHeader').getElementsByTagName('h1')[0].innerHTML = wgPageName; document.getElementById('WikiaPageHeader').getElementsByTagName('h2')[0].innerHTML = 'Version 2.0'; }		}		else { var body = 'bodyContent'; document.getElementById('firstHeading').innerHTML = wgPageName; }		var links = ''; var windows = ''; for(var i in B3.extensions) { links += ' ' + i + ' '; windows += ' '; }		document.getElementById(body).innerHTML = (document.getElementById('AdminDashboardHeader') ? document.getElementById('WikiaArticle').getElementsByTagName('h1')[0].outerHTML : '') + ' ' + links + ' Documentation ' + windows + '       '; for(var i in document.getElementById('B3Sidebar').getElementsByTagName('a')) { document.getElementById('B3Sidebar').getElementsByTagName('a')[i].onclick = function(event) { var divs = document.getElementById('B3Windows').getElementsByTagName('div'); for(var i in divs) { if(divs[i].parentNode == document.getElementById('B3Windows')) {divs[i].style.display = 'none';} }				document.getElementById(this.innerHTML.toLowerCase).style.display = 'block'; }		}		document.getElementById('B3Windows').getElementsByTagName('div')[0].style.display = 'block'; window.onbeforeunload = function { for(var i = 0; i < B3.onbeforeunload.length; i++) { if(typeof B3.onbeforeunload[i] == 'function') {try {B3.onbeforeunload[i];} catch(err) {console.log('Error in function at index ' + i + ': ' + err);}} }			if(B3.active) {return 'One or more B3 modules are currently performing actions. If you leave this page now, all pending tasks will cease, and will not be recovered the next time you return to a B3 interface.';} }		B3.view('doc/'); }	for(var i = 0; i < B3.onload.length; i++) { if(typeof B3.onload[i] == 'function') {try {B3.onload[i];} catch(err) {console.log('Error in function at index ' + i + ': ' + err);}} } } B3.registerExtension = function(extension, data) { B3.extensions[extension] = data; B3.onload.push(B3[extension].onload); B3.documentation[extension] = B3[extension].documentation; if(B3.init && document.getElementById('B3Windows')) { document.getElementById('B3SidebarModules').innerHTML += ' ' + extension + ' '; document.getElementById('B3Windows').innerHTML += ' '; document.getElementById('B3SidebarModules').lastChild.getElementsByTagName('a')[0].onclick = function(event) { var divs = document.getElementById('B3Windows').getElementsByTagName('div'); for(var i in divs) { if(divs[i].parentNode == document.getElementById('B3Windows')) {divs[i].style.display = 'none';} }			document.getElementById(this.innerHTML.toLowerCase).style.display = 'block'; }	} } B3.setActive = function(extension, val) { B3[extension].active = val; if(val) { B3.active = true; }	else { for(var i in B3.extensions) { if(B3[i].active) {return;} }		B3.active = false; } } B3.view = function(page) { if(page.toLowerCase.startsWith('doc/')) { var path = page.split('/'); var obj = B3.documentation; var pathlink = 'doc/'; var pathtext = 'Doc / '; for(var i = 0; i < path.length; i++) { if(path[i] && obj[path[i]]) { pathlink += path[i] + '/'; pathtext += '' + path[i] + ' / '; obj = obj[path[i]]; if(obj.content && typeof obj.title != 'object' && typeof obj.content != 'object') { if(typeof obj.title == 'function') {document.getElementById('doc-title').innerHTML = obj.title(path.slice(i));} else if(obj.title) {document.getElementById('doc-title').innerHTML = obj.title;} else {document.getElementById('doc-title').innerHTML = path[path.length - 2];} if(typeof obj.content == 'function') {document.getElementById('doc-content').innerHTML = obj.content(path.slice(i));} else {document.getElementById('doc-content').innerHTML = obj.content;} document.getElementById('doc-path').innerHTML = pathtext; var as = document.getElementById('documentation').getElementsByTagName('a'); for(var j = 0; j < as.length; j++) { if(as[j].href && as[j].pathname.toLowerCase.indexOf('/doc/')) {as[j].onclick = function(event) {event.preventDefault; B3.view(this.pathname.substring(this.pathname.indexOf('/doc/') + 1));};} }					return; }			}		}		//path doesn't lead to a page if(obj._title) {document.getElementById('doc-title').innerHTML = obj._title;} else {document.getElementById('doc-title').innerHTML = path[path.length - 2];} var content = ' Contents '; if(obj._content) {content = obj._content + content;} for(var i in obj) { if(!i.startsWith('_')) {content += '' + i + '</a></li>';} }		document.getElementById('doc-content').innerHTML = content + '</ul>'; document.getElementById('doc-path').innerHTML = pathtext; var as = document.getElementById('documentation').getElementsByTagName('a'); for(var j = 0; j < as.length; j++) { if(as[j].href && as[j].pathname.toLowerCase.indexOf('/doc/')) {as[j].onclick = function(event) {event.preventDefault; B3.view(this.pathname.substring(this.pathname.indexOf('/doc/') + 1));};} else if(!as[j].target) {as[j].target = '_blank';} }	}	else {throw new Error('B3: Invalid doc page reference');} } B3.documentation = { _title: 'B3 Documentation', _content: 'B3, short for Browser-Based Bot, is a Javascript framework for the MediaWiki API. B3 is designed to be indefinitely extensible, so that anyone can write a module that anyone can use, simply by importing it along with the rest of B3. This documentation contains any notes that came with any registered extensions, as well as information on registered libraries, the API, and how to write your own extensions.', General: { Extensibility: { content: 'B3 is designed to allow for an infinite number of independent extensions that can be imported from anywhere on the internet. In fact, all of the functionality of B3 comes from extensions and the libraries that power them.\n\n Basics \n\n Documentation ' }	},	Data: { Extensions: { content: function(params) { var html = ''; for(var i in B3.extensions) { html += ' ' + i + ' <ul>'; for(var j in B3.extensions[i]) { switch(typeof B3.extensions[i][j]) { case 'string': var val = B3.extensions[i][j]; break; case 'function': var val = B3.extensions[i][j]; break; case 'object': var val = '<ul>'; for(var k in B3.extensions[i][j]) { val += '<li>' + B3.extensions[i][j][k] + '</li>'; }								val += '</ul>'; break; }						html += '<li>' + j + ': ' + val + '</li>'; }					html += '</ul>'; }				return html; }		},		Libraries: { }	},	API: { content: function(params) { B3.documentation.API._api = new APIQuery({logger: function {}}); B3.documentation.API._api.newQuery('GET', {}, function(result) {				if(document.getElementById('doc-api-result')) {					document.getElementById('doc-api-result').innerHTML = xhr.responseText;				}			}); return 'The form below will allow you to query the API manually. This can be helpful for testing outputs or debugging server interactions. Queries are made through APIQuery</a>, so tokens will be fetched for you automatically. Be aware that because each result is fetched directly from the API, the form is capable of modifying the wiki - for example, if you want to see what an edit request returns, the form will edit an actual page. Make sure that you don\'t accidentally disrupt your wiki through the form. Queries are submitted when the form loses focus.\n<form onblur="B3.documentation.API._api.newQuery(document.getElementById(\'doc-api-method\').value, document.getElementById(\'doc-api-params\').value, B3.documentation.API._api[0].onsuccess); B3.documentation.API._api.clear(0); if(!B3.documentation.API._api[0].params.format.endsWith(\'fm\')) {B3.documentation.API._api[0].params.format += \'fm\';} B3.documentation.API._api.send(0);"><label for="doc-api-method">Method: <input type="text" id="doc-api-method" value="GET"> <label for="doc-api-params">Params: <input type="text" id="doc-api-params"> \n Result '; }	} } /* Extended functions */ String.prototype.startsWith = function(text) { if(this.indexOf(text) != -1 && this.indexOf(text) == 0) {return true;} else {return false;} } String.prototype.endsWith = function(text) { if(this.indexOf(text) != -1 && this.indexOf(text) == this.length - text.length) {return true;} return false; } String.prototype.removeTrailing = function(chars) { var str = this; for(var i = 0; i < chars.length; i++) { while(str.charAt(0) == chars.charAt(i)) {str = str.substring(1, str.length);} }	for(var i = 0; i < chars.length; i++) { while(str.charAt(str.length - 1) == chars.charAt(i)) {str = str.substring(0, str.length - 1);} }	return str; } String.prototype.capitalize = function { return this.charAt(0).toUpperCase + this.substring(1); } function createCookie(name, value, days) { if(days) { var date = new Date; date.setTime(date.getTime + (days * 24 * 60 * 60 * 1000)); var expires = '; expires=' + date.toGMTString; }	else {var expires = '';} document.cookie = name + '=' + value + expires + '; path=/'; } function readCookie(name) { var nameEQ = name + '='; var ca = document.cookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while(c.charAt(0) == ' ') {c = c.substring(1);} if(c.indexOf(nameEQ) == 0) {return c.substring(nameEQ.length);} }	return null; } function eraseCookie(name) { createCookie(name, '', -1); } addOnloadHook(B3.setup);