9

I have written a small GNOME Shell extension, that I want to distribute to some collegues.

For this I created a RPM. After the installation a restart of GNOME-Shell is needed to make the extension visible, so it can be enabled. Either by using <ALT-F2> followed by r when using X11 or log out and in when using Wayland.

Only after this restart the extension is visible in GNOME-Tweaks or can be activated using gnome-extensions enable ....

I was told that there might be a way to make the extension known to GNOME-Shell without restart. I searched around, but didn't find anything.

So: Can a GNOME-Shell extension be installed in a way that no restart is needed before it can be activated?

Environment is GNOME-Shell 3.34 & 3.36 on Fedora 31 & 32.

Ralf
  • 1,773
  • 8
  • 17

1 Answers1

2

This will enable the extension as if it was coming from ego. Replace global.userdatadir with global.datadir and PER_USER with SYSTEM if the extension is in /usr/share/gnome-shell/extensions/.

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;

const ExtensionUtils = imports.misc.extensionUtils;
const Main = imports.ui.main;

// Here, the directory must be in ~/.local/share/gnome-shell/extensions.
function installFromLocal(uuid) {
    let dir = Gio.File.new_for_path(GLib.build_filenamev([global.userdatadir, 'extensions', uuid]));
    let manager = Main.extensionManager;
    
    try {
        let extension = manager.createExtensionObject(uuid, dir, ExtensionUtils.ExtensionType.PER_USER);
        manager.loadExtension(extension);
        if (!manager.enableExtension(uuid))
            throw new Error('Cannot add %s to enabled extensions gsettings key'.format(uuid));
    } catch (e) {
        let extension = Main.extensionManager.lookup(uuid);
        if (extension)
            Main.extensionManager.unloadExtension(extension);
        throw new Error('Error while installing %s: %s (%s)'.format(uuid, 'LoadExtensionError', e));
    }
}
abakkk
  • 21
  • 3
  • Looks interesting, but how would I execute this during my RPM install? – Ralf Oct 11 '20 at 11:12
  • I think it is possible to run the script through the GNOME Shell DBus service. But I don't know how to call DBus during RPM install. – abakkk Oct 13 '20 at 08:08
  • This worked for me after running it inside `looking glass` debugging util. It doesn't work if I run it with the `gjs` command. It will probably work with sending the JS code through `gdbus call` – Gareve Dec 16 '21 at 01:32