LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 (function() | 18 (function() |
19 { | 19 { |
20 /* Background page proxy */ | 20 /* Background page proxy */ |
21 | |
22 var proxy = { | 21 var proxy = { |
23 objects: [], | 22 objects: [], |
24 callbacks: [], | 23 callbacks: [], |
25 | 24 |
26 send: function(message) | 25 send: function(message) |
27 { | 26 { |
28 var evt = document.createEvent("Event"); | 27 var evt = document.createEvent("Event"); |
29 evt.initEvent("beforeload"); | 28 evt.initEvent("beforeload"); |
30 return safari.self.tab.canLoad(evt, {type: "proxy", payload: message}); | 29 return safari.self.tab.canLoad(evt, {type: "proxy", payload: message}); |
31 }, | 30 }, |
(...skipping 28 matching lines...) Expand all Loading... |
60 obj.apply( | 59 obj.apply( |
61 this.getObject(event.message.contextId), | 60 this.getObject(event.message.contextId), |
62 this.deserializeSequence(event.message.args) | 61 this.deserializeSequence(event.message.args) |
63 ); | 62 ); |
64 }.bind(this)); | 63 }.bind(this)); |
65 } | 64 } |
66 | 65 |
67 return {type: "callback", callbackId: callbackId}; | 66 return {type: "callback", callbackId: callbackId}; |
68 } | 67 } |
69 | 68 |
70 if (typeof obj == "object") | 69 if (typeof obj == "object" && |
71 if (obj != null) | 70 obj != null && |
72 if (obj.constructor != Date) | 71 obj.constructor != Date && |
73 if (obj.constructor != RegExp) | 72 obj.constructor != RegExp) |
74 { | 73 { |
75 if (!memo) | 74 if (!memo) |
76 memo = {specs: [], objects: []}; | 75 memo = {specs: [], objects: []}; |
77 | 76 |
78 var idx = memo.objects.indexOf(obj); | 77 var idx = memo.objects.indexOf(obj); |
79 if (idx != -1) | 78 if (idx != -1) |
80 return memo.specs[idx]; | 79 return memo.specs[idx]; |
81 | 80 |
82 var spec = {}; | 81 var spec = {}; |
83 memo.specs.push(spec); | 82 memo.specs.push(spec); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 117 |
119 return array; | 118 return array; |
120 }, | 119 }, |
121 deserialize: function(spec, memo) | 120 deserialize: function(spec, memo) |
122 { | 121 { |
123 switch (spec.type) | 122 switch (spec.type) |
124 { | 123 { |
125 case "value": | 124 case "value": |
126 return spec.value; | 125 return spec.value; |
127 case "object": | 126 case "object": |
128 case "function": | 127 return this.getObject(spec.objectId); |
129 return this.getObject(spec.objectId, spec.type); | |
130 case "array": | 128 case "array": |
131 if (!memo) | 129 if (!memo) |
132 memo = {specs: [], arrays: []}; | 130 memo = {specs: [], arrays: []}; |
133 | 131 |
134 var idx = memo.specs.indexOf(spec); | 132 var idx = memo.specs.indexOf(spec); |
135 if (idx != -1) | 133 if (idx != -1) |
136 return memo.arrays[idx]; | 134 return memo.arrays[idx]; |
137 | 135 |
138 var array = []; | 136 var array = []; |
139 memo.specs.push(spec); | 137 memo.specs.push(spec); |
140 memo.arrays.push(array); | 138 memo.arrays.push(array); |
141 | 139 |
142 return this.deserializeSequence(spec.items, array, memo); | 140 return this.deserializeSequence(spec.items, array, memo); |
143 } | 141 } |
144 }, | 142 }, |
145 getProperty: function(objectId, property) | 143 getProperty: function(objectId, property) |
146 { | 144 { |
147 return this.deserializeResult( | 145 return this.deserializeResult( |
148 this.send({ | 146 this.send( |
| 147 { |
149 type: "getProperty", | 148 type: "getProperty", |
150 objectId: objectId, | 149 objectId: objectId, |
151 property: property | 150 property: property |
152 }) | 151 }) |
153 ); | 152 ); |
154 }, | 153 }, |
155 createProperty: function(objectId, property, enumerable) | 154 createProperty: function(objectId, property, enumerable) |
156 { | 155 { |
157 return { | 156 return { |
158 get: function() | 157 get: function() |
159 { | 158 { |
160 return this.getProperty(objectId, property); | 159 return this.getProperty(objectId, property); |
161 }.bind(this), | 160 }.bind(this), |
162 set: function(value) | 161 set: function(value) |
163 { | 162 { |
164 this.checkResult( | 163 this.checkResult( |
165 this.send({ | 164 this.send( |
| 165 { |
166 type: "setProperty", | 166 type: "setProperty", |
167 objectId: objectId, | 167 objectId: objectId, |
168 property: property, | 168 property: property, |
169 value: this.serialize(value) | 169 value: this.serialize(value) |
170 }) | 170 }) |
171 ) | 171 ); |
172 }.bind(this), | 172 }.bind(this), |
173 enumerable: enumerable | 173 enumerable: enumerable, |
| 174 configurable: true |
174 }; | 175 }; |
175 }, | 176 }, |
176 createObject: function(objectId) | |
177 { | |
178 var objectInfo = this.send({ | |
179 type: "inspectObject", | |
180 objectId: objectId | |
181 }); | |
182 | |
183 var prototype; | |
184 if (objectInfo.prototypeId != null) | |
185 prototype = this.getObject(objectInfo.prototypeId); | |
186 else | |
187 prototype = Object.prototype; | |
188 | |
189 var properties = {}; | |
190 for (var property in objectInfo.properties) | |
191 properties[property] = this.createProperty( | |
192 objectId, property, | |
193 objectInfo.properties[property].enumerable | |
194 ); | |
195 | |
196 return Object.create(prototype, properties); | |
197 }, | |
198 createFunction: function(objectId) | 177 createFunction: function(objectId) |
199 { | 178 { |
200 var objectInfo = this.send({ | |
201 type: "inspectObject", | |
202 objectId: objectId | |
203 }); | |
204 | |
205 var proxy = this; | 179 var proxy = this; |
206 var func = function() | 180 return function() |
207 { | 181 { |
208 return proxy.deserializeResult( | 182 return proxy.deserializeResult( |
209 proxy.send({ | 183 proxy.send( |
| 184 { |
210 type: "callFunction", | 185 type: "callFunction", |
211 functionId: objectId, | 186 functionId: objectId, |
212 contextId: proxy.objects.indexOf(this), | 187 contextId: proxy.objects.indexOf(this), |
213 args: Array.prototype.map.call( | 188 args: Array.prototype.map.call( |
214 arguments, | 189 arguments, |
215 proxy.serialize.bind(proxy) | 190 proxy.serialize.bind(proxy) |
216 ) | 191 ) |
217 }) | 192 }) |
218 ); | 193 ); |
219 }; | 194 }; |
220 | 195 }, |
221 var builtin = Object.getOwnPropertyNames(func); | 196 getObject: function(objectId) { |
| 197 var objectInfo = this.send({ |
| 198 type: "inspectObject", |
| 199 objectId: objectId |
| 200 }); |
| 201 |
| 202 var obj = this.objects[objectId]; |
| 203 if (obj) |
| 204 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop
]; }); |
| 205 else |
| 206 { |
| 207 if (objectInfo.isFunction) |
| 208 obj = this.createFunction(objectId); |
| 209 else |
| 210 obj = {}; |
| 211 |
| 212 this.objects[objectId] = obj; |
| 213 } |
| 214 |
| 215 var ignored = []; |
| 216 if ("prototypeOf" in objectInfo) |
| 217 { |
| 218 var prototype = window[objectInfo.prototypeOf].prototype; |
| 219 |
| 220 ignored = Object.getOwnPropertyNames(prototype); |
| 221 ignored.splice(ignored.indexOf("constructor"), 1); |
| 222 |
| 223 obj.__proto__ = prototype; |
| 224 } |
| 225 else |
| 226 { |
| 227 if (objectInfo.isFunction) |
| 228 ignored = Object.getOwnPropertyNames(function() {}); |
| 229 else |
| 230 ignored = []; |
| 231 |
| 232 if ("prototypeId" in objectInfo) |
| 233 obj.__proto__ = this.getObject(objectInfo.prototypeId); |
| 234 else |
| 235 obj.__proto__ = null; |
| 236 } |
| 237 |
222 for (var property in objectInfo.properties) | 238 for (var property in objectInfo.properties) |
223 if (builtin.indexOf(property) == -1) | 239 if (ignored.indexOf(property) == -1) |
224 Object.defineProperty(func, property, this.createProperty( | 240 Object.defineProperty(obj, property, this.createProperty( |
225 objectId, property, | 241 objectId, property, |
226 objectInfo.properties[property].enumerable | 242 objectInfo.properties[property].enumerable |
227 )); | 243 )); |
228 | 244 |
229 func.prototype = this.getProperty(objectId, "prototype"); | 245 if (objectInfo.isFunction) |
230 return func; | 246 obj.prototype = this.getProperty(objectId, "prototype"); |
231 }, | |
232 getObject: function(objectId, type) | |
233 { | |
234 var obj = this.objects[objectId]; | |
235 | |
236 if (!obj) | |
237 { | |
238 if (type == "function") | |
239 obj = this.createFunction(objectId); | |
240 else | |
241 obj = this.createObject(objectId); | |
242 | |
243 this.objects[objectId] = obj; | |
244 } | |
245 | 247 |
246 return obj; | 248 return obj; |
247 } | 249 } |
248 }; | 250 }; |
249 | 251 |
250 | 252 |
251 /* Web request blocking */ | 253 /* Web request blocking */ |
252 | 254 |
253 document.addEventListener("beforeload", function(event) | 255 document.addEventListener("beforeload", function(event) |
254 { | 256 { |
(...skipping 23 matching lines...) Expand all Loading... |
278 } | 280 } |
279 default: | 281 default: |
280 type = "other"; | 282 type = "other"; |
281 } | 283 } |
282 | 284 |
283 if (!safari.self.tab.canLoad(event, {type: "webRequest", payload: {url: even
t.url, type: type}})) | 285 if (!safari.self.tab.canLoad(event, {type: "webRequest", payload: {url: even
t.url, type: type}})) |
284 event.preventDefault(); | 286 event.preventDefault(); |
285 }, true); | 287 }, true); |
286 | 288 |
287 | 289 |
288 /* Fix absolute URLs on extention pages */ | |
289 | |
290 if (document.location.href.indexOf(safari.extension.baseURI) == 0) { | |
291 document.addEventListener("DOMContentLoaded", function(event) | |
292 { | |
293 var attributes = ['href', 'src']; | |
294 for (var i = 0; i < attributes.length; i++) | |
295 { | |
296 var elements = document.querySelectorAll("[" + attributes[i] + "^='/']")
; | |
297 for (var j = 0; j < elements.length; j++) | |
298 elements[j][attributes[i]] = safari.extension.baseURI + elements[j][at
tributes[i]].substr(1); | |
299 } | |
300 }); | |
301 } | |
302 | |
303 | |
304 /* API */ | 290 /* API */ |
305 | 291 |
306 ext.backgroundPage = { | 292 ext.backgroundPage = { |
307 _eventTarget: safari.self, | 293 _eventTarget: safari.self, |
308 _messageDispatcher: safari.self.tab, | 294 _messageDispatcher: safari.self.tab, |
309 | 295 |
310 sendMessage: sendMessage, | 296 sendMessage: sendMessage, |
311 getWindow: function() { return proxy.getObject(0); } | 297 getWindow: function() { return proxy.getObject(0); } |
312 }; | 298 }; |
313 | 299 |
314 ext.onMessage = new MessageEventTarget(safari.self); | 300 ext.onMessage = new MessageEventTarget(safari.self); |
315 })(); | 301 })(); |
LEFT | RIGHT |