(function(){if(void 0!==rea.globals._content)console.warn('content: Stop here, cause a second Tampermonkey instance was detected!\nThis can be caused by using "document.write" at Userscripts.\nSee https://code.google.com/p/chromium/issues/detail?id=253388 for more information');else{var p={_content:!0,Converter:null,Eventing:null,D:!1,CV:!1,USV:!1};(function(){for(var b in p)p.hasOwnProperty(b)&&(rea.globals[b]=p[b])})();var B=function(){var b={},c={webkitStorageInfo:!0,JSHINT:!0,webkitIDBTransaction:!0,
webkitIDBRequest:!0,webkitIDBObjectStore:!0,webkitIDBKeyRange:!0,webkitIDBIndex:!0,webkitIDBFactory:!0,webkitIDBDatabase:!0,webkitIDBCursor:!0,webkitIndexedDB:!0,webkitOfflineAudioContext:!0,webkitAudioContext:!0,webkitURL:!0},a,d,f=Object.getOwnPropertyNames(window),e;a=window;for(d=[];(a=Object.getPrototypeOf(a))&&a!==Object.prototype;)d=d.concat(Object.getOwnPropertyNames(a));e=d;for(a=0;d=e[a];a++)c[d]||p[d]||(2<d.length&&"on"===d.substr(0,2)?b[d]={proto:!0,event:!0}:b[d]={proto:!0});for(a=0;d=
f[a];a++)c[d]||p[d]||b[d]||(b[d]={window:!0});return b};Registry.require("xmlhttprequest");Registry.require("convert");Registry.require("helper");var k=Registry.get("helper"),r=k.clean;Converter=Registry.get("convert");var C={scriptBlocker:!1,proxy:!1},s=null,q=!1,t=!1,l=function(){var b,c;return{onDOMContentLoaded:function(){D&&console.log("content: detected DOMContentLoaded "+Eventing.contextId);c=!0;q&&g.sendMessage("if (typeof domContentLoadedListener !== 'undefined') domContentLoadedListener();");
window.removeEventListener("DOMContentLoaded",l.onDOMContentLoaded,!1)},onLoad:function(){D&&console.log("content: detected load "+Eventing.contextId);b=c=!0;q&&g.sendMessage("if (typeof loadListener !== 'undefined') loadListener();");window.removeEventListener("DOMContentLoaded",l.onDOMContentLoaded,!1);window.removeEventListener("load",l.onLoad,!1)},seen:function(){var a={};a.__defineGetter__("load",function(){return b});a.__defineGetter__("DOMContentLoaded",function(){return c});return a}()}}(),
g=function(){var b={},c=k.createUUID(),a=k.createUUID();return{backupProps:function(){var d=["var backup = {safeWindow: {},safeDocument: {},String: String,Array: Array,Uint8Array: Uint8Array,JSON: JSON,Math: Math,Event: Event,MutationEvent: MutationEvent,console: console,location: location,eval: eval,createSafeDocument: ",function(){["createEvent","createElement","dispatchEvent","addEventListener","removeEventListener"].forEach(function(a){var d=document[a];backup.safeDocument[a]=function(){return d.apply(document,
arguments)}})}.toString(),"};(",function(){"postMessage addEventListener removeEventListener setTimeout setInterval clearTimeout clearInterval alert prompt confirm encodeURIComponent decodeURIComponent escape unescape atob btoa".split(" ").forEach(function(a){var d=window[a];backup.safeWindow[a]=function(){return d.apply(window,arguments)}})}.toString(),")();(",function(){["location"].forEach(function(a){backup.safeWindow[a]=window[a]})}.toString(),")();var comm_uuid = '",a,"';try {backup.createSafeDocument();Object.defineProperties(window, {'",
c,"': {enumerable: false,writable: false,configurable: true,value: function(a) { if (a === comm_uuid) return backup; }}});if (Object.freeze) Object.freeze(window['",c,"']);} catch (e) {}"].join(""),b=document.createElement("script");b.textContent=["(function TM_back() {",d,"})();"].join("");(document.head||document.body||document.documentElement||document).appendChild(b);b.parentNode.removeChild(b)},initEnv:function(d,b){var e=["var Context = (function() {var self = {};with (self) {var copy = function(src) {var props = Object.getOwnPropertyNames(src);for (var ii=0, kk=null; (kk=props[ii]) !== undefined; ii++) {self[kk] = src[kk];};};copy(window['",
c,"']('",a,"'));copy({Context: self,exec: function(s) { (new Function(['with (this) { (function() {', s, '})();}'].join(''))).call(self); },V:false,ENV:false,TS:false,D:",D?"true":"false",",Converter:",k.serialize(Converter),",use:",JSON.stringify(C),",windowProps:",JSON.stringify(B()),",isIncognito:",d.inIncognitoContext,",downloadMode: '",d.downloadMode,"',enforce_strict_mode:",d.enforce_strict_mode,",use_strong_mode:",d.use_strong_mode,",version: '",d.version,"',scripts:",JSON.stringify(b),",_content: false,Eventing: (",
k.serialize(x),")(),short_id: '",rea.runtime.short_id,"',write_listeners: []});","Eventing.init('"+Eventing.contextId+"', self);","var cleanup = function(evt) {Eventing.cleanup();safeWindow.removeEventListener('unload', cleanup, false);};safeWindow.addEventListener('unload', cleanup, false);Context.write_listeners.push(function() {Context.createSafeDocument();Eventing.registerListener();});",function(){var a;a=""+("var logLevel = "+s+";\n");var d="";try{d="("+Registry.getRaw("environment.js")+")();\n"}catch(b){}var h=
"Context.chromeEmu = ("+k.serialize(F)+")();\n",c="Context.tmCE = ("+k.serialize(y)+")();\nvar Event = function() {};\n",f="var TM_context_id = '"+Eventing.contextId+"';\n",e="";g.sendMessage("console.log('Tampermonkey started');");if(l.seen.load||"complete"==document.readyState)e+="Context.pageLoaded = true;\nContext.domContentLoaded = true;\n",D&&console.log("content: Start ENV with page loaded "+Eventing.contextId);else if(l.seen.DOMContentLoaded||"interactive"==document.readyState)e+="Context.domContentLoaded = true;\n",
D&&console.log("content: Start ENV with DOMContentLoaded "+Eventing.contextId);return"(function () { "+a+f+c+h+e+d+"})();"}(),"};return self;})();"].join(""),h=document.createElement("script");h.textContent=["(function TM_mother() {",e,"})();"].join("");(document.head||document.body||document.documentElement||document).appendChild(h);h.parentNode.removeChild(h)},sendMessage:function(a){Eventing.fireEvent(a)},getResponseId:function(a){var c=0;if(a){for(;0==c||void 0!=b[c];)c=k.createUUID();b[c]=a}return c},
runResponse:function(a,c){for(var e in b)if(e==a){var h="";try{if(h=JSON.parse(Converter.decode(c)),b[e])b[e](h);else console.log("Warn: content: responseId "+e+" is undefined!!!")}catch(E){console.log("content: Json parse error (runResponse):"+E+"\n"+c)}b[e]=null;return}console.log("WARN: responseId "+a+" not found!")}}}(),F=function(){var b=Context.tmCE,c=function(a){return void 0===a||null===a?a:String(a)},a={},d=[],f=[],e={key:"##REPLACE_KEY##",clean:function(){a={};d=[];f=[]},runUpdateListeners:function(a,
d,b){for(var c=0;c<f.length;c++){var e=f[c];try{e(a,d,b)}catch(m){console.log("emu: Error (runUpdateListeners):"+m+"\n"+e.toString())}}},runRequestHandlers:function(a,c,f,u){d.forEach(function(d){try{d(a,c,function(a){b.onResponse(e.key,u,f,JSON.stringify(a))})}catch(m){console.log("emu: Error (runRequestHandlers):"+m+"\n"+d.toString())}})},runContentRequestHandlers:function(a,c,f,u){d.forEach(function(d){try{d(a,c,function(a){b.onContentResponse(e.key,u,f,JSON.stringify(a))})}catch(m){console.log("emu: Error (runContentRequestHandlers):"+
m+"\n"+d.toString())}})},runRequest:function(a,d,b){try{var c=JSON.parse(Converter.decode(d));e.runRequestHandlers(c.request,c.sender,a,b)}catch(f){console.log("emu: Json parse error (runRequest):"+f+"\n"+d)}},runContentRequest:function(a,d,b){try{var c=JSON.parse(Converter.decode(d));e.runContentRequestHandlers(c.request,c.sender,a,b)}catch(f){console.log("emu: Json parse error (runContentRequest):"+f+"\n"+d)}},runResponse:function(d,b){try{for(var c in a)if(c==d){var e="";try{e=JSON.parse(Converter.decode(b)),
a[c](e)}catch(f){console.log("emu: Json parse error (runResponse):"+f+"\n"+b)}delete a[c];return}}catch(m){console.log("emu: Json parse error (runResponse):"+m+"\n"+b)}console.log("WARN: emu: responseId "+d+" not found!")},runConnectResponse:function(d,b){try{for(var c in a)if(c==d){var e="",f=!1;try{e=JSON.parse(Converter.decode(b)),a[c](e),f=e.onDisconnect}catch(m){console.log("emu: Json parse error (runConnectResponse):"+m+"\n"+b)}f&&(a[c]=null);return}}catch(g){console.log("emu: Json parse error (runConnectResponse):"+
g+"\n"+b)}console.log("WARN: emu: responseId "+d+" not found!")},getResponseId:function(d){var b=0;if(d){for(;0==b||void 0!=a[b];)b=((new Date).getTime()+Math.floor(19831206*Math.random()+1)).toString();a[b]=d}return b},extension:{getURL:function(a){return c(b.getUrl(e.key,a))},connect:function(a){var d=[],c=[],f={notifyListeners:function(a,d){for(var b=0;b<a.length;b++)a[b](d)},postMessage:function(a){b.sendExtensionPortMessage(e.key,JSON.stringify(a),g)},onMessage:{addListener:function(a){d.push(a)}},
onDisconnect:{addListener:function(a){c.push(a)}},disconnect:function(){b.sendExtensionPortClose(e.key,g);f=c=d=null}},g=e.getResponseId(function(a){a.onMessage?f&&f.notifyListeners(d,a.msg):a.onDisconnect&&f&&(f.notifyListeners(c),f=null)});b.sendExtensionConnect(e.key,a,g);return f},sendMessage:function(a,d){var c=e.getResponseId(d);b.sendExtensionMessage(e.key,JSON.stringify(a),c)},onMessage:{addListener:function(a){d.push(a)}}},i18n:{getMessage:function(){for(var a=[],d=0;d<arguments.length;d++)a.push(arguments[d]);
a=b.getMessage(e.key,JSON.stringify(a));return c(a)}},tabs:{sendMessage:function(a,d,c){c=e.getResponseId(c);b.sendTabsRequest(e.key,a,JSON.stringify(d),c)},create:function(a){b.createTab(e.key,JSON.stringify(a))},query:function(a,d){var c=e.getResponseId(d);b.queryTab(e.key,JSON.stringify(a),c)},update:function(a,d){b.updateTab(e.key,a,d)},onUpdated:{addListener:function(a){f.push(a)}}}};return e},y=function(){var b={ports:{},getArgs:function(b){return b},log:function(b){_content?console.log("content: "+
b):Eventing.fireEvent({fn:"log",args:"page: "+b})},onContentResponse:function(c,a,d,f){_content?g.runResponse(d,Converter.encode(f)):Eventing.fireEvent({fn:"onContentResponse",args:b.getArgs(arguments)})},onResponse:function(c,a,d,f){if(_content)try{var e=Converter.encode(f);g.sendMessage("Context.chromeEmu.runResponse('"+d+"', \""+e+'")')}catch(h){console.log("Error: processing onResponse")}else Eventing.fireEvent({fn:"onResponse",args:b.getArgs(arguments)})},onConnectResponse:function(c,a,d,f){if(_content)try{var e=
Converter.encode(f);g.sendMessage("Context.chromeEmu.runConnectResponse('"+d+"', \""+e+'")')}catch(h){console.log("Error: processing onConnectResponse")}else Eventing.fireEvent({fn:"onConnectResponse",args:b.getArgs(arguments)})},onContentRequest:function(b,a,d){_content?b.id&&this.id&&b.id!=this.id||(b=Converter.encode(JSON.stringify({sender:a,request:b})),g.sendMessage("Context.chromeEmu.runContentRequest('"+d+"', \""+b+'", 0);')):console.log("Warn: onContentRequest from non BG not supported")},
onMessage:function(b,a,d){_content?b.id&&this.id&&b.id!=this.id||(b=Converter.encode(JSON.stringify({sender:a,request:b})),g.sendMessage("Context.chromeEmu.runRequest('"+d+"', \""+b+'", 0)')):console.log("Warn: onMessage from non BG not supported")},runUpdateListener:function(){console.log("WARN: not supported!")},getUrl:function(){console.log("WARN: not supported!")},sendExtensionMessage:function(c,a,d){if(_content){var f=JSON.parse(a);f.responseId=d;rea.extension.sendMessage(f,function(a){b.onResponse(c,
0,d,JSON.stringify(a))});f=null}else Eventing.fireEvent({fn:"sendExtensionMessage",args:b.getArgs(arguments)})},sendExtensionConnect:function(c,a,d){if(_content){var f=rea.extension.connect({name:a});f.onMessage.addListener(function(a){b.onConnectResponse(c,0,d,JSON.stringify({onMessage:!0,msg:a}))});f.onDisconnect.addListener(function(){b.onConnectResponse(c,0,d,JSON.stringify({onDisconnect:!0}));delete b.ports[d]});b.ports[d]=f}else Eventing.fireEvent({fn:"sendExtensionConnect",args:b.getArgs(arguments)})},
sendExtensionPortMessage:function(c,a,d){if(_content){var f=b.ports[d];if(f){var e=JSON.parse(a);e.responseId=d;f.postMessage(e)}else this.log("Error: sendExtensionPortMessage unable to find port "+d)}else Eventing.fireEvent({fn:"sendExtensionPortMessage",args:b.getArgs(arguments)})},sendExtensionPortClose:function(c,a){if(_content){var d=b.ports[a];d?(d.disconnect(),delete b.ports[a]):this.log("Error: sendExtensionPortClose unable to find port "+a)}else Eventing.fireEvent({fn:"sendExtensionPortClose",
args:b.getArgs(arguments)})},sendTabsRequest:function(){console.log("WARN: not supported!")},createTab:function(){console.log("WARN: not supported!")},queryTab:function(){console.log("WARN: not supported!")},updateTab:function(){console.log("WARN: not supported!")},onUpdated:function(){console.log("WARN: not supported!")},getMessage:function(){console.log("WARN: not supported!")},storageKey:function(){console.log("WARN: not supported!")},storageRemoveItem:function(){console.log("WARN: not supported!")},
storageSetItem:function(){console.log("WARN: not supported!")},storageGetItem:function(){console.log("WARN: not supported!")},storageLength:function(){console.log("WARN: not supported!")}};return b},n=new y,x=function(){var b=function(a){return Context.exec(a)},c={safeWindow:window,safeDocument:document},a={topframe:window.self===window.top,contextId:null,rEventId:null,sEventId:null,eventSource:null,log:function(a,b,c,h){console.log((_content?"content":"page")+":"+a,b,c,h)},postMessageEventSource:{executor:b,
element:{dispatchEvent:function(a){return c.safeWindow.postMessage.apply(document,[a,"*"])},addEventListener:function(a,b){return c.safeWindow.addEventListener("message",function(c){c.source==window&&c.data.id==a&&b(c)},!1)},removeEventListener:function(a,b){return c.safeWindow.removeEventListener("message",b)}},event:{encode:function(a,b){return{id:a,json:JSON.stringify(b)}},decode:function(a){return JSON.parse(a.data.json)}}},standardEventSource:{executor:b,element:{dispatchEvent:function(){return c.safeDocument.dispatchEvent.apply(document,
arguments)},addEventListener:function(){return c.safeDocument.addEventListener.apply(document,arguments)},removeEventListener:function(){return c.safeDocument.removeEventListener.apply(document,arguments)}},event:{encode:function(a,b){var e=c.safeDocument.createEvent("MutationEvent");e.initMutationEvent(a,!1,!1,null,null,null,Converter.encodeR(JSON.stringify(b)),e.ADDITION);return e},decode:function(a){return JSON.parse(Converter.decodeR(a.attrName))}}},init:function(b,f){c=f||c;a.contextId=b;a.contextId?
(_content?(a.rEventId="TM_toContent",a.sEventId="TM_toPage",a.eventHandler=a.eventHandlerContent):(a.rEventId="TM_toPage",a.sEventId="TM_toContent",a.eventHandler=a.eventHandlerPage),a.eventSource=a.standardEventSource,a.registerListener()):a.log("Eventing.init() failed!!!")},fireEvent:function(b,c){void 0==c&&(c=a.sEventId+a.contextId);try{var e=a.eventSource.event.encode(c,b);a.eventSource.element.dispatchEvent(e)}catch(h){a.log("Error: fire event",c,"->",b,h)}},registerListener:function(){a.eventSource.element.removeEventListener(a.rEventId+
a.contextId,a.eventHandler,!1);a.eventSource.element.addEventListener(a.rEventId+a.contextId,a.eventHandler,!1)},eventHandlerPage:function(b){try{var c=a.eventSource.event.decode(b);try{a.eventSource.executor(c)}catch(e){console.log("page: Error: processing event! ",e.message,{fn:c})}}catch(h){a.log("Error: retrieving event!",h.message),a.log(b)}},eventHandlerContent:function(b){try{var c=a.eventSource.event.decode(b);try{for(var e=[],h=0,g;void 0!==(g=c.args[h]);h++)e.push(g);n[c.fn].apply(this,
e)}catch(k){a.log("Error: processing event!",k.message,c)}}catch(l){a.log("Error: retrieving event!",l.message),a.log(b)}},cleanup:function(){a.eventSource&&a.eventSource.element.removeEventListener(a.rEventId+a.contextId,a.eventHandler,!1)}};return a};Eventing=new x;var v=function(b){rea.extension.sendMessage({method:"unLoad",id:Eventing.contextId,topframe:Eventing.topframe,forced:!!b,url:window.location.href},function(){});try{Eventing.cleanup()}catch(c){console.log("cleanup: Error: "+c.message)}window.removeEventListener("DOMContentLoaded",
l.onDOMContentLoaded,!1);window.removeEventListener("load",l.onLoad,!1);window.removeEventListener("unload",v,!1);null!=r?(r.finalize(),r=null):console.log("content: Warning: multiple unload events detected!!!")},z=function(b,c,a){if(!q)return window.setTimeout(function(){z(b,c,a)},10),!0;if(t){var d=g.getResponseId(a);n.onContentRequest(b,c,d)}else window.self===window.top&&("getSrc"==b.method?a({src:k.getSource(document)}):"reload"==b.method?window.location.reload():"confirm"==b.method?window.setTimeout(function(){var c=
confirm(b.msg);a({confirm:c})},100):"showMsg"==b.method?window.setTimeout(function(){window.setTimeout(function(){alert(b.msg)},1);a({})},100):"setForeignAttr"==b.method&&(window[b.attr]=b.value,a({})));return!0};window.addEventListener("DOMContentLoaded",l.onDOMContentLoaded,!1);window.addEventListener("load",l.onLoad,!1);var w=1,A=function(){g.backupProps();n.id=k.createUUID();window.addEventListener("unload",v,!1);rea.extension.onMessage.addListener(z);rea.extension.sendMessage({method:"prepare",
id:n.id,raw:[],topframe:Eventing.topframe,url:window.location.href},function(b){if(void 0===b)D&&console.debug("content: _early_ execution, connection to bg failed -> retry!"),window.setTimeout(A,w),w*=2;else{s=b.logLevel;(D|=60<=s)&&console.log("content: Started ("+n.id+", "+window.location.origin+window.location.pathname+")");if(b.contexters||b.scripts&&b.scripts.length){D&&console.log("content: start event processing for "+n.id+" ("+b.scripts.length+" to run)");t=!0;Eventing.init(n.id);var c={};
["inIncognitoContext","downloadMode","enforce_strict_mode","use_strong_mode","version"].forEach(function(a){c[a]=b[a]});g.initEnv(c,b.scripts||[])}else D&&console.log("content: disable event processing for "+n.id),v(!0),t=!1;q=!0}})};rea.content.onReady(A)}})();
