Skip to content

Instantly share code, notes, and snippets.

@luk0y
Last active September 12, 2024 07:07
Show Gist options
  • Save luk0y/812cdd572e138ed435a5a48f2eabb128 to your computer and use it in GitHub Desktop.
Save luk0y/812cdd572e138ed435a5a48f2eabb128 to your computer and use it in GitHub Desktop.
Remove "Take Screenshot" on Ubuntu from window context Menu
// Tested on Ubuntu 24.04. Updated based on the code shared here https://www.reddit.com/r/gnome/comments/vj2s53/comment/idi59rf/
// Follow this guide to create extention https://gjs.guide/extensions/development/creating.html. Add this code in extension.js and don't forget to enable the extension.
// Fan of ALT + Space for minimising windows
import { WindowMenu } from 'resource:///org/gnome/shell/ui/windowMenu.js';
export default class MyTestExtension {
constructor() {
this._originalBuildMenu = null;
}
enable() {
// Ensure that WindowMenu exists and _buildMenu is defined before overriding
if (WindowMenu.prototype._buildMenu) {
// Save the original _buildMenu method
const extensionInstance = this;
this._originalBuildMenu = WindowMenu.prototype._buildMenu;
// Override _buildMenu with proper context preservation
WindowMenu.prototype._buildMenu = function (window) {
// Call the original _buildMenu method with the correct context (i.e., the WindowMenu instance)
extensionInstance._originalBuildMenu.apply(this, [window]);
// Get the menu items
let items = this._getMenuItems();
// Log menu items to verify what's available
/* for (let i = 0; i < items.length; i++) {
log(`Menu item ${i}: ${items[i].label}`);
}
*/
// Remove the "Take Screenshot" option if it exists
for (let i = 0; i < items.length; i++) {
let label = items[i].label.get_text(); // Get the actual text of the label
if (label === "Take Screenshot") {
// log("Removing 'Take Screenshot' option.");
items[i].destroy();
}
}
};
} else {
log("WindowMenu.prototype._buildMenu is undefined when trying to override.");
}
}
disable() {
// Restore the original _buildMenu function
if (this._originalBuildMenu) {
WindowMenu.prototype._buildMenu = this._originalBuildMenu;
this._originalBuildMenu = null;
} else {
log("Unable to restore _buildMenu because the original is undefined.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment