text: function onUnload(e,n){var t=function(){n.removeEventListener("unload",t,!1),e()};n.addEventListener("unload",t,!1)}function initStorage(e,n){var t="KangoStorage",o=gStorageData,s=function(n,s){delete o[n],s||e.dispatchMessage([t,"removeItem"].join("_"),{name:n})},i=function(n){o=gStorageData={},n||e.dispatchMessage([t,"clear"].join("_"))},r=function(n,i,r){"undefined"!=typeof i?"function"!=typeof i&&(o[n]=i,r||e.dispatchMessage([t,"setItem"].join("_"),{name:n,value:i})):s(n)},a={setItem:function(e){return r(e.name,e.value)},removeItem:function(e){return s(e.name)},clear:function(){return i()}};e.addEventListener("message",function(e){var n=e.data,o=e.name.split("_");if(o[0]==t)for(var s in a)if(a.hasOwnProperty(s)&&s==o[1]){a[s](n,e.source);break}}),e.storage={setItem:function(e,n,t){return r(e,n,t)},getItem:function(e){return"undefined"!=typeof o[e]?n(o[e]):null},removeItem:function(e,n){return s(e,n)},getKeys:function(){var e=[];for(var t in o)o.hasOwnProperty(t)&&e.push(t);return n(e)},clear:function(e){return i(e)}}}function initI18n(e,n){var t=gLocaleMessages,o=gLocale;e.i18n={getMessages:function(){return n(t)},getMessage:function(e){var n=t[e]?t[e]:e;return arguments.length>1?string.format.apply(string,[n].concat(Array.prototype.slice.call(arguments,1))):n},getCurrentLocale:function(){return o}}}function ContentApiProxy(e){function n(n){return n&&object.isObject(n)?gContentSandbox.exposeObject(n,"r",e):n}var t=null;this.getExtensionInfo=function(){return n(extensionInfo.getRawData())},this.console={log:function(e){console.log(e)},warn:function(e){console.warn(e)},error:function(e){console.error(e)}},this.browser={getName:function(){return"firefox"}},this.io={getResourceUrl:getResourceUrl},this.tab={isPrivate:function(){return docShell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing}},this.dispatchMessage=func.bind(messageRouter.dispatchMessage,messageRouter),this.addEventListener=function(e,o){return messageRouter.addEventListener(e,function(e){e.source=e.target=null,e=n(e),e.source=e.target=t,o(e)})},this.fireEvent=func.bind(messageRouter.fireEvent,messageRouter);var o=new MessageTargetModule(func.bind(this.addEventListener,this)),s=new InvokeAsyncModule(func.bind(this.addEventListener,this),func.bind(this.dispatchMessage,this),null,func.bind(console.log,console));return this.xhr={send:function(e,t){var o=e.contentType;("xml"==o||"json"==o)&&(e.contentType="text"),e.sanitizeData=!0,s.invokeAsyncCallback("kango.xhr.send",e,function(s){var i={status:s.status,response:null};if(""!=s.response&&null!=s.response)if("json"==o)try{i.response=JSON.parse(s.response)}catch(r){}else if("xml"==o)try{var a=new DOMParser;i.response=a.parseFromString(s.response,"text/xml")}catch(r){}else i.response=s.response;e.contentType=o,t(n(i))})}},this.invokeAsync=func.bind(s.invokeAsync,s),this.invokeAsyncCallback=func.bind(s.invokeAsyncCallback,s),this.addMessageListener=func.bind(o.addMessageListener,o),this.removeMessageListener=func.bind(o.removeMessageListener,o),initStorage(this,n),initI18n(this,n),t=gContentSandbox.exposeObject(this,"r",e),Cu.exportFunction(func.bind(this.xhr.send,t.xhr),t.xhr,{defineAs:"send",allowCallbacks:!0}),Cu.exportFunction(func.bind(this.invokeAsyncCallback,t),t,{defineAs:"invokeAsyncCallback",allowCallbacks:!0}),Cu.exportFunction(func.bind(this.invokeAsync,t),t,{defineAs:"invokeAsync",allowCallbacks:!0}),Cu.exportFunction(func.bind(this.addMessageListener,t),t,{defineAs:"addMessageListener",allowCallbacks:!0}),Cu.exportFunction(func.bind(this.removeMessageListener,t),t,{defineAs:"removeMessageListener",allowCallbacks:!0}),Cu.exportFunction(func.bind(this.storage.setItem,t.storage),t.storage,{defineAs:"setItem",allowCallbacks:!0}),this.wrappedObject=t,this}function ContentSandbox(){this._proxies=[]}function initStorageData(e){"undefined"==typeof gStorageData?gInvokeAsyncModule.invokeAsync("modules/kango/storage/storage.getItems",function(n){gStorageData=n,e()}):e()}function initI18nData(e){"undefined"==typeof gLocale?gInvokeAsyncModule.invokeAsync("modules/kango/i18n/getCurrentLocale",function(n){gLocale=n,gInvokeAsyncModule.invokeAsync("modules/kango/i18n/getMessages",function(n){gLocaleMessages=n,e()})}):e()}function executeContentScripts(e,n){initModules();var t=e==e.top;initStorageData(function(){initI18nData(function(){gInvokeAsyncModule.invokeAsync("modules/kango/userscript_engine/getScripts",e.document.URL,n,t,function(n){object.forEach(n,function(n,t){gContentSandbox.evalScriptsInSandbox(e,n,t)})})})})}function initModules(){gContentSandbox||(gContentSandbox=new ContentSandbox,gInvokeAsyncModule=new InvokeAsyncModule(func.bind(messageRouter.addEventListener,messageRouter),func.bind(messageRouter.dispatchMessage,messageRouter),null,func.bind(console.log,console)))}function DocumentObserver(){this._documentLoadListener=func.bind(this._onPageLoad,this)}var utils=require("kango/utils"),console=require("kango/console"),messageRouter=require("includes/content_messaging"),func=utils.func,object=utils.object,array=utils.array,string=utils.string,extensionInfo=require("kango/extension_info"),EventTarget=utils.EventTarget,InvokeAsyncModule=require("kango/invoke_async"),MessageTargetModule=require("kango/message_target");Cu["import"]("resource://gre/modules/XPCOMUtils.jsm");var gContentSandbox,gInvokeAsyncModule,gStorageData,gLocale,gLocaleMessages;ContentSandbox.prototype={_executeScript:function(e,n){try{e.path?Services.scriptloader.loadSubScript(e.path,n,"UTF-8"):Cu.evalInSandbox(e.text,n,"default",e.path,1)}catch(t){console.reportError(t,e.path)}},exposeObject:function(e,n,t){if(Services.vc.compare(Services.appinfo.platformVersion,"34")>=0)return Cu.cloneInto(e,t,{cloneFunctions:!0});n=n||"r",e.__exposedProps__=e.__exposedProps__||{};for(var o in e)"__exposedProps__"!=o&&e.hasOwnProperty(o)&&(e.__exposedProps__[o]=n,e[o]&&object.isObject(e[o])&&this.exposeObject(e[o],n,t));return e},evalScriptsInSandbox:function(e,n,t){var o=this.getSandboxForWindow(e,t);n.forEach(function(e){e.requires.forEach(function(e){this._executeScript(e,o)},this),this._executeScript(e,o)},this)},deleteTabProxy:function(e,n){for(var t=0;t<this._proxies.length;t++)if(this._proxies[t].window==e&&this._proxies[t].namespace==n)return this._proxies.splice(t,1),!0;return!1},getSandboxForWindow:function(e,n){for(var t=0;t<this._proxies.length;t++)if(this._proxies[t].window==e&&this._proxies[t].namespace==n)return this._proxies[t].sandbox;var o=new Cu.Sandbox(e,{sandboxPrototype:e,wantXrays:!0}),s=new ContentApiProxy(o);return o.kango=s.wrappedObject,this._proxies.push({window:e,proxy:s,sandbox:o,namespace:n}),onUnload(func.bind(function(){this.deleteTabProxy(e,n)},this),e),o}},DocumentObserver.prototype={observe:function(e,n,t){if("document-element-inserted"==n){var o=e;if(o&&o.defaultView){var s=o.defaultView;s&&s==content&&this._isScriptableUrl(s.document.URL)&&(s.addEventListener("DOMContentLoaded",this._documentLoadListener,!0),s.addEventListener("load",this._documentLoadListener,!0),executeContentScripts(s,"document-start"))}}},_isScriptableUrl:function(e){return 0==e.indexOf("http")||0==e.indexOf("file")},_onPageLoad:function(e){var n=e.target,t=n.defaultView;t.removeEventListener("load",this._documentLoadListener,!0),t.removeEventListener("DOMContentLoaded",this._documentLoadListener,!0),n instanceof t.HTMLDocument&&(t==t.top&&messageRouter.dispatchMessage("DocumentComplete"),executeContentScripts(t,"document-end"))}},function(){initModules();var e=new DocumentObserver;Services.obs.addObserver(e,"document-element-inserted",!1),module.exports.dispose=function(){Services.obs.removeObserver(e,"document-element-inserted")}}();