oriolrius.cat

Des del 2000 compartiendo sobre…

Tag: menu

Add a New Dropdown Menu in OpenWRT LUCI

Reading time: 3 – 5 minutes

OpenWRT, the popular open-source Linux operating system designed for embedded devices, offers the LUCI interface for easy configuration and management. LUCI is essentially the web interface for OpenWRT, and while it’s already feature-rich, sometimes you may want to extend its functionalities based on your needs.

Recently, I had a requirement to enhance my OpenWRT LUCI interface. Specifically, I wanted to introduce a new menu named “Apps” in the LUCI dashboard. The objective was to have each entry within this new menu link to different applications installed on my device. And not just that! I wanted these application pages to pop open in new tabs for ease of access.

The Solution

The changes were made in a single file located at:

/usr/lib/lua/luci/controller/apps.lua

Within this file, I implemented the following code:

module("luci.controller.apps", package.seeall)

function index()
    local page

    page = entry({"admin", "apps"}, firstchild(), _("Apps"), 60)
    page.dependent = false

    entry({"admin", "apps", "portainer"}, call("serve_js_redirect", "https", "9443"), _("Portainer"), 10).leaf = true
    entry({"admin", "apps", "nodered"}, call("serve_js_redirect", "http", "1880"), _("NodeRED"), 20).leaf = true
    entry({"admin", "apps", "grafana"}, call("serve_js_redirect", "http", "3000"), _("Grafana"), 30).leaf = true
    entry({"admin", "apps", "prometheus"}, call("serve_js_redirect", "http", "9090"), _("Prometheus"), 40).leaf = true
end

function serve_js_redirect(protocol, port)
    local ip = luci.http.getenv("SERVER_ADDR")
    local url = protocol .. "://" .. ip .. ":" .. port
    luci.http.prepare_content("text/html; charset=utf-8")
    luci.http.write("<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Redirecting...</title></head><body>")
    luci.http.write("<script type='text/javascript'>")
    luci.http.write("window.onload = function() {")
    luci.http.write("  var newWindow = window.open('".. url .."', '_blank');")
    luci.http.write("  if (!newWindow || newWindow.closed || typeof newWindow.closed == 'undefined') {")
    luci.http.write("    document.getElementById('manualLink').style.display = 'block';")
    luci.http.write("    setTimeout(function() { window.location.href = document.referrer; }, 60000);")  -- Redirigir después de 60 segundos
    luci.http.write("  }")
    luci.http.write("}")
    luci.http.write("</script>")
    luci.http.write("<p id='manualLink' style='display: none;'>The window didn't open automatically. <a href='".. url .."' target='_blank'>Click here</a> to open manually.</p>")
    luci.http.write("</body></html>")
end

Key Points

  • The index function is responsible for defining the new menu and its entries.
  • The serve_js_redirect function creates a web page that uses JavaScript to automatically open the desired application in a new browser tab.
  • A failsafe mechanism is added in the form of a link. If, for any reason (like pop-up blockers), the new tab doesn’t open automatically, the user has the option to manually click on a link to open the application.
  • The script also includes a feature that will redirect the user back to the previous page after 60 seconds if the new tab doesn’t open.

This modification provides a seamless way to integrate external applications directly into the LUCI interface, making navigation and management even more convenient!


Editant menú de GNOME

Reading time: 2 – 3 minutes

El Benja em va preguntar el divendres, com edito el menú del GNOME i jo vaig pensar ostres fa temps que ho vaig fer i no recordo com. De fet, el paquet base de GNOME 2.12 porta un editor força simple dels menús. Es tracta del gmenu-simple-editor, bàsicament només pensat per mostrar o ocultar opcions del menú. Però jo concretament avui m’he preguntat com podia fer apareixer als meus menús algunes aplicacions que no són natives de GNOME de fet són dues aplicacions que funcionen amb el motor JRE de Java i que volia poder cridar sense haver de sortir a la CLI.

Bé doncs, als fòrums de gentoo he trobat aquest thread Gnome 2.12.1 menu editor fails to launch. La solució ha vingut en forma d’una aplicació que ara mateix no esta al portage de gentoo però que té un ebuild al bugzilla de gentoo, concretament estic parlant de alacarte (bug 111376). Es tracta d’un editor molt senzill d’usar i molt semblant d’aspecte a l’editor que porta GNOME de serie.

Resumeixo els passos a seguir per instal·lar-lo:

# mkdir -p /usr/local/portage/x11-misc/alacarte/files
# cd /usr/local/portage/x11-misc/alacarte/
# wget http://bugs.gentoo.org/attachment.cgi?id=72041
# PORTDIR_OVERLAY="/usr/local/portage"
# export PORTDIR_OVERLAY
# ebuild /usr/local/portage/x11-misc/alacarte/alacarte-0.8.ebuild digest
# echo dev-python/pyxdg ~x86 >> /etc/portage/package.keywords
# echo x11-misc/alacarte ~x86 >> /etc/portage/package.keywords
# emerge x11-misc/alacarte

Amb això ja tenim l’aplicació llesta, si voleu veure l’aspecte que té:

alacarte.png

Fa uns dies va sortir la versió 2.14 de GNOME tot i que encara no la tinc instal·lada i jo diria que ja porta un editor com deu mana de serie. El que no sé segur és si és el propi alacarte o un altre.