Difference between revisions of "MediaWiki:Common.js"

From TAMI
Jump to navigationJump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
"use strict"
 +
 +
/*
 +
* This file should be used as the contents of the page:
 +
*
 +
* http://telavivmakers.org/index.php/MediaWiki:Common.js
 +
*
 +
*/
 +
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
var tami_is_using_common_js = 1;
 
var tami_is_using_common_js = 1;
Line 6: Line 15:
 
/**************************************************************/
 
/**************************************************************/
 
// Set to false or delete to disable analytics/tracking:
 
// Set to false or delete to disable analytics/tracking:
TowTruckConfig_enableAnalytics = true;
+
var TowTruckConfig_enableAnalytics = true;
  
var towtruck_script = document.createElement("script");
+
function towtruck_init() {
towtruck_script.setAttribute("type", "text/javascript");
+
    var towtruck_script = document.createElement("script");
towtruck_script.setAttribute("src", "https://towtruck.mozillalabs.com/towtruck.js");
+
    towtruck_script.setAttribute("type", "text/javascript");
document.getElementsByTagName("head")[0].appendChild(towtruck_script);
+
    towtruck_script.setAttribute("src", "https://towtruck.mozillalabs.com/towtruck.js");
 +
    document.getElementsByTagName("head")[0].appendChild(towtruck_script);
  
var towtruck_button = document.createElement("button");
+
    var towtruck_button = document.createElement("button");
towtruck_button.setAttribute("onclick", "TowTruck(this); return false;")
+
    towtruck_button.setAttribute("onclick", "TowTruck(this); return false;")
towtruck_button.innerHTML = "Start Collaborating";
+
    towtruck_button.innerHTML = "Start Collaborating";
towtruck_button.style.fontSize = "0.75em";
+
    towtruck_button.style.fontSize = "0.75em";
document.getElementById("p-navigation").getElementsByTagName("div")[0].appendChild(towtruck_button);
+
    document.getElementById("p-navigation").getElementsByTagName("div")[0].appendChild(towtruck_button);
 +
}
 +
towtruck_init();
  
 
/*************************************************************/
 
/*************************************************************/
Line 23: Line 35:
 
/*************************************************************/
 
/*************************************************************/
  
function title_changer()
+
function get(url)
 +
{
 +
    return new Promise(function(resolve, reject) {
 +
        var request = new XMLHttpRequest();
 +
        request.onload = function () {
 +
            if (request.status == 200) {
 +
                resolve(request.response);
 +
            } else {
 +
                reject(Error(request.statusText));
 +
            }
 +
        }
 +
        request.onerror = function () {
 +
            reject(Error('network error'));
 +
        }
 +
        request.open('GET', url);
 +
        request.send();
 +
    });
 +
}
 +
 
 +
function get_titles()
 +
{
 +
    return new Promise(function (resolve, reject) {
 +
        get(document.location.origin + '/index.php/TAMI_Names?action=raw').then(
 +
            function (response) {
 +
                var titles = response.split("\n").filter(function(txt) {
 +
                    return txt.trim().length > 0;
 +
                });
 +
                console.log('got ' + titles.length + ' titles');
 +
                resolve(titles);
 +
            },
 +
            function (error) {
 +
                console.error('Failed!', error);
 +
                reject(error);
 +
            }
 +
        );
 +
    });
 +
}
 +
 
 +
function title_changer(titles)
 
{
 
{
 
     var title_element = document.getElementById("Welcome_to_Tel_Aviv_Makers_Insurgence_hackerspace.21");
 
     var title_element = document.getElementById("Welcome_to_Tel_Aviv_Makers_Insurgence_hackerspace.21");
Line 30: Line 80:
 
         return;
 
         return;
 
     }
 
     }
     var titles = [
+
     function change_title() {
        "Insurgents",
+
         var i = Math.floor((Math.random() * titles.length));
        "Insurmountable", "Interconnected", "Intergalactic",
 
        "Incredible",
 
        "Improvement",
 
        "Israel", "Ideal", "Indomitable", "I", "Igneous", "Incontinent",
 
        "Ifsoever", "Imminent", "Immanent", "Immunodeficient", "Inconvertible",
 
        "Incontrovertible", "Inception", "Infinite", "Impish", "Indubitable",
 
        "Ingenious", "Incontinent", "Inspiring", "Inkfilled", "Invaded", "Ionized",
 
        "Irresistable", "Isochloric", "Isobath", "Isomagnetic",
 
        "Industrious", "Inconvincible", "Inexpert", "Intrest-group"
 
        ];
 
    var n = titles.length;
 
    var i = 0;
 
    var change_title = function() {
 
         i = Math.floor((Math.random() * n));
 
 
         title_element.innerHTML = "Welcome to Tel Aviv Makers " + titles[i] + " hackerspace!";
 
         title_element.innerHTML = "Welcome to Tel Aviv Makers " + titles[i] + " hackerspace!";
 
     }
 
     }
Line 51: Line 87:
 
     setInterval(change_title, 60000);
 
     setInterval(change_title, 60000);
 
}
 
}
title_changer();
+
get_titles().then(title_changer);

Latest revision as of 15:46, 3 January 2015

"use strict"

/*
 * This file should be used as the contents of the page:
 *
 * http://telavivmakers.org/index.php/MediaWiki:Common.js
 *
 */

/* Any JavaScript here will be loaded for all users on every page load. */
var tami_is_using_common_js = 1;

/**************************************************************/
/* Add Tow Truck botton (Start Collaborating) on the menu     */
/**************************************************************/
// Set to false or delete to disable analytics/tracking:
var TowTruckConfig_enableAnalytics = true;

function towtruck_init() {
    var towtruck_script = document.createElement("script");
    towtruck_script.setAttribute("type", "text/javascript");
    towtruck_script.setAttribute("src", "https://towtruck.mozillalabs.com/towtruck.js");
    document.getElementsByTagName("head")[0].appendChild(towtruck_script);

    var towtruck_button = document.createElement("button");
    towtruck_button.setAttribute("onclick", "TowTruck(this); return false;")
    towtruck_button.innerHTML = "Start Collaborating";
    towtruck_button.style.fontSize = "0.75em";
    document.getElementById("p-navigation").getElementsByTagName("div")[0].appendChild(towtruck_button);
}
towtruck_init();

/*************************************************************/
/* Set TAMI title to random (*) word starting with I         */
/*************************************************************/

function get(url)
{
    return new Promise(function(resolve, reject) {
        var request = new XMLHttpRequest();
        request.onload = function () {
            if (request.status == 200) {
                resolve(request.response);
            } else {
                reject(Error(request.statusText));
            }
        }
        request.onerror = function () {
            reject(Error('network error'));
        }
        request.open('GET', url);
        request.send();
    });
}

function get_titles()
{
    return new Promise(function (resolve, reject) {
        get(document.location.origin + '/index.php/TAMI_Names?action=raw').then(
            function (response) {
                var titles = response.split("\n").filter(function(txt) {
                    return txt.trim().length > 0;
                });
                console.log('got ' + titles.length + ' titles');
                resolve(titles);
            },
            function (error) {
                console.error('Failed!', error);
                reject(error);
            }
        );
    });
}

function title_changer(titles)
{
    var title_element = document.getElementById("Welcome_to_Tel_Aviv_Makers_Insurgence_hackerspace.21");
    if (title_element === undefined) {
        //console.log("someone broke the fun script (or just not the main page)");
        return;
    }
    function change_title() {
        var i = Math.floor((Math.random() * titles.length));
        title_element.innerHTML = "Welcome to Tel Aviv Makers " + titles[i] + " hackerspace!";
    }
    change_title();
    setInterval(change_title, 60000);
}
get_titles().then(title_changer);