The officially official Devuan Forum!

You are not logged in.

#1 2025-08-25 00:15:29

greenjeans
Member
Registered: 2017-04-07
Posts: 1,113  
Website

A simple browser in 100 lines of code.

This is not intended to be a serious candidate for a browser, this is just a simple exercise in using a tiny amount of code (and some not-so-tiny libraries) to create a working browser. In the same spirit as many others who have done similar in the last 16 years that i've been using Linux, there's lots of examples from back in the day, this is a modern one using current daedalus packages. A determined user could start with this skeleton and do lots of things. wink

The buttons work, url bar works (it is NOT a search bar, you will need to add a working url thusly "dev1galaxy.org", it will add the https etc.).

Not safe for sketchy sites obviously, but a fun toy just to see what works. All in 100 lines of code (around ~75 if you take out comments and spaces).

Depends are listed in the code, obviously there's other things you need like gtk and python3, but any vanilla Devuan should have 'em.

#!/usr/bin/env python3

# Mini-browser experimental. Don't use on unsecure sites!
# Dependencies:
# libjavascriptcoregtk-4.1-0
# libwebkit2gtk-4.1-0
# gir1.2-javascriptcoregtk-4.1 (2.48.0-1~deb12u1)
# gir1.2-soup-3.0 (3.2.2-2)
# gir1.2-webkit2-4.1 (2.48.0-1~deb12u1)

import gi
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1")
from gi.repository import Gtk, WebKit2

class MiniBrowser:
    def __init__(self, homepage):
        # Store homepage
        self.homepage = homepage

        # Set up main window
        self.window = Gtk.Window()
        self.window.set_title("Dev1 Browser")
        self.window.set_icon_name("web-browser")
        self.window.set_default_size(800, 600)
        self.window.connect("destroy", Gtk.main_quit)

        # Create a WebView
        self.webview = WebKit2.WebView()
        self.webview.load_uri(self.homepage)

        # Handle link clicks
        self.webview.connect("decide-policy", self.on_policy)

        # Create URL entry
        self.url_entry = Gtk.Entry()
        self.url_entry.set_placeholder_text("Enter URL (e.g., https://dev1galaxy.org)")
        self.url_entry.set_text(self.homepage)
        self.url_entry.connect("activate", self.on_url_entry_activate)
        self.webview.connect("load-changed", self.on_load_changed)

        # Create navigation buttons
        self.back_button = Gtk.Button(label="Back")
        self.forward_button = Gtk.Button(label="Forward")
        self.home_button = Gtk.Button(label="Home")

        # Connect buttons to functions
        self.back_button.connect("clicked", self.go_back)
        self.forward_button.connect("clicked", self.go_forward)
        self.home_button.connect("clicked", self.go_home)

        # Create a horizontal box for the nav bar and URL entry
        nav_bar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5)
        nav_bar.set_margin_start(10)  # 10px left margin
        nav_bar.set_margin_top(5) # 5px top margin
        nav_bar.pack_start(self.back_button, False, False, 0)
        nav_bar.pack_start(self.forward_button, False, False, 0)
        nav_bar.pack_start(self.home_button, False, False, 0)
        nav_bar.pack_start(self.url_entry, True, True, 5)  # URL entry expands to fill space

        # Create a vertical box for nav bar and webview
        main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
        main_box.pack_start(nav_bar, False, False, 0)
        main_box.pack_start(self.webview, True, True, 0)

        # Add main box to window and show
        self.window.add(main_box)
        self.window.show_all()

    def on_policy(self, webview, decision, decision_type):
        # Allow navigation (link clicks)
        return False  # False means "proceed", weird right?

    def on_url_entry_activate(self, entry):
        url = entry.get_text().strip()
        if not url.startswith(('http://', 'https://')):
            url = 'https://' + url
        self.webview.load_uri(url)

    def on_load_changed(self, webview, load_event):
        if load_event == WebKit2.LoadEvent.COMMITTED:
            current_url = self.webview.get_uri()
            self.url_entry.set_text(current_url)

    def go_back(self, button):
        if self.webview.can_go_back():
            self.webview.go_back()

    def go_forward(self, button):
        if self.webview.can_go_forward():
            self.webview.go_forward()

    def go_home(self, button):
        self.webview.load_uri(self.homepage)
        self.url_entry.set_text(self.homepage)

if __name__ == "__main__":
    homepage = "https://dev1galaxy.org/"
    browser = MiniBrowser(homepage)
    Gtk.main()

Last edited by greenjeans (2025-08-26 19:52:34)


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded August 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Offline

Board footer