OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 21 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
| 22 |
| 23 let {Utils} = require("utils"); |
| 24 let {RequestNotifier} = require("child/requestNotifier"); |
| 25 let {storeNodes} = require("child/contentPolicy"); |
| 26 |
| 27 let getContextInfo = |
| 28 /** |
| 29 * Determines the context menu entries to be shown for a contextmenu event. |
| 30 * @param {Event} event |
| 31 * @return {Array} |
| 32 */ |
| 33 exports.getContextInfo = function(event) |
| 34 { |
| 35 let items = []; |
| 36 let target = event.target; |
| 37 if (target.localName == "menupopup" && target.triggerNode) |
| 38 { |
| 39 // SeaMonkey gives us the context menu's popupshowing event |
| 40 target = target.triggerNode; |
| 41 } |
| 42 if (target instanceof Ci.nsIDOMHTMLMapElement || target instanceof Ci.nsIDOMHT
MLAreaElement) |
| 43 { |
| 44 // HTML image maps will usually receive events when the mouse pointer is |
| 45 // over a different element, get the real event target. |
| 46 let rect = target.getClientRects()[0]; |
| 47 target = target.ownerDocument.elementFromPoint(Math.max(rect.left, 0), Math.
max(rect.top, 0)); |
| 48 } |
| 49 |
| 50 if (!target) |
| 51 return items; |
| 52 |
| 53 let addMenuItem = function([node, nodeData]) |
| 54 { |
| 55 let nodeID = null; |
| 56 if (node && node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
| 57 nodeID = storeNodes([node]); |
| 58 items.push([nodeID, nodeData]); |
| 59 }.bind(this); |
| 60 |
| 61 // Look up data that we have for the node |
| 62 let data = RequestNotifier.getDataForNode(target); |
| 63 let hadImage = false; |
| 64 if (data && !data[1].filter) |
| 65 { |
| 66 addMenuItem(data); |
| 67 hadImage = (data[1].type == "IMAGE"); |
| 68 } |
| 69 |
| 70 // Look for frame data |
| 71 let wnd = Utils.getWindow(target); |
| 72 if (wnd.frameElement) |
| 73 { |
| 74 let data = RequestNotifier.getDataForNode(wnd.frameElement, true); |
| 75 if (data && !data[1].filter) |
| 76 addMenuItem(data); |
| 77 } |
| 78 |
| 79 // Look for a background image |
| 80 if (!hadImage) |
| 81 { |
| 82 let extractImageURL = function(computedStyle, property) |
| 83 { |
| 84 let value = computedStyle.getPropertyCSSValue(property); |
| 85 // CSSValueList |
| 86 if ("length" in value && value.length >= 1) |
| 87 value = value[0]; |
| 88 // CSSValuePrimitiveType |
| 89 if ("primitiveType" in value && value.primitiveType == value.CSS_URI) |
| 90 return Utils.unwrapURL(value.getStringValue()).spec; |
| 91 |
| 92 return null; |
| 93 }; |
| 94 |
| 95 let node = target; |
| 96 while (node) |
| 97 { |
| 98 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
| 99 { |
| 100 let style = wnd.getComputedStyle(node, ""); |
| 101 let bgImage = extractImageURL(style, "background-image") || extractImage
URL(style, "list-style-image"); |
| 102 if (bgImage) |
| 103 { |
| 104 let data = RequestNotifier.getDataForNode(wnd.document, true, "IMAGE",
bgImage); |
| 105 if (data && !data[1].filter) |
| 106 { |
| 107 addMenuItem(data); |
| 108 break; |
| 109 } |
| 110 } |
| 111 } |
| 112 |
| 113 node = node.parentNode; |
| 114 } |
| 115 } |
| 116 |
| 117 return items; |
| 118 }; |
| 119 |
| 120 let ContextMenuObserver = |
| 121 { |
| 122 observe: function(subject, topic, data) |
| 123 { |
| 124 if (topic == "content-contextmenu") |
| 125 { |
| 126 if (subject.wrappedJSObject) |
| 127 subject = subject.wrappedJSObject; |
| 128 |
| 129 if (subject.addonInfo) |
| 130 subject.addonInfo.adblockplus = getContextInfo(subject.event); |
| 131 } |
| 132 }, |
| 133 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse
rver]) |
| 134 }; |
| 135 |
| 136 let addObserver = Utils.getPropertyWithoutCompatShims(Services.obs, "addObserver
"); |
| 137 addObserver.call(Services.obs, ContextMenuObserver, "content-contextmenu", true)
; |
| 138 onShutdown.add(() => { |
| 139 let removeObserver = Utils.getPropertyWithoutCompatShims(Services.obs, "remove
Observer"); |
| 140 removeObserver.call(Services.obs, ContextMenuObserver, "content-contextmenu"); |
| 141 }); |
OLD | NEW |