| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 safari.self.tab.dispatchMessage("loading", document.location.href); | 20 safari.self.tab.dispatchMessage("loading", document.location.href); |
| 21 | 21 |
| 22 | 22 |
| 23 /* Background page proxy */ | 23 /* Background page proxy */ |
| 24 | |
| 25 var beforeLoadEvent = document.createEvent("Event"); | |
| 26 beforeLoadEvent.initEvent("beforeload"); | |
| 27 | |
| 24 var proxy = { | 28 var proxy = { |
| 25 objects: [], | 29 objects: [], |
| 26 callbacks: [], | 30 callbacks: [], |
| 27 | 31 |
| 28 send: function(message) | 32 send: function(message) |
| 29 { | 33 { |
| 30 var evt = document.createEvent("Event"); | 34 return safari.self.tab.canLoad(beforeLoadEvent, {type: "proxy", payload: m essage}); |
| 31 evt.initEvent("beforeload"); | |
| 32 return safari.self.tab.canLoad(evt, {type: "proxy", payload: message}); | |
| 33 }, | 35 }, |
| 34 checkResult: function(result) | 36 checkResult: function(result) |
| 35 { | 37 { |
| 36 if (!result.succeed) | 38 if (!result.succeed) |
| 37 throw result.error; | 39 throw result.error; |
| 38 }, | 40 }, |
| 39 deserializeResult: function(result) | 41 deserializeResult: function(result) |
| 40 { | 42 { |
| 41 this.checkResult(result); | 43 this.checkResult(result); |
| 42 return this.deserialize(result.result); | 44 return this.deserialize(result.result); |
| 43 }, | 45 }, |
| 44 serialize: function(obj, memo) | 46 serialize: function(obj, memo) |
| 45 { | 47 { |
| 46 var objectId = this.objects.indexOf(obj); | 48 var objectId = this.getObjectId(obj); |
| 47 if (objectId != -1) | 49 if (objectId != -1) |
| 48 return {type: "hosted", objectId: objectId}; | 50 return {type: "hosted", objectId: objectId}; |
| 49 | 51 |
| 50 if (typeof obj == "function") | 52 if (typeof obj == "function") |
| 51 { | 53 { |
| 52 var callbackId = this.callbacks.indexOf(obj); | 54 var callbackId = this.callbacks.indexOf(obj); |
| 53 | 55 |
| 54 if (callbackId == -1) | 56 if (callbackId == -1) |
| 55 { | 57 { |
| 56 callbackId = this.callbacks.push(obj) - 1; | 58 callbackId = this.callbacks.push(obj) - 1; |
| 57 | 59 |
| 58 safari.self.addEventListener("message", function(event) | 60 if (callbackId == 0) |
|
Wladimir Palant
2014/01/15 16:21:01
This is non-obvious and needs a short explanation
| |
| 59 { | 61 { |
| 60 if (event.name == "proxyCallback") | 62 safari.self.addEventListener("message", function(event) |
| 61 if (event.message.callbackId == callbackId) | 63 { |
| 62 obj.apply( | 64 if (event.name == "proxyCallback") |
| 63 this.getObject(event.message.contextId), | 65 { |
| 64 this.deserializeSequence(event.message.args) | 66 this.callbacks[event.message.callbackId].apply( |
| 65 ); | 67 this.getObject(event.message.contextId), |
| 66 }.bind(this)); | 68 this.deserializeSequence(event.message.args) |
| 69 ); | |
| 70 } | |
| 71 }.bind(this)); | |
| 72 } | |
| 67 } | 73 } |
| 68 | 74 |
| 69 return {type: "callback", callbackId: callbackId}; | 75 return {type: "callback", callbackId: callbackId}; |
| 70 } | 76 } |
| 71 | 77 |
| 72 if (typeof obj == "object" && | 78 if (typeof obj == "object" && |
| 73 obj != null && | 79 obj != null && |
| 74 obj.constructor != Date && | 80 obj.constructor != Date && |
| 75 obj.constructor != RegExp) | 81 obj.constructor != RegExp) |
| 76 { | 82 { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 functionId: objectId, | 200 functionId: objectId, |
| 195 contextId: proxy.getObjectId(this), | 201 contextId: proxy.getObjectId(this), |
| 196 args: Array.prototype.map.call( | 202 args: Array.prototype.map.call( |
| 197 arguments, | 203 arguments, |
| 198 proxy.serialize.bind(proxy) | 204 proxy.serialize.bind(proxy) |
| 199 ) | 205 ) |
| 200 }) | 206 }) |
| 201 ); | 207 ); |
| 202 }; | 208 }; |
| 203 }, | 209 }, |
| 204 getObject: function(objectId) { | 210 getObject: function(objectId) |
| 211 { | |
| 205 var objectInfo = this.send({ | 212 var objectInfo = this.send({ |
| 206 type: "inspectObject", | 213 type: "inspectObject", |
| 207 objectId: objectId | 214 objectId: objectId |
| 208 }); | 215 }); |
| 209 | 216 |
| 210 var obj = this.objects[objectId]; | 217 var obj = this.objects[objectId]; |
| 211 if (obj) | 218 if (obj) |
| 212 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop ]; }); | 219 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop ]; }); |
| 213 else | 220 else |
| 214 { | 221 { |
| 215 if (objectInfo.isFunction) | 222 if (objectInfo.isFunction) |
| 216 obj = this.createFunction(objectId); | 223 obj = this.createFunction(objectId); |
| 217 else | 224 else |
| 218 obj = {}; | 225 obj = {}; |
| 219 | 226 |
| 220 this.objects[objectId] = obj; | 227 this.objects[objectId] = obj; |
| 221 } | 228 } |
| 222 | 229 |
| 223 var ignored = []; | 230 var ignored = []; |
| 224 if ("prototypeOf" in objectInfo) | 231 if ("prototypeOf" in objectInfo) |
| 225 { | 232 { |
| 226 var prototype = window[objectInfo.prototypeOf].prototype; | 233 var prototype = window[objectInfo.prototypeOf].prototype; |
| 227 | 234 |
| 228 ignored = Object.getOwnPropertyNames(prototype); | 235 ignored = Object.getOwnPropertyNames(prototype); |
| 229 ignored.splice(ignored.indexOf("constructor"), 1); | 236 ignored.splice(ignored.indexOf("constructor"), 1); |
|
Wladimir Palant
2014/01/15 16:21:01
The assumption here is that ignored.indexOf() will
| |
| 230 | 237 |
| 231 obj.__proto__ = prototype; | 238 obj.__proto__ = prototype; |
| 232 } | 239 } |
| 233 else | 240 else |
| 234 { | 241 { |
| 235 if (objectInfo.isFunction) | 242 if (objectInfo.isFunction) |
| 243 { | |
| 236 ignored = Object.getOwnPropertyNames(function() {}); | 244 ignored = Object.getOwnPropertyNames(function() {}); |
| 245 ignored.splice(ignored.indexOf("prototype"), 1); | |
| 246 } | |
| 237 else | 247 else |
| 238 ignored = []; | 248 ignored = []; |
| 239 | 249 |
| 240 if ("prototypeId" in objectInfo) | 250 if ("prototypeId" in objectInfo) |
| 241 obj.__proto__ = this.getObject(objectInfo.prototypeId); | 251 obj.__proto__ = this.getObject(objectInfo.prototypeId); |
| 242 else | 252 else |
| 243 obj.__proto__ = null; | 253 obj.__proto__ = null; |
| 244 } | 254 } |
| 245 | 255 |
| 246 for (var property in objectInfo.properties) | 256 for (var property in objectInfo.properties) |
| 247 if (ignored.indexOf(property) == -1) | 257 { |
| 258 if (ignored.indexOf(property) != -1) | |
| 259 continue; | |
| 260 | |
| 261 if (!Object.hasOwnProperty(obj, property) || Object.getOwnPropertyDescri ptor(obj, property).configurable) | |
| 262 { | |
| 248 Object.defineProperty(obj, property, this.createProperty( | 263 Object.defineProperty(obj, property, this.createProperty( |
| 249 property, objectInfo.properties[property].enumerable | 264 property, objectInfo.properties[property].enumerable |
| 250 )); | 265 )); |
| 251 | 266 } |
| 252 if (objectInfo.isFunction) | 267 else |
| 253 obj.prototype = this.getProperty(objectId, "prototype"); | 268 obj[property] = this.getProperty(objectId, property); |
| 269 } | |
| 254 | 270 |
| 255 return obj; | 271 return obj; |
| 256 } | 272 } |
| 257 }; | 273 }; |
| 258 | 274 |
| 259 | 275 |
| 260 /* Web request blocking */ | 276 /* Web request blocking */ |
| 261 | 277 |
| 262 document.addEventListener("beforeload", function(event) | 278 document.addEventListener("beforeload", function(event) |
| 263 { | 279 { |
| 264 var type; | 280 var type; |
| 265 | 281 |
| 266 switch(event.target.nodeName) | 282 switch(event.target.localName) |
| 267 { | 283 { |
| 268 case "FRAME": | 284 case "frame": |
| 269 case "IFRAME": | 285 case "iframe": |
| 270 type = "frame"; | 286 type = "frame"; |
| 271 break; | 287 break; |
| 272 case "IMG": | 288 case "img": |
| 273 type = "image"; | 289 type = "image"; |
| 274 break; | 290 break; |
| 275 case "OBJECT": | 291 case "object": |
| 276 case "EMBED": | 292 case "embed": |
| 277 type = "object"; | 293 type = "object"; |
| 278 break; | 294 break; |
| 279 case "SCRIPT": | 295 case "script": |
| 280 type = "script"; | 296 type = "script"; |
| 281 break; | 297 break; |
| 282 case "LINK": | 298 case "link": |
| 283 if (/(^|\s)stylesheet($|\s)/i.test(event.target.rel)) | 299 if (/\bstylesheet\b/i.test(event.target.rel)) |
| 284 { | 300 { |
| 285 type = "stylesheet"; | 301 type = "stylesheet"; |
| 286 break; | 302 break; |
| 287 } | 303 } |
| 288 default: | 304 default: |
| 289 type = "other"; | 305 type = "other"; |
| 290 } | 306 } |
| 291 | 307 |
| 292 if (!safari.self.tab.canLoad(event, {type: "webRequest", payload: {url: even t.url, type: type}})) | 308 if (!safari.self.tab.canLoad(event, {type: "webRequest", payload: {url: even t.url, type: type}})) |
| 293 event.preventDefault(); | 309 event.preventDefault(); |
| 294 }, true); | 310 }, true); |
| 295 | 311 |
| 296 | 312 |
| 297 /* API */ | 313 /* API */ |
| 298 | 314 |
| 299 ext.backgroundPage = { | 315 ext.backgroundPage = { |
| 300 _eventTarget: safari.self, | 316 _eventTarget: safari.self, |
| 301 _messageDispatcher: safari.self.tab, | 317 _messageDispatcher: safari.self.tab, |
| 302 | 318 |
| 303 sendMessage: sendMessage, | 319 sendMessage: sendMessage, |
| 304 getWindow: function() { return proxy.getObject(0); } | 320 getWindow: function() { return proxy.getObject(0); } |
| 305 }; | 321 }; |
| 306 | 322 |
| 307 ext.onMessage = new MessageEventTarget(safari.self); | 323 ext.onMessage = new MessageEventTarget(safari.self); |
| 308 })(); | 324 })(); |
| OLD | NEW |