Wednesday, December 7, 2011

Tweak Google Menu (beta)


// ==UserScript==
// @name           Tweak Google Menu
// @version        0.53
// @author         Tomas Luis Alemany
// @namespace      
// @description    Allows you to tweak the new Google menu
// @include        https://*.google.com*
// @include        http://*.google.com*
// @include        http://mail.google.com*
// @include        https://mail.google.com*
// ==/UserScript==
//
//==========================================================================================
// Change Log:
// 2011-12-08: 0.53
//      - Simple GUI to update configuration keys.
// 2011-12-07: 0.52
// - Fix first run key definition.
// 2011-12-07: 0.51
// - Fix compatibility with Chrome.
// 2011-12-06:  0.52
// - Initial release.
//==========================================================================================

// LocalStorage API - Compatibility for Chrome
if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
   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];};}

//GM_deleteValue("firstrun");GM_deleteValue("gmlist");GM_deleteValue("mmlist");GM_deleteValue("smlist");//#debug# }

if (!GM_getValue("firstrun")) {GM_deleteValue("gmlist");GM_deleteValue("mmlist");GM_deleteValue("smlist");GM_setValue("firstrun","0");}//#debug# }

// Details of all the buttons of the menu
// To add a new button use the following syntaxis:
// var code = {label: 'Default Label', url: 'URL to open', imgurl: 'URL of the image', imgpos: 'Image position'}
var sweb = {label: 'Search Web', url: 'https://www.google.com', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-266px -124px'};
var gplus = {label: 'Google+', url: 'https://plus.google.com', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-111px 0px'};
var greader = {label: 'Reader', url: 'https://www.google.com/reader', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-143px -74px'};
var gmail = {label: 'Gmail', url: 'https://mail.google.com', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-185px -37px'};
var gcalendar = {label: 'Calendar', url: 'https://www.google.com/calendar', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-148px -37px'};
var gdocs = {label: 'Documents', url: 'https://www.google.com/docs', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-192px 0px'};
var gmusic = {label: 'Music', url: 'https://music.google.com', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '0px -244px'};
var picasa_alt = {label: 'Picasa', url: 'https://picasaweb.google.com', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-111px -267px'};
var picasa = {label: 'Picasa', url: 'https://picasaweb.google.com', imgurl: 'lh5.googleusercontent.com/s/v/83.07/img/logo/pwa-es.gif', imgpos: '4px 4px'};
var youtube = {label: 'YouTube', url: 'http://www.youtube.com/?tab=w1', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-185px -267px'};
var simages = {label: 'Search Images', url: 'https://www.google.com/images', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-37px -207px'};
var gmaps = {label: 'Maps', url: 'https://www.google.com/maps', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-148px -267px'};
var gnews = {label: 'News', url: 'https://www.google.com/news', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-111px -37px'};
var svideos = {label: 'Search Videos', url: 'https://video.google.com', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-67px -37px'};
var gbooks = {label: 'Books', url: 'https://books.google.com/bkshp?hl=en&tab=wp', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-259px -267px'};
var goffers = {label: 'Offers', url: 'https://www.google.com/offers', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-222px -267px'};
var market = {label: 'Market', url: 'https://market.android.com/?hl=en', imgurl: 'market.android.com/static/client/images/logo.png', imgpos: '0px 0px'};
var gfinance = {label: 'Finance', url: 'https://www.google.com/finance?tab=we', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-74px -207px'};
var gtranslate = {label: 'Translate', url: 'https://translate.google.com/?hl=en&tab=wT', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-37px 0px'};
var gphotos = {label: 'Photos', url: 'https://plus.google.com/u/0/photos?tab=wq', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-111px -267px'};
var gwallet = {label: 'Wallet', url: 'https://wallet.google.com/manage/', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-30px -37px'};
var shopping = {label: 'Shopping', url: 'https://www.google.com/prdhp?hl=en&tab=wf', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '-229px -124px'};
var blogger = {label: 'Blogger', url: 'https://www.blogger.com/?tab=wj', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '0px -207px'};
var gmobile = {label: 'Mobile', url: 'https://www.google.com/mobile/', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '0px 0px'};
var bset = {label: 'Settings', url: '', imgurl: 'www.google.com/reader/ui/345992534-settings.png', imgpos: '20px 10px'};
var bem = {label: 'Even More', url: 'http://www.google.com/intl/en/options/', imgurl: 'ssl.gstatic.com/gb/images/j_40368b96.png', imgpos: '10px -272px'};

var updateSettings = function (list) {
   switch(list) {
      case 'global':
         GM_setValue("gmlist", "greader,gmail,gcalendar,gdocs,gmusic,market,sweb,picasa,gphotos,simages,youtube,gmaps,gnews,svideos,gbooks,goffers,gfinance,gtranslate,shopping,blogger,gmobile,gwallet");
         break;
      case 'main':
         GM_setValue("mmlist", "sweb,simages,gmaps,youtube,gnews,gmail,gdocs");
         break;
      case 'secondary':
         GM_setValue("smlist", "gcalendar,gtranslate,gmobile,gbooks,gmusic,goffers,gwallet,shopping,blogger,greader,gfinance,gphotos,svideos,market,picasa");
         break;
      default:
         GM_setValue("mmlist", "sweb,simages,gmaps,youtube,gnews,gmail,gdocs");
         GM_setValue("smlist", "gcalendar,gtranslate,gmobile,gbooks,gmusic,goffers,gwallet,shopping,blogger,greader,gfinance,gphotos,svideos,market,picasa");}};

// Init keys if no exist
if (!GM_getValue("gmlist")) {updateSettings('global');}
if (!GM_getValue("mmlist")) {updateSettings('main');}
if (!GM_getValue("smlist")) {updateSettings('secondary');}
var ggmenu = GM_getValue("gmlist").split(","); // Full menu options
var mgmenu = GM_getValue("mmlist").split(","); // Main menu options
var sgmenu = GM_getValue("smlist").split(","); // Secn menu options

var openWindowSettings = function () {
   if (confirm ("Customize the menu options?")) {
      if (confirm ("Do you want to see the full list of options available?")) {alert(GM_getValue("gmlist"));}
      var mmset = prompt ("Menu1(black)",GM_getValue("mmlist"));
      if (mmset != '' && mmset != null) {
         var aset = mmset.split(",");
         var mmset = "";
         for (x in aset) {if (ggmenu.indexOf(aset[x]) >= 0) {mmset += aset[x] + ",";} else {alert('Error: ' + aset[x] + ' is not a valid option. Removed from the list.');}}
         if (mmset != '') {GM_setValue("mmlist", mmset.slice(0,mmset.length-1));} else {alert('Error');}
      }
      var smset = prompt ("Menu2(white)",GM_getValue("smlist"));
      if (smset != '' && smset != null) {
         var aset = smset.split(",");
         var smset = "";
         for (x in aset) {if (ggmenu.indexOf(aset[x]) >= 0) {smset += aset[x] + ",";} else {alert('Error: ' + aset[x] + ' is not a valid option. Removed from the list.');}}
         if (smset != '') {GM_setValue("smlist", smset.slice(0,smset.length-1));} else {alert('Error');}
      }
   } else {if (confirm ("Reset to default?")){updateSettings();}}};

var addMenuButton = function (bclass,gmbuttonid) {
   if (gmbuttonid){
      var blabel = eval (gmbuttonid + ".label");
      var burl = eval (gmbuttonid + ".url");
      var bimg = eval (gmbuttonid + ".imgurl");
      var bimgpos = eval (gmbuttonid + ".imgpos");}
   switch(bclass) {
      case 'gbzt':
         var buttonhtml = '<li class="gbt"><a class="gbzt" id="gb_999" href="' + burl + '" target="_blank"><span style="background-image: url(//' + bimg + '); ';
         buttonhtml += 'background-position: '+ bimgpos +'; background-repeat:no-repeat;" class="gbtb2"></span><span class="gbts">' + blabel + '</span></a></li>';
         break;
      case 'gbmt':
         var buttonhtml = '<span class="gbmtc"><a class="gbmt" id="gb_999" href="' + burl + '" target="_blank"><span style="background-image: url(//' + bimg + '); ';
         buttonhtml += 'background-position: '+ bimgpos +'; background-repeat:no-repeat;" class="gbtb2"></span><span class="gbts">' + blabel + '</span></a></span>';
         break;
      case 'evenmore':
           var buttonhtml = '<span class="gbmtc" id="emb"></span>';
         break;
      case 'smenustr':
         var buttonhtml = '<li id="gbtm" class="gbt"><a  id="gbztm" class="gbgt" aria-owns="gbd" aria-haspopup="true" href="http://www.google.com/intl/en/options/">';
         buttonhtml += '<span class="gbts gbtsa" id="gbztms">More</span></span></a>';
         buttonhtml += '<div id="gbd" class="gbm" aria-owner="gbztm" style="height: auto; width: auto; visibility: hidden;">';
         buttonhtml += '<div id="gbdi" class="gbmc"><div class="gbmasc" id="gbm1">';
         break;
      case 'smenuend':
         var buttonhtml = '</div></div></div></li>';
         break;
      default:
         var buttonhtml = '';}
   return buttonhtml;};

var addSettingsButton = function () {
   var sb = document.createElement('div');
   sb.setAttribute("id", "gbtem");
   sb.setAttribute("class", "gbmtc");
   var sbp = document.getElementById('gbdi');
   sbp.appendChild(sb);
   var sba = document.createElement('a');
   sba.setAttribute("class", "gbmt");
   sba.href = "";
   sba.addEventListener('click', function() {openWindowSettings();}, false);
   var sbahtml = '<span class="gbts" style="background-image: url(//' + bset.imgurl + '); background-position: ' + bset.imgpos + '; ';
   sbahtml += 'background-size: 24px 24px; background-repeat:no-repeat; opacity: .55;">Settings</span>';
   sba.innerHTML = sbahtml;
   var sbap = document.getElementById('gbtem');
   sbap.appendChild(sba);};

var addEvenMoreButton = function () {
   var emb = document.createElement('a');
   emb.setAttribute("id", "emba");
   emb.setAttribute("class", "gbmt");
   emb.setAttribute("href", bem.url);
   var embhtml = '<span class="gbtb2" style="background-image: url(//' + bem.imgurl + '); background-position: ' + bem.imgpos + '; background-repeat: no-repeat; opacity: .55;"></span>';
   embhtml += '<span class="gbts">' + bem.label + '</span>';
   emb.innerHTML = embhtml;
   var embp = document.getElementById('emb');   
   embp.appendChild(emb);};

var regExp = /<\/?[^>]+>/gi;
var replaceTags = function (xStr){xStr = xStr.replace(regExp,"");return xStr;};

// Get Google Plus username
var i = document.getElementById('gb_119');
gplus.label = replaceTags(i.innerHTML);

// Remove Google Menu
var x = document.getElementById('gbzc');
x.parentNode.removeChild(x);

// Compose and show the customized Google Menu
var u = document.createElement("ol");
u.setAttribute("id", "gbzc");
u.setAttribute("class", "gbtc");
var uHTML = '';
uHTML = addMenuButton('gbzt','gplus');
for (y in mgmenu) {uHTML += addMenuButton('gbzt',mgmenu[y]);}
uHTML += addMenuButton('smenustr');
for (z in sgmenu) {uHTML += addMenuButton('gbmt',sgmenu[z]);}
uHTML += addMenuButton('evenmore');
uHTML += addMenuButton('smenuend');
u.innerHTML = uHTML;
var y = document.getElementById('gbz');
y.appendChild(u); 
addEvenMoreButton();
addSettingsButton();

0 comments:

Post a Comment