webext-menus
Version:
Create dynamic menus in webextensions
3 lines (2 loc) • 2.34 kB
JavaScript
var webextMenus=function(){"use strict";function e(e){const n=new Map,t=new Map,i=function(e){const n=new Set(["all","bookmark","browser_action","launcher","page_action","tab","tools_menu"]);return Object.values(e.ContextType).filter(e=>!n.has(e))}(e);let o=1;return{create:function(s){const r=!1!==s.visible;delete s.visible,s.id=s.id||`WEBEXT_MENUS/${o++}`,r&&e.create(Object.assign({},s));const c={id:s.id,rawOptions:s,visible:r,pendingVisible:null,prev:[],refreshed:!1};n.set(s.id,c);for(const e of(l=function e(t){return t.rawOptions.contexts?t.rawOptions.contexts:null!=t.rawOptions.parentId?e(n.get(t.rawOptions.parentId)):["page"]}(c),l.reduce((e,n)=>("all"===n?i.forEach(n=>e.add(n)):e.add(n),e),new Set))){const n=`${s.parentId}/${e}`,i=t.get(n);i&&c.prev.push(i),t.set(n,c)}var l;return s.id},update:function(t,i){const o=n.get(t);null!=i.visible&&(i.visible!==o.visible&&(o.pendingVisible=i.visible),delete i.visible);if(Object.assign(o.rawOptions,i),o.visible)return e.update(t,i)},commit:function(){for(const t of n.values())t.prev.some(e=>e.refreshed)&&(t.refreshed=!0,null==t.pendingVisible&&t.visible&&(e.remove(t.id),t.pendingVisible=!0)),!1===t.pendingVisible?(e.remove(t.id),t.visible=!1,t.pendingVisible=null):!0===t.pendingVisible&&(e.create(Object.assign({},t.rawOptions)),t.visible=!0,t.pendingVisible=null,t.refreshed=!0);for(const e of n.values())e.refreshed=!1}}}return function(n,t){const i=function(){const e="undefined"!=typeof browser?browser:chrome;return e.menus||e.contextMenus}(),o=[],s=[],r=(null==t?function(e){let n;try{return n=e.create({visible:!1,title:"test_visible"},()=>{const e="undefined"!=typeof browser?browser:chrome;e.runtime.lastError&&console.warn("failed to create menu when checking visible property",e.runtime.lastError)}),!0}catch(e){return!1}finally{null!=n&&e.remove(n)}}(i):t)?i:e(i);return function(){for(const e of n){const n=Object.assign({},e);"function"==typeof e.checked&&(delete n.checked,s.push(e)),e.oncontext?(delete n.oncontext,o.push(e),e.show=!1,n.visible=!1):e.show=!0,e.id=r.create(n)}}(),{update:function(){(function(){for(const e of o){const n=Boolean(e.oncontext());e.show!==n&&(e.show=n,r.update(e.id,{visible:n}))}r.commit&&r.commit()})(),function(){for(const e of s)r.update(e.id,{checked:e.checked()})}()}}}}();
//# sourceMappingURL=webext-menus.min.js.map