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 /** |
| 28 * Determines the context menu entries to be shown for a contextmenu event. |
| 29 * @param {Event} event |
| 30 * @return {Array} |
| 31 */ |
| 32 function getContextInfo(event) |
| 33 { |
| 34 let items = []; |
| 35 let target = event.target; |
| 36 if (target.localName == "menupopup" && target.triggerNode) |
| 37 { |
| 38 // SeaMonkey gives us the context menu's popupshowing event |
| 39 target = target.triggerNode; |
| 40 } |
| 41 if (target instanceof Ci.nsIDOMHTMLMapElement || target instanceof Ci.nsIDOMHT
MLAreaElement) |
| 42 { |
| 43 // HTML image maps will usually receive events when the mouse pointer is |
| 44 // over a different element, get the real event target. |
| 45 let rect = target.getClientRects()[0]; |
| 46 target = target.ownerDocument.elementFromPoint(Math.max(rect.left, 0), Math.
max(rect.top, 0)); |
| 47 } |
| 48 |
| 49 if (!target) |
| 50 return items; |
| 51 |
| 52 let addMenuItem = function([node, nodeData]) |
| 53 { |
| 54 let nodeID = null; |
| 55 if (node && node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
| 56 nodeID = storeNodes([node]); |
| 57 items.push([nodeID, nodeData]); |
| 58 }.bind(this); |
| 59 |
| 60 // Look up data that we have for the node |
| 61 let data = RequestNotifier.getDataForNode(target); |
| 62 let hadImage = false; |
| 63 if (data && !data[1].filter) |
| 64 { |
| 65 addMenuItem(data); |
| 66 hadImage = (data[1].type == "IMAGE"); |
| 67 } |
| 68 |
| 69 // Look for frame data |
| 70 let wnd = Utils.getWindow(target); |
| 71 if (wnd.frameElement) |
| 72 { |
| 73 let data = RequestNotifier.getDataForNode(wnd.frameElement, true); |
| 74 if (data && !data[1].filter) |
| 75 addMenuItem(data); |
| 76 } |
| 77 |
| 78 // Look for a background image |
| 79 if (!hadImage) |
| 80 { |
| 81 let extractImageURL = function(computedStyle, property) |
| 82 { |
| 83 let value = computedStyle.getPropertyCSSValue(property); |
| 84 // CSSValueList |
| 85 if ("length" in value && value.length >= 1) |
| 86 value = value[0]; |
| 87 // CSSValuePrimitiveType |
| 88 if ("primitiveType" in value && value.primitiveType == value.CSS_URI) |
| 89 return Utils.unwrapURL(value.getStringValue()).spec; |
| 90 |
| 91 return null; |
| 92 }; |
| 93 |
| 94 let node = target; |
| 95 while (node) |
| 96 { |
| 97 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
| 98 { |
| 99 let style = wnd.getComputedStyle(node, ""); |
| 100 let bgImage = extractImageURL(style, "background-image") || extractImage
URL(style, "list-style-image"); |
| 101 if (bgImage) |
| 102 { |
| 103 let data = RequestNotifier.getDataForNode(wnd.document, true, "IMAGE",
bgImage); |
| 104 if (data && !data[1].filter) |
| 105 { |
| 106 addMenuItem(data); |
| 107 break; |
| 108 } |
| 109 } |
| 110 } |
| 111 |
| 112 node = node.parentNode; |
| 113 } |
| 114 } |
| 115 |
| 116 return items; |
| 117 }; |
| 118 |
| 119 let ContextMenuObserver = |
| 120 { |
| 121 observe: function(subject, topic, data) |
| 122 { |
| 123 if (topic == "content-contextmenu") |
| 124 { |
| 125 if (subject.wrappedJSObject) |
| 126 subject = subject.wrappedJSObject; |
| 127 |
| 128 if (subject.addonInfo) |
| 129 subject.addonInfo.adblockplus = getContextInfo(subject.event); |
| 130 } |
| 131 }, |
| 132 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse
rver]) |
| 133 }; |
| 134 |
| 135 let addObserver = Utils.getPropertyWithoutCompatShims(Services.obs, "addObserver
"); |
| 136 addObserver.call(Services.obs, ContextMenuObserver, "content-contextmenu", true)
; |
| 137 onShutdown.add(() => { |
| 138 let removeObserver = Utils.getPropertyWithoutCompatShims(Services.obs, "remove
Observer"); |
| 139 removeObserver.call(Services.obs, ContextMenuObserver, "content-contextmenu"); |
| 140 }); |
OLD | NEW |