diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2019-07-04 17:12:55 +0200 |
---|---|---|
committer | Alessio Vanni <vannilla@firemail.cc> | 2019-07-04 17:12:55 +0200 |
commit | a15d6164fa76b14a06de23eecaa850704a77447f (patch) | |
tree | ad11682680b05497bd5f55c0c112d06856cad359 /js/vapi-background.js | |
parent | 194b9f768b7e8ea57217fa6cf7b501727e65b662 (diff) | |
download | ematrix-a15d6164fa76b14a06de23eecaa850704a77447f.tar.lz ematrix-a15d6164fa76b14a06de23eecaa850704a77447f.tar.xz ematrix-a15d6164fa76b14a06de23eecaa850704a77447f.zip |
Split context menu from vapi-background
Also fix a typo in Makefile.
Diffstat (limited to 'js/vapi-background.js')
-rw-r--r-- | js/vapi-background.js | 184 |
1 files changed, 0 insertions, 184 deletions
diff --git a/js/vapi-background.js b/js/vapi-background.js index 1690c06..8f095ae 100644 --- a/js/vapi-background.js +++ b/js/vapi-background.js @@ -1146,190 +1146,6 @@ vAPI.toolbarButton.init(); } - vAPI.contextMenu = { - contextMap: { - frame: 'inFrame', - link: 'onLink', - image: 'onImage', - audio: 'onAudio', - video: 'onVideo', - editable: 'onEditableArea' - } - }; - - vAPI.contextMenu.displayMenuItem = function ({target}) { - let doc = target.ownerDocument; - let gContextMenu = doc.defaultView.gContextMenu; - if (!gContextMenu.browser) { - return; - } - - let menuitem = doc.getElementById(vAPI.contextMenu.menuItemId); - let currentURI = gContextMenu.browser.currentURI; - - // https://github.com/chrisaljoudi/uBlock/issues/105 - // TODO: Should the element picker works on any kind of pages? - if (!currentURI.schemeIs('http') && !currentURI.schemeIs('https')) { - menuitem.setAttribute('hidden', true); - return; - } - - let ctx = vAPI.contextMenu.contexts; - - if (!ctx) { - menuitem.setAttribute('hidden', false); - return; - } - - let ctxMap = vAPI.contextMenu.contextMap; - - for (let context of ctx) { - if (context === 'page' - && !gContextMenu.onLink - && !gContextMenu.onImage - && !gContextMenu.onEditableArea - && !gContextMenu.inFrame - && !gContextMenu.onVideo - && !gContextMenu.onAudio) { - menuitem.setAttribute('hidden', false); - return; - } - - if (ctxMap.hasOwnProperty(context) - && gContextMenu[ctxMap[context]]) { - menuitem.setAttribute('hidden', false); - return; - } - } - - menuitem.setAttribute('hidden', true); - }; - - vAPI.contextMenu.register = (function () { - let register = function (doc) { - if (!this.menuItemId) { - return; - } - - // Already installed? - if (doc.getElementById(this.menuItemId) !== null) { - return; - } - - let contextMenu = doc.getElementById('contentAreaContextMenu'); - - let menuitem = doc.createElement('menuitem'); - menuitem.setAttribute('id', this.menuItemId); - menuitem.setAttribute('label', this.menuLabel); - menuitem.setAttribute('image', vAPI.getURL('img/browsericons/icon19-19.png')); - menuitem.setAttribute('class', 'menuitem-iconic'); - menuitem.addEventListener('command', this.onCommand); - - contextMenu.addEventListener('popupshowing', this.displayMenuItem); - contextMenu.insertBefore(menuitem, doc.getElementById('inspect-separator')); - }; - - var registerSafely = function (doc, tryCount) { - // https://github.com/gorhill/uBlock/issues/906 - // Be sure document.readyState is 'complete': it could happen - // at launch time that we are called by - // vAPI.contextMenu.create() directly before the environment - // is properly initialized. - if (doc.readyState === 'complete') { - register.call(this, doc); - return; - } - - if (typeof tryCount !== 'number') { - tryCount = 0; - } - - tryCount += 1; - if ( tryCount < 8) { - vAPI.setTimeout(registerSafely.bind(this, doc, tryCount), 200); - } - }; - - return registerSafely; - })(); - - vAPI.contextMenu.unregister = function (doc) { - if (!this.menuItemId) { - return; - } - - let menuitem = doc.getElementById(this.menuItemId); - if (menuitem === null) { - return; - } - - let contextMenu = menuitem.parentNode; - menuitem.removeEventListener('command', this.onCommand); - contextMenu.removeEventListener('popupshowing', this.displayMenuItem); - contextMenu.removeChild(menuitem); - }; - - vAPI.contextMenu.create = function (details, callback) { - this.menuItemId = details.id; - this.menuLabel = details.title; - this.contexts = details.contexts; - - if (Array.isArray(this.contexts) && this.contexts.length) { - this.contexts = this.contexts.indexOf('all') === -1 - ? this.contexts - : null; - } else { - // default in Chrome - this.contexts = ['page']; - } - - this.onCommand = function () { - let gContextMenu = vAPI.browser.getOwnerWindow(this).gContextMenu; - let details = { - menuItemId: this.id - }; - - if (gContextMenu.inFrame) { - details.tagName = 'iframe'; - // Probably won't work with e10s - // eMatrix: doesn't matter ;) - details.frameUrl = gContextMenu.focusedWindow.location.href; - } else if (gContextMenu.onImage) { - details.tagName = 'img'; - details.srcUrl = gContextMenu.mediaURL; - } else if (gContextMenu.onAudio) { - details.tagName = 'audio'; - details.srcUrl = gContextMenu.mediaURL; - } else if (gContextMenu.onVideo) { - details.tagName = 'video'; - details.srcUrl = gContextMenu.mediaURL; - } else if (gContextMenu.onLink) { - details.tagName = 'a'; - details.linkUrl = gContextMenu.linkURL; - } - - callback(details, { - id: vAPI.tabs.manager.tabIdFromTarget(gContextMenu.browser), - url: gContextMenu.browser.currentURI.asciiSpec - }); - }; - - for (let win of vAPI.window.getWindows()) { - this.register(win.document); - } - }; - - vAPI.contextMenu.remove = function () { - for (let win of vAPI.window.getWindows()) { - this.unregister(win.document); - } - - this.menuItemId = null; - this.menuLabel = null; - this.contexts = null; - this.onCommand = null; - }; - let optionsObserver = (function () { let addonId = 'eMatrix@vannilla.org'; |