Index: safari/content.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/safari/content.js |
@@ -0,0 +1,254 @@ |
+(function() { |
+ /* Background page proxy */ |
+ |
+ var proxy = { |
+ objects: [], |
+ callbacks: [], |
+ |
+ send: function(message) { |
+ var evt = document.createEvent("Event"); |
+ evt.initEvent("beforeload"); |
+ return safari.self.tab.canLoad(evt, {type: "proxy", payload: message}); |
+ }, |
+ checkResult: function(result) { |
+ if (!result.succeed) |
+ throw result.error; |
+ }, |
+ deserializeResult: function(result) { |
+ this.checkResult(result); |
+ return this.deserialize(result.result); |
+ }, |
+ serialize: function(obj, memo) { |
+ var objectId = this.objects.indexOf(obj); |
+ if (objectId != -1) |
+ return {type: "hosted", objectId: objectId}; |
+ |
+ if (typeof obj == "function") { |
+ var callbackId = this.callbacks.indexOf(obj); |
+ |
+ if (callbackId == -1) { |
+ callbackId = this.callbacks.push(obj) - 1; |
+ |
+ safari.self.addEventListener("message", function(event) { |
+ if (event.name == "proxyCallback") |
+ if (event.message.callbackId == callbackId) |
+ obj.apply( |
+ this.getObject(event.message.contextId), |
+ this.deserializeSequence(event.message.args) |
+ ); |
+ }.bind(this)); |
+ } |
+ |
+ return {type: "callback", callbackId: callbackId}; |
+ } |
+ |
+ if (typeof obj == "object") |
+ if (obj != null) |
+ if (obj.constructor != Date) |
+ if (obj.constructor != RegExp) { |
+ if (!memo) |
+ memo = {specs: [], objects: []}; |
+ |
+ var idx = memo.objects.indexOf(obj); |
+ if (idx != -1) |
+ return memo.specs[idx]; |
+ |
+ var spec = {}; |
+ memo.specs.push(spec); |
+ memo.objects.push(obj); |
+ |
+ if (obj.constructor == Array) { |
+ spec.type = "array"; |
+ spec.items = []; |
+ |
+ for (var i = 0; i < obj.length; i++) |
+ spec.items.push(this.serialize(obj[i], memo)); |
+ } else { |
+ spec.type = "object"; |
+ spec.properties = {}; |
+ |
+ for (var k in obj) |
+ spec.properties[k] = this.serialize(obj[k], memo); |
+ } |
+ |
+ return spec; |
+ } |
+ |
+ return {type: "value", value: obj}; |
+ }, |
+ deserializeSequence: function(specs, array, memo) { |
+ if (!array) |
+ array = []; |
+ |
+ if (!memo) |
+ memo = {specs: [], arrays: []}; |
+ |
+ for (var i = 0; i < specs.length; i++) |
+ array.push(this.deserialize(specs[i], memo)); |
+ |
+ return array; |
+ }, |
+ deserialize: function(spec, memo) { |
+ switch (spec.type) { |
+ case "value": |
+ return spec.value; |
+ case "object": |
+ case "function": |
+ return this.getObject(spec.objectId, spec.type); |
+ case "array": |
+ if (!memo) |
+ memo = {specs: [], arrays: []}; |
+ |
+ var idx = memo.specs.indexOf(spec); |
+ if (idx != -1) |
+ return memo.arrays[idx]; |
+ |
+ var array = []; |
+ memo.specs.push(spec); |
+ memo.arrays.push(array); |
+ |
+ return this.deserializeSequence(spec.items, array, memo); |
+ } |
+ }, |
+ getProperty: function(objectId, property) { |
+ return this.deserializeResult( |
+ this.send({ |
+ type: "getProperty", |
+ objectId: objectId, |
+ property: property |
+ }) |
+ ); |
+ }, |
+ createProperty: function(objectId, property, enumerable) { |
+ return { |
+ get: function() { |
+ return this.getProperty(objectId, property); |
+ }.bind(this), |
+ set: function(value) { |
+ this.checkResult( |
+ this.send({ |
+ type: "setProperty", |
+ objectId: objectId, |
+ property: property, |
+ value: this.serialize(value) |
+ }) |
+ ) |
+ }.bind(this), |
+ enumerable: enumerable |
+ }; |
+ }, |
+ createObject: function(objectId) { |
+ var objectInfo = this.send({ |
+ type: "inspectObject", |
+ objectId: objectId |
+ }); |
+ |
+ var prototype; |
+ if (objectInfo.prototypeId != null) |
+ prototype = this.getObject(objectInfo.prototypeId); |
+ else |
+ prototype = Object.prototype; |
+ |
+ var properties = {}; |
+ for (var property in objectInfo.properties) |
+ properties[property] = this.createProperty( |
+ objectId, property, |
+ objectInfo.properties[property].enumerable |
+ ); |
+ |
+ return Object.create(prototype, properties); |
+ }, |
+ createFunction: function(objectId) { |
+ var objectInfo = this.send({ |
+ type: "inspectObject", |
+ objectId: objectId |
+ }); |
+ |
+ var proxy = this; |
+ var func = function() { |
+ return proxy.deserializeResult( |
+ proxy.send({ |
+ type: "callFunction", |
+ functionId: objectId, |
+ contextId: proxy.objects.indexOf(this), |
+ args: Array.prototype.map.call( |
+ arguments, |
+ proxy.serialize.bind(proxy) |
+ ) |
+ }) |
+ ); |
+ }; |
+ |
+ var builtin = Object.getOwnPropertyNames(func); |
+ for (var property in objectInfo.properties) |
+ if (builtin.indexOf(property) == -1) |
+ Object.defineProperty(func, property, this.createProperty( |
+ objectId, property, |
+ objectInfo.properties[property].enumerable |
+ )); |
+ |
+ func.prototype = this.getProperty(objectId, "prototype"); |
+ return func; |
+ }, |
+ getObject: function(objectId, type) { |
+ var obj = this.objects[objectId]; |
+ |
+ if (!obj) { |
+ if (type == "function") |
+ obj = this.createFunction(objectId); |
+ else |
+ obj = this.createObject(objectId); |
+ |
+ this.objects[objectId] = obj; |
+ } |
+ |
+ return obj; |
+ } |
+ }; |
+ |
+ |
+ /* Web request blocking */ |
+ |
+ document.addEventListener("beforeload", function(event) { |
+ var type; |
+ |
+ switch(event.target.nodeName) { |
+ case "FRAME": |
+ case "IFRAME": |
+ type = "frame"; |
+ break; |
+ case "IMG": |
+ type = "image"; |
+ break; |
+ case "OBJECT": |
+ case "EMBED": |
+ type = "object"; |
+ break; |
+ case "SCRIPT": |
+ type = "script"; |
+ break; |
+ case "LINK": |
+ if (/(^|\s)stylesheet($|\s)/i.test(event.target.rel)) { |
+ type = "stylesheet"; |
+ break; |
+ } |
+ default: |
+ type = "other"; |
+ } |
+ |
+ if (!safari.self.tab.canLoad(event, {type: "webRequest", payload: {url: event.url, type: type}})) |
+ event.preventDefault(); |
+ }, true); |
+ |
+ /* API */ |
+ |
+ ext.backgroundPage = { |
+ _eventTarget: safari.self, |
+ _messageDispatcher: safari.self.tab, |
+ |
+ sendMessage: sendMessage, |
+ getWindow: function() { return proxy.getObject(0); } |
+ }; |
+ |
+ ext.onMessage = new MessageEventTarget(safari.self); |
+})(); |