libCtxtMenu

Simple adapter for adding legacy browsers rightclick menu commands.

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/435215/986494/libCtxtMenu.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// ==UserLibrary==
// @name         libCtxtMenu
// @namespace    https://openuserjs.org/users/Anakunda
// @version      1.00.0
// @author       Anakunda
// @license      GPL-3.0-or-later
// @copyright    2021, Anakunda (https://openuserjs.org/users/Anakunda)
// ==/UserScript==
// ==/UserLibrary==

'use strict';

class ContextMenu {
	constructor(id, create = true) {
		if (!id) {
			let dt = new Date().getTime();
			this.id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
				const r = (dt + Math.random() * 16) % 16 | 0;
				dt = Math.floor(dt / 16);
				return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
			});
		}
		if (!(this.new = (this.menu = document.body.querySelector(`menu[type="context"][id="${id}"]`)) == null)) return;
		this.menu = document.createElement('MENU');
		this.menu.type = 'context';
		this.menu.id = id;
		if (create) this.create();
	}

	create() {
		if (this.menu.parentNode == null) document.body.append(this.menu);
	}
	destroy() {
		if (this.menu.parentNode != null) this.menu.parentNode.removeChild(this.menu);
	}
	addItem(caption, callback) {
		if (!this.new) return false;
		console.assert(this.menu instanceof HTMLMenuElement);
		const menuItem = document.createElement('MENUITEM');
		if (caption) menuItem.label = caption; else return;
		if (typeof callback == 'function') {
			menuItem.type = 'command';
			menuItem.onclick = evt => {
				console.assert(this.target instanceof HTMLElement);
				return this.target instanceof HTMLElement ? callback(this.target) : false; //callback.call(this.target, this.target)
			};
		}
		this.menu.append(menuItem);
		return true;
	}

	attach(element) {
		if (!(element instanceof HTMLElement)) throw 'Invalid argument';
		console.assert(this.menu instanceof HTMLMenuElement);
		this.create();
		element.setAttribute('contextmenu', this.menu.id);
		element.oncontextmenu = evt => { this.target = evt.currentTarget };
	}
	detach(element) {
		if (!(element instanceof HTMLElement)) throw 'Invalid argument';
		console.assert(this.menu instanceof HTMLMenuElement);
		if (element.getAttribute('contextmenu') != this.menu.id) return;
		element.oncontextmenu = null;
		element.removeAttribute('contextmenu');
	}
}