ttyler333 / Google Custom Logo

// ==UserScript==
// @name        Google Custom Logo
// @namespace   googlelogos
// @description Change the google logo to whatever you wish.
// @version     1.5
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
// @grant       GM_getValue
// @grant       GM_setValue
// @include     https://*.google.com/*
// @run-at       document-end
// ==/UserScript==

//http://i.imgur.com/rCjrBAR.png
//window.addEventListener('DOMContentLoaded', applyRules)
if (!this.GM_getValue) {
    this.GM_getValue=function (key,def) {
        return localStorage[key] || def;
    };
    this.GM_setValue=function (key,value) {
        return localStorage[key]=value;
    };
    this.GM_deleteValue=function (key) {
        return delete localStorage[key];
    };
}
//$("div#logocont h1 a img").attr("src","http://i.imgur.com/rCjrBAR.png").removeAttr("height").attr("width","120").css("top","0px");

  function setImage(){
    var imgUrl = prompt("Enter the Image Url", "");
      if (imgUrl !== null) {
          GM_setValue("gImgUrl", imgUrl);
         changeImg(imgUrl);
      }
  }


 function changeImg(inputimg, height=0, margL, margT){
     if(height===0){
          jQuery("div#hplogo a img").attr("src", inputimg).removeAttr("width");
     }else{
    jQuery("div#hplogo a img").attr("src", inputimg).removeAttr("width").attr("height", height).css("margin-left", margL+"px").css("margin-top", margT+"px");
    }
 }

function Gtoggle(){
    
}

function Gsize(){
    var height = prompt("Please enter the image height. We will automatically size the width.", "");
      if (height !== null) {
          GM_setValue("gImgHeight", parseInt(height));
         changeImg(GM_getValue("gImgUrl"), GM_getValue("gImgHeight"), GM_getValue("gImgMarginLeft"), GM_getValue("gImgMarginLeft"));
      }
  }

function Gsettings(){
    $('#Gnotify').html("Press End to exit adjusting the image.");
    var paddingtop = parseInt(jQuery("div#hplogo a img").css("margin-top"));
    var paddingleft = parseInt(jQuery("div#hplogo a img").css("margin-left"));
   document.onkeydown = function(e) {
    switch (e.keyCode) {
        case 37:
            //alert('left');
            paddingleft--;
            jQuery("div#hplogo a img").css("margin-left", paddingleft);
            break;
        case 38:
            //up
            paddingtop--;
            jQuery("div#hplogo a img").css("margin-top", paddingtop);
        
            break;
        case 39:
            //alert('right');
            paddingleft++;
            jQuery("div#hplogo a img").css("margin-left", paddingleft);
            break;
        case 40:
            //alert('down'); 
            paddingtop++;
            jQuery("div#hplogo a img").css("margin-top", paddingtop);
            break;
        case 35:
            //exit keycodes.
            $('#Gnotify').html('');
         
          GM_setValue("gImgMarginTop", paddingtop); 
          GM_setValue("gImgMarginLeft", paddingleft); 
            document.onkeydown = null;
    }
};
}
function createmenus(){
    var scriptCode = new Array();   // this is where we are going to build our new script
    // here's the build of the new script, one line at a time
    scriptCode.push('function setG(elemid){');
    scriptCode.push(' var a = document.getElementById(elemid);  ');
    scriptCode.push('var evt = document.createEvent(\'HTMLEvents\');');
    scriptCode.push('evt.initEvent(\'click\', false, true);');
    scriptCode.push(' a.dispatchEvent(evt); }');
    scriptCode.push('');
    var script = document.createElement('script');    // create the script element
    script.innerHTML = scriptCode.join('\n');         // add the script code to it
    scriptCode.length = 0;                            // recover the memory we used to build the script
    
    
    document.getElementsByTagName('head')[0].appendChild(script); 
    //(document.body || document.head || document.documentElement).appendChild(script);
   $("<div id='setGImg' style='visible:false'></div>").insertAfter("body");
   $("<div id='Gsettings' style='visible:false'></div>").insertAfter("body");
   //$("<div id='Gtoggle' style='visible:false'></div>").insertAfter("body");
   $("<div id='Gsize' style='visible:false'></div>").insertAfter("body");
   $("<div id='Gnotify' style='width:250px;margin:0 auto;'></div>").insertAfter("span#body");
   $("<menu type=\"context\" id=\"mymenu\">"
      + "<menuitem label=\"Toggle G Image\" onclick=\"javascript:setG('Gtoggle');\"></menuitem>"
      + "<menuitem label=\"Edit Google Image\" onclick=\"javascript:setG('setGImg');\"></menuitem>"
      +	"<menuitem label=\"Adjust Image Position\" onclick=\"javascript:setG('Gsettings');\"></menuitem>"
      +	"<menuitem label=\"Adjust Image Size\" onclick=\"javascript:setG('Gsize');\"></menuitem>"
      + "</menu>").insertAfter("body");
   $("body").attr("contextmenu", "mymenu");
    
}






window.addEventListener('DOMContentLoaded', function(e) { //DOMContentLoaded
    //Load the defaults
    var imgTagLoc = "div#hplogo a img";
    GM_setValue("gImgUrlDefault", $(imgTagLoc).attr("src")); //Get the default before manipulating

if(GM_getValue("gImgUrl") === undefined){
  GM_setValue("gImgUrl", GM_getValue("gImgUrlDefault")); //this is the stored variable where custom images will be saved.
}
    
if(GM_getValue("gImgMarginTop") === undefined){
  GM_setValue("gImgMarginTop", 0); //this is the stored variable where custom images will be saved.
}
if(GM_getValue("gImgMarginLeft") === undefined){
  GM_setValue("gImgMarginLeft", 0); //this is the stored variable where custom images will be saved.
}
if(GM_getValue("gImgHeight") === undefined){
    GM_setValue("gImgHeight", $(imgTagLoc).attr("height")); //this is the stored variable where custom images will be saved.
}
//if(GM_getValue("gImgWidth") == undefined){
//    GM_setValue("gImgWidth", $(imgTagLoc.attr("width"))); //this is the stored variable where custom images will be saved.
//}
    
    changeImg(GM_getValue("gImgUrl"), GM_getValue("gImgHeight"), GM_getValue("gImgMarginLeft"), GM_getValue("gImgMarginTop"));
    
    //Handle the magic
    createmenus();
    
    var b=document.getElementById("setGImg");
    b.addEventListener("click", setImage, false); 
    
    var c=document.getElementById("Gsettings");
    c.addEventListener("click", Gsettings, false); 
    
    var d=document.getElementById("Gtoggle");
    d.addEventListener("click", Gtoggle, false); 
    
    var f=document.getElementById("Gsize");
    f.addEventListener("click", Gsize, false); 
}, false);