RegisterMenu

enter menu

Tính đến 27-09-2015. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name       RegisterMenu
// @namespace  http://lifia.unlp.edu.ar
// @version    0.2
// @description  enter menu
// @match      https://*/*
// @match      http://*/*
// @require    http://code.jquery.com/jquery-2.1.4.min.js
// ==/UserScript==

/*    Elimina todos los .js para utilizar la version del @require y no se genere conflicto      */

if (window.jQuery){
  $('head script[src*="js"]').remove();
    
}

GM_registerMenuCommand('run', funcionSeba);
GM_registerMenuCommand('import JSON', importJson);

function funcionSeba(){
  var obj = getLocal();
  var objectParent = constructObject(obj);
  runPage(objectParent);
}

function importJson() {
    var importData = prompt("Importar la configuracion. Ingrese el JSON correspondiente");
    // La longitud debe ser igual o mayor a 218, como para asegurar la estructura inicial del Json.
    if(importData.length >= 218){
      dataJson = JSON.parse(importData);
      saveLocal();
      alert("Se ha importado exitosamente!");
    } else {
      alert("Formato JSON ingresado no es el correspondiente. Por favor reintentelo nuevamente")
    }
}

function saveLocal(){
  if (typeof(Storage) !== "undefined") {
    localStorage.setItem("obj", JSON.stringify(dataJson));
  } else {
    alert("Sorry, your browser does not support Web Storage...");
  }
}

function getLocal(){
  if (typeof(Storage) !== "undefined") {
    return JSON.parse(localStorage.getItem("obj"));
  } else {
    alert("Sorry, your browser does not support Web Storage...");
  }
}
  
function getElements(xpath){
  // Recive algo como obj[0].headerLeft
  var node = document.evaluate(
          xpath,
          document,
          null,
          XPathResult.FIRST_ORDERED_NODE_TYPE,
          null ).singleNodeValue;
  return node;
}

function concatElement(element){
  var stringElements = "";
  var getElement;
  $.each( element, function( key, value ) {
    if (value != "none"){
      getElement = getElements(value);
      if(getElement !== null){
        stringElements += "<div>"+getElement.innerHTML+"</div>";
      }
      else
        stringElements = null;
    }
    else
      stringElements = "none";
  });
  return stringElements;
}

function constructObject(obj){
  var object = {};
  var error = false;
  $.each( obj, function( key, value ) {
    if (concatElement(value) === null){
      error = true
      messagge = "No se pudo cargar un elemento en "+key;
      return true;
    }
    if (concatElement(value) == "none"){
      object[key] = "";
    }
    else
      object[key] = concatElement(value);
  });  
  if (error === true){
    alert(messagge);
    return null;
  }
  else
    return object;
}

//var objectParent = constructObject(obj);

function importElement(source, destination){
  $(destination).append(source);
}

function insertZoneMenu(element){
  dwrap = document.createElement("div");
  dwrap.innerHTML = element;
  var links = dwrap.getElementsByTagName("a");  
  $("#menu").append("<nav class='navbar navbar-default' role='navigation'> <div class='navbar-header'> <button type='button' class='navbar-toggle' data-toggle='collapse' data-target='#bs-example-navbar-collapse-1'> <span class='sr-only'>Toggle navigation</span><span class='icon-bar'></span><span class='icon-bar'></span><span class='icon-bar'></span></button> </div>  <div class='collapse navbar-collapse' id='bs-example-navbar-collapse-1'> <ul id='menu-nav' class='nav navbar-nav'>  </ul> </div>  </nav> "); 
  $.each(links, function(i, e){
      alert();
    $("#menu-nav").append("<li><a href='"+e+"'>"+$(e).html()+"</a></li>");
  });  
}

function runPage(objectParent){
  $("head").append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
  $("head").append("<script src='https://code.jquery.com/jquery-2.1.4.min.js'></script>");
  $("head").append("<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js'></script>");
  //$("head").append("<link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css'>");
  $("head").append("<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'>");
  $("head").append("<style>*{min-width: 0px !important;}</style>");
  $("head").append("<style>*{max-width: 100% !important;}</style>");
  //$("ins").remove();

  if (objectParent !== null){
    //Insert Bootstrap structure
    $("body").html("");
    $("body").append("<div class='container-fluid'> <div class='row'> <div id='header-left' class='col-md-4'> </div> <div id='header-center' class='col-md-4'> </div> <div id='header-right' class='col-md-4'> </div> </div> <div class='row'> <div id='menu' class='col-md-12'> </div> </div> <div class='row'> <div id='main-left' class='col-md-4'> </div> <div id='main-center' class='col-md-4'> </div> <div id='main-right' class='col-md-4'> </div> </div> <div class='row'> <div id='footer-left' class='col-md-4'> </div> <div id='footer-center' class='col-md-4'> </div> <div id='footer-right' class='col-md-4'> </div> </div> </div>");
  
    importElement(objectParent.headerLeft,"#header-left");
    importElement(objectParent.headerCenter,"#header-center");
    importElement(objectParent.headerRight,"#header-right");
    insertZoneMenu(objectParent.menu);
    //importElement(objectParent.menu,"#menu");
    importElement(objectParent.mainLeft,"#main-left");
    importElement(objectParent.mainCenter,"#main-center");
    importElement(objectParent.mainRight,"#main-right");
    importElement(objectParent.footerLeft,"#footer-left");  
    importElement(objectParent.footerCenter,"#footer-center"); 
    importElement(objectParent.footerRight,"#footer-right"); 
  }
}