// ==UserScript==
// @name         Google Logo Fonts
// @namespace    https://github.com/AndersDJohnson/
// @version      1.0.0
// @downloadURL  https://github.com/AndersDJohnson/google-logo-fonts.user.js/raw/master/src/js/google-logo-fonts.user.js
// @description  Switch Google's logo to random fonts from Google Fonts API.
// @author       Anders D. Johnson
// @copyright    2015+, Anders D. Johnson
// @match        https://www.google.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require      https://raw.githubusercontent.com/typekit/webfontloader/master/webfontloader.js
// @grant        GM_xmlhttpRequest 
// ==/UserScript==

var randomIndex = function (array) {
    return Math.floor(array.length * Math.random());
};

var random = function(array) {
    return array[randomIndex(array)];
};

/**
 * http://worknotes.scripting.com/february2012/22612ByDw/listOfGoogleFonts
 * http://static.scripting.com/google/webFontNames.txt
 */
var getFonts = function () {
    return $.get('https://s3.amazonaws.com/static.scripting.com/google/webFontNames.txt')
    .then(function(data) {
        return data.split(/[\n\r]/);
    })
};

var loadFont = function (name) {
    var $link = $('<link>', {
        href: 'https://fonts.googleapis.com/css?family=' + name,
        rel: 'stylesheet',
        type: 'text/css'
    });

    $(document.body).append($link);
};

$(function () {
    var $logo = $('#hplogo');
    $logo.hide();

    var $par = $logo.parent();

    var $new = $('<div>');
    var colors = {
        blue: '#4285F4',
        red: '#EA4335',
        yellow: '#FBBC05',
        green: '#34A853'
    };
    var letterColors = ['blue','red','yellow','blue','green','red'];
    $.each('Google'.split(''), function (k, v) {
        var $s = $('<span>');
        $s.text(v);
        $s.css({color: colors[letterColors[k]]});
        $new.append($s);
    });
    $new.css({
        fontSize: 80,
        position: 'absolute',
        width: '100%',
        textAlign: 'center',
        bottom: 36
    });

    var activate = function () {
        $logo.replaceWith($new);
        $par.css({
            position: 'relative' 
        });
    };

    getFonts()
    .then(function (fonts) {
        var font = random(fonts);
        //console.log('font', font);

        WebFont.load({
            google: {
                families: [font]
            },
            active: function() {
                $new.css({fontFamily: font});
                activate();
            }
        });
    });

});

@@ -117,17 +117,17 @@
       1.
-0
+1
 .0%0A// @d
@@ -1167,24 +1167,79 @@
 %0A    %7D)%0A%7D;%0A%0A
+$(function () %7B%0A    var $body = $(document.body);%0A%0A    
 var loadFont
@@ -1259,24 +1259,28 @@
 name) %7B%0A    
+   
+ 
 var $link = 
@@ -1293,16 +1293,20 @@
 nk%3E', %7B%0A
+    
         
@@ -1370,16 +1370,20 @@
         
+ 
+   
 rel: 'st
@@ -1393,16 +1393,20 @@
 sheet',%0A
+    
         
@@ -1422,16 +1422,20 @@
 xt/css'%0A
+    
     %7D);%0A
@@ -1439,32 +1439,25 @@
 );%0A%0A    
+  
-$(document.
+  $
 body
-)
 .append(
@@ -1464,35 +1464,23 @@
 $link);%0A
-%7D;%0A%0A$(function () %7B
+    %7D;%0A
 %0A    var
@@ -1519,16 +1519,109 @@
 .hide();
+%0A    %0A    $body.append('%3Cstyle%3E.wf-inactive #hplogo %7B display: block !important; %7D%3C/style%3E');
 %0A%0A    va
@@ -2249,19 +2249,13 @@
 ogo.
-replaceWith
+after
 ($ne
@@ -2334,124 +2334,50 @@
  %7D;%0A
-%0A
     
-getFonts()%0A    .then(function (fonts) %7B%0A        var font = random(fonts);%0A        //console.log('font', 
+%0A    var applyFont = function (
 font)
-;%0A
+ %7B
 %0A   
@@ -2604,16 +2604,366 @@
 );%0A    %7D
-)
 ;
+%0A%0A    var match = window.location.search.match(/%5B%5C?&%5D?font=(%5B%5E&%5D*)/);%0A    var qfont = match ? match%5B1%5D : null;%0A%0A    if (qfont) %7B%0A        qfont = decodeURIComponent(qfont);%0A        applyFont(qfont);%0A    %7D%0A    else %7B%0A        getFonts()%0A        .then(function (fonts) %7B%0A            var font = random(fonts);%0A            applyFont(font);%0A        %7D);%0A    %7D
 %0A%0A%7D);%0A

Support query parameter.