OLD | NEW |
1 /* | 1 /* |
2 * This Source Code is subject to the terms of the Mozilla Public License | 2 * This Source Code is subject to the terms of the Mozilla Public License |
3 * version 2.0 (the "License"). You can obtain a copy of the License at | 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
4 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
5 */ | 5 */ |
6 | 6 |
7 let EXPORTED_SYMBOLS = ["shutdown", "getNodeInfo"]; | 7 let EXPORTED_SYMBOLS = ["shutdown", "getNodeInfo", "togglePreview", |
| 8 "forgetNode"]; |
8 | 9 |
9 const Ci = Components.interfaces; | 10 const Ci = Components.interfaces; |
10 const Cu = Components.utils; | 11 const Cu = Components.utils; |
11 | 12 |
12 let {console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {}); | 13 let {console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {}); |
13 let {DebuggerServer} = Cu.import("resource://gre/modules/devtools/dbg-server.jsm
", {}); | 14 let {DebuggerServer} = Cu.import("resource://gre/modules/devtools/dbg-server.jsm
", {}); |
| 15 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 16 |
| 17 let processID = Services.appinfo.processID; |
| 18 let maxNodeID = 0; |
| 19 let nodes = new Map(); |
| 20 |
14 let name = "elemhidehelper"; | 21 let name = "elemhidehelper"; |
15 let actor = { | 22 let actor = { |
16 constructorFun: Actor, | 23 constructorFun: Actor, |
17 constructorName: name, | 24 constructorName: name, |
18 name: name | 25 name: name |
19 }; | 26 }; |
20 | 27 |
21 DebuggerServer.addTabActor(actor, name); | 28 DebuggerServer.addTabActor(actor, name); |
22 | 29 |
23 let shutdown = (function() | 30 let shutdown = (function() |
24 { | 31 { |
25 let executed = false; | 32 let executed = false; |
26 return function() | 33 return function() |
27 { | 34 { |
28 if (!executed) | 35 if (!executed) |
29 DebuggerServer.removeTabActor(actor); | 36 { |
30 executed = true; | 37 executed = true; |
| 38 try |
| 39 { |
| 40 DebuggerServer.removeTabActor(actor); |
| 41 } |
| 42 catch (e) |
| 43 { |
| 44 // The call above will throw in the content process despite succeeding, |
| 45 // see https://bugzilla.mozilla.org/show_bug.cgi?id=1189780. |
| 46 Cu.reportError(e); |
| 47 } |
| 48 } |
31 } | 49 } |
32 })(); | 50 })(); |
33 | 51 |
34 function Actor(connection, tabActor) | 52 function Actor(connection, tabActor) |
35 { | 53 { |
36 } | 54 } |
37 | 55 |
38 Actor.prototype = { | 56 Actor.prototype = { |
39 requestTypes: { | 57 requestTypes: { |
40 nodeinfo: function(request, connection) | 58 nodeinfo: function(request, connection) |
41 { | 59 { |
42 let nodeActor = connection.getActor(request.nodeActor); | 60 let nodeActor = connection.getActor(request.nodeActor); |
43 if (!nodeActor || !nodeActor.rawNode || | 61 return getNodeInfo(nodeActor ? nodeActor.rawNode: null); |
44 nodeActor.rawNode.nodeType != Ci.nsIDOMNode.ELEMENT_NODE) | |
45 { | |
46 return {}; | |
47 } | |
48 | |
49 return getNodeInfo(nodeActor.rawNode); | |
50 } | 62 } |
51 } | 63 } |
52 }; | 64 }; |
53 | 65 |
54 function getNodeInfo(node) | 66 function getNodeInfo(node) |
55 { | 67 { |
56 return { | 68 let nodeData = getNodeData(node); |
57 host: node.ownerDocument.defaultView.location.hostname, | 69 if (nodeData) |
58 nodeData: getNodeData(node) | 70 { |
59 }; | 71 let nodeID = processID + "-" + (++maxNodeID); |
| 72 nodes.set(nodeID, {document: node.ownerDocument, style: null}); |
| 73 return { |
| 74 host: node.ownerDocument.defaultView.location.hostname, |
| 75 nodeData: nodeData, |
| 76 nodeID: nodeID |
| 77 }; |
| 78 } |
| 79 |
| 80 return {}; |
60 } | 81 } |
61 | 82 |
62 function getNodeData(node, parentNode) | 83 function getNodeData(node, parentNode) |
63 { | 84 { |
64 if (!node) | 85 if (!node || node.nodeType != Ci.nsIDOMNode.ELEMENT_NODE) |
65 return null; | 86 return null; |
66 | 87 |
67 let result = {}; | 88 let result = {}; |
68 result.tagName = {value: node.tagName, checked: false}; | 89 result.tagName = {value: node.tagName, checked: false}; |
69 | 90 |
70 if (typeof parentNode != "undefined") | 91 if (typeof parentNode != "undefined") |
71 result.parentNode = parentNode; | 92 result.parentNode = parentNode; |
72 else | 93 else |
73 result.parentNode = getNodeData(node.parentElement); | 94 result.parentNode = getNodeData(node.parentElement); |
74 | 95 |
(...skipping 26 matching lines...) Expand all Loading... |
101 { | 122 { |
102 // Make sure ID attribute comes first | 123 // Make sure ID attribute comes first |
103 let tmp = result.attributes[1]; | 124 let tmp = result.attributes[1]; |
104 result.attributes[1] = result.attributes[0]; | 125 result.attributes[1] = result.attributes[0]; |
105 result.attributes[0] = tmp; | 126 result.attributes[0] = tmp; |
106 } | 127 } |
107 | 128 |
108 result.customCSS = {selected: "", checked: false}; | 129 result.customCSS = {selected: "", checked: false}; |
109 return result; | 130 return result; |
110 } | 131 } |
| 132 |
| 133 function togglePreview(nodeID, stylesheetData) |
| 134 { |
| 135 let context = nodes.get(nodeID); |
| 136 if (!context) |
| 137 return; |
| 138 |
| 139 if (stylesheetData) |
| 140 { |
| 141 if (!context.style || !context.style.parentNode) |
| 142 { |
| 143 context.style = context.document.createElementNS( |
| 144 "http://www.w3.org/1999/xhtml", "style"); |
| 145 context.style.setAttribute("type", "text/css"); |
| 146 context.document.documentElement.appendChild(context.style); |
| 147 } |
| 148 context.style.textContent = stylesheetData; |
| 149 } |
| 150 else |
| 151 { |
| 152 try |
| 153 { |
| 154 if (context.style && context.style.parentNode) |
| 155 context.style.parentNode.removeChild(context.style); |
| 156 context.style = null; |
| 157 } |
| 158 catch (e) |
| 159 { |
| 160 // If the window was closed (reloaded) we end up with a dead object |
| 161 // reference (https://bugzilla.mozilla.org/show_bug.cgi?id=695480). Just |
| 162 // forget this node then. |
| 163 forgetNode(nodeID); |
| 164 } |
| 165 } |
| 166 } |
| 167 |
| 168 function forgetNode(nodeID) |
| 169 { |
| 170 nodes.delete(nodeID); |
| 171 } |
OLD | NEW |