add the onclick event seperately
This commit is contained in:
parent
f5cf3f09a2
commit
bdd8eda819
1 changed files with 78 additions and 59 deletions
|
@ -5,6 +5,7 @@ export default class Menu {
|
||||||
|
|
||||||
constructor (ID, title, contexts, event, type, icon) {
|
constructor (ID, title, contexts, event, type, icon) {
|
||||||
if ((typeof ID).includes(`obj`) && !Array.isArray(ID)) {
|
if ((typeof ID).includes(`obj`) && !Array.isArray(ID)) {
|
||||||
|
// Create the ID if it doesn't exist.
|
||||||
ID.ID = ((ID.hasOwnProperty(`ID`)) ? ID.ID : false) ? ID.ID : String(Math.random() / Math.random() * 100);
|
ID.ID = ((ID.hasOwnProperty(`ID`)) ? ID.ID : false) ? ID.ID : String(Math.random() / Math.random() * 100);
|
||||||
|
|
||||||
(Object.keys(ID)).forEach((key) => {
|
(Object.keys(ID)).forEach((key) => {
|
||||||
|
@ -14,7 +15,7 @@ export default class Menu {
|
||||||
this.ID = String((ID) ? ID : (Math.random() / Math.random() * 100));
|
this.ID = String((ID) ? ID : (Math.random() / Math.random() * 100));
|
||||||
this.title = (title) ? title : `Menu`;
|
this.title = (title) ? title : `Menu`;
|
||||||
this.contexts = (Array.isArray(contexts)) ? contexts : [`all`];
|
this.contexts = (Array.isArray(contexts)) ? contexts : [`all`];
|
||||||
this.event = (event) ? event : function() {};
|
this.events = (event) ? event : {"onClicked" : function() {}};
|
||||||
this.type = (((typeof type).includes(`str`) && type) ? type.trim() : false) ? type : `normal`;
|
this.type = (((typeof type).includes(`str`) && type) ? type.trim() : false) ? type : `normal`;
|
||||||
|
|
||||||
if (icon) {
|
if (icon) {
|
||||||
|
@ -26,8 +27,7 @@ export default class Menu {
|
||||||
id: this.ID,
|
id: this.ID,
|
||||||
title: this.title,
|
title: this.title,
|
||||||
contexts: this.contexts,
|
contexts: this.contexts,
|
||||||
type: this.type,
|
type: this.type
|
||||||
onclick: this.event
|
|
||||||
};
|
};
|
||||||
(this.icon) ? this.#options.icon = this.icon : null;
|
(this.icon) ? this.#options.icon = this.icon : null;
|
||||||
((this.hidden != null) ? (!this.hidden) : true) ? this.show() : null;
|
((this.hidden != null) ? (!this.hidden) : true) ? this.show() : null;
|
||||||
|
@ -40,7 +40,17 @@ export default class Menu {
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
this.hidden = false;
|
this.hidden = false;
|
||||||
chrome.contextMenus.create(this.#options);
|
this.ID = chrome.contextMenus.create(this.#options);
|
||||||
|
|
||||||
|
if (((this.events && (typeof this.events).includes(`obj`) && !Array.isArray(this.events))) ? Object.keys(events) > 0 : false) {
|
||||||
|
(this.events.onClicked)
|
||||||
|
? chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||||
|
((info.menuItemId) ? info.menuItemId == this.ID : false)
|
||||||
|
? this.events.onClicked(info, tab)
|
||||||
|
: false;
|
||||||
|
})
|
||||||
|
: false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update the context menu.
|
/* Update the context menu.
|
||||||
|
@ -57,17 +67,26 @@ export default class Menu {
|
||||||
this.#options = {
|
this.#options = {
|
||||||
title: this.title,
|
title: this.title,
|
||||||
contexts: this.contexts,
|
contexts: this.contexts,
|
||||||
type: this.type,
|
type: this.type
|
||||||
onclick: this.event
|
|
||||||
};
|
};
|
||||||
(this.icon) ? this.#options.icon = this.icon : null;
|
(this.icon) ? this.#options.icon = this.icon : null;
|
||||||
|
|
||||||
chrome.contextMenus.update(this.ID, this.#options);
|
chrome.contextMenus.update(this.ID, this.#options);
|
||||||
|
|
||||||
|
(((this.events && (typeof this.events).includes(`obj`) && !Array.isArray(this.events))) ? Object.keys(events) > 0 : false)
|
||||||
|
? ((this.events.onClicked)
|
||||||
|
? chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||||
|
((info.menuItemId) ? info.menuItemId == this.ID : false)
|
||||||
|
? this.events.onClicked(info, tab)
|
||||||
|
: false;
|
||||||
|
})
|
||||||
|
: false)
|
||||||
|
: false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run a new function when triggered. */
|
/* Run a new function when triggered. */
|
||||||
onclick(callback) {
|
onclick(callback) {
|
||||||
this.event = callback;
|
this.event = {"onClicked": callback};
|
||||||
this.update();
|
this.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue