(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 q={_content:!0,Converter:null,Eventing:null,D:!1,CV:!1,USV:!1};(function(){for(var b in q)q.hasOwnProperty(b)&&(rea.globals[b]=q[b])})();var C=function(){var b={},f={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=Object.getOwnPropertyNames(window),c;c=window;for(var d=[];c=Object.getPrototypeOf(c);)d=d.concat(Object.getOwnPropertyNames(c));c=d;for(var d=0,e=null;e=c[d];d++)f[e]||q[e]||(2<e.length&&"on"===e.substr(0,2)?b[e]={proto:!0,event:!0}:b[e]={proto:!0});for(d=0;e=a[d];d++)f[e]||
q[e]||b[e]||(b[e]={window:!0});return b};Registry.require("xmlhttprequest");Registry.require("convert");Registry.require("helper");var h=Registry.get("helper"),s=h.clean;Converter=Registry.get("convert");var E={scriptBlocker:!1,proxy:!1},t=null,r=!1,u=!1,m=function(){var b,f;return{onDOMContentLoaded:function(){D&&console.log("content: detected DOMContentLoaded "+Eventing.contextId);f=!0;r&&g.sendMessage("Context.domContentLoaded = true; if (typeof runAllLoadListeners !== 'undefined') runAllLoadListeners();")},
onLoad:function(){D&&console.log("content: detected load "+Eventing.contextId);b=!0;r&&g.sendMessage("Context.pageLoaded = true; if (typeof runAllLoadListeners !== 'undefined') runAllLoadListeners();")},seen:function(){var a={};a.__defineGetter__("load",function(){return b});a.__defineGetter__("DOMContentLoaded",function(){return f});return a}()}}(),g=function(){var b={},f=h.createUUID(),a=h.createUUID();return{backupProps:function(){var c=["var backup = {safeWindow: {},safeDocument: {},String: String,Array: Array,Uint8Array: Uint8Array,JSON: JSON,Math: Math,Event: Event,MutationEvent: MutationEvent,console: console,location: location,eval: eval};(",
function(){["createEvent","createElement","dispatchEvent","addEventListener","removeEventListener"].forEach(function(c){var a=document[c];backup.safeDocument[c]=function(){return a.apply(document,arguments)}})}.toString(),")();(",function(){"postMessage addEventListener removeEventListener setTimeout setInterval clearTimeout clearInterval alert prompt confirm encodeURIComponent decodeURIComponent escape unescape atob btoa".split(" ").forEach(function(c){var a=window[c];backup.safeWindow[c]=function(){return a.apply(window,
arguments)}})}.toString(),")();(",function(){["location"].forEach(function(c){backup.safeWindow[c]=window[c]})}.toString(),")();var comm_uuid = '",a,"';try {Object.defineProperties(window, {'",f,"': {enumerable: false,writable: false,configurable: false,value: function(a) { if (a === comm_uuid) return backup; }}});if (Object.freeze) Object.freeze(window['",f,"']);} catch (e) {}"].join(""),d=document.createElement("script");d.textContent=["(function TM_back() {",c,"})();"].join("");(document.head||
document.body||document.documentElement||document).appendChild(d);d.parentNode.removeChild(d)},initEnv:function(c,d){var b=["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['",f,"']('",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:",h.serialize(Converter),",use:",JSON.stringify(E),",windowProps:",JSON.stringify(C()),",isIncognito:",c.inIncognitoContext,",downloadMode: '",c.downloadMode,"',scripts:",JSON.stringify(d),",_content: false,Eventing: (",h.serialize(y),")(),short_id: '",rea.runtime.short_id,"'});","Eventing.init('"+Eventing.contextId+"', self);","var cleanup = function(evt) {Eventing.cleanup();safeWindow.removeEventListener('unload', cleanup, false);};safeWindow.addEventListener.apply('unload', cleanup, false);",
function(){var c;c=""+("var logLevel = "+t+";\n");var a="";try{a="("+Registry.getRaw("environment.js")+")();\n"}catch(b){}var d="Context.chromeEmu = ("+h.serialize(F)+")();\n",k="Context.tmCE = ("+h.serialize(z)+")();\nvar Event = function() {};\n",e="var TM_context_id = '"+Eventing.contextId+"';\n",f="";g.sendMessage("console.log('Tampermonkey started');");if(m.seen.load||"complete"==document.readyState)f+="Context.pageLoaded = true;\nContext.domContentLoaded = true;\n",D&&console.log("content: Start ENV with page loaded "+
Eventing.contextId);else if(m.seen.DOMContentLoaded||"interactive"==document.readyState)f+="Context.domContentLoaded = true;\n",D&&console.log("content: Start ENV with DOMContentLoaded "+Eventing.contextId);return"(function () { "+c+e+k+d+f+a+"})();"}(),"};return self;})();"].join(""),k=document.createElement("script");k.textContent=["(function TM_mother() {",b,"})();"].join("");(document.head||document.body||document.documentElement||document).appendChild(k);k.parentNode.removeChild(k)},sendMessage:function(c){Eventing.fireEvent(c)},
getResponseId:function(c){var a=0;if(c){for(;0==a||void 0!=b[a];)a=h.createUUID();b[a]=c}return a},runResponse:function(a,d){for(var e in b)if(e==a){var k="";try{if(k=JSON.parse(Converter.decode(d)),b[e])b[e](k);else console.log("Warn: content: responseId "+e+" is undefined!!!")}catch(f){console.log("content: Json parse error (runResponse):"+f+"\n"+d)}b[e]=null;return}console.log("WARN: responseId "+a+" not found!")}}}(),F=function(){var b=Context.tmCE,f=function(a){return void 0===a||null===a?a:
String(a)},a={},c=[],d=[],e={key:"##REPLACE_KEY##",clean:function(){a={};c=[];d=[]},runUpdateListeners:function(a,c,b){for(var e=0;e<d.length;e++){var f=d[e];try{f(a,c,b)}catch(l){console.log("emu: Error (runUpdateListeners):"+l+"\n"+f.toString())}}},runRequestHandlers:function(a,d,f,v){for(var p=0;p<c.length;p++){var l=c[p];try{l(a,d,function(a){b.onResponse(e.key,v,f,JSON.stringify(a))})}catch(g){console.log("emu: Error (runRequestHandlers):"+g+"\n"+l.toString())}}},runContentRequestHandlers:function(a,
d,f,v){for(var p=0;p<c.length;p++){var l=c[p];try{l(a,d,function(a){b.onContentResponse(e.key,v,f,JSON.stringify(a))})}catch(g){console.log("emu: Error (runContentRequestHandlers):"+g+"\n"+l.toString())}}},runRequest:function(a,c,b){try{var d=JSON.parse(Converter.decode(c));e.runRequestHandlers(d.request,d.sender,a,b)}catch(f){console.log("emu: Json parse error (runRequest):"+f+"\n"+c)}},runContentRequest:function(a,c,b){try{var d=JSON.parse(Converter.decode(c));e.runContentRequestHandlers(d.request,
d.sender,a,b)}catch(f){console.log("emu: Json parse error (runContentRequest):"+f+"\n"+c)}},runResponse:function(c,b){try{for(var d in a)if(d==c){var e="";try{e=JSON.parse(Converter.decode(b)),a[d](e)}catch(f){console.log("emu: Json parse error (runResponse):"+f+"\n"+b)}delete a[d];return}}catch(l){console.log("emu: Json parse error (runResponse):"+l+"\n"+b)}console.log("WARN: emu: responseId "+c+" not found!")},runConnectResponse:function(c,b){try{for(var d in a)if(d==c){var e="",f=!1;try{e=JSON.parse(Converter.decode(b)),
a[d](e),f=e.onDisconnect}catch(l){console.log("emu: Json parse error (runConnectResponse):"+l+"\n"+b)}f&&(a[d]=null);return}}catch(g){console.log("emu: Json parse error (runConnectResponse):"+g+"\n"+b)}console.log("WARN: emu: responseId "+c+" not found!")},getResponseId:function(c){var b=0;if(c){for(;0==b||void 0!=a[b];)b=((new Date).getTime()+Math.floor(19831206*Math.random()+1)).toString();a[b]=c}return b},extension:{getURL:function(a){return f(b.getUrl(e.key,a))},connect:function(a){var c=[],d=
[],f={notifyListeners:function(a,c){for(var b=0;b<a.length;b++)a[b](c)},postMessage:function(a){b.sendExtensionPortMessage(e.key,JSON.stringify(a),g)},onMessage:{addListener:function(a){c.push(a)}},onDisconnect:{addListener:function(a){d.push(a)}},disconnect:function(){f=d=c=null}},g=e.getResponseId(function(a){a.onMessage?f&&f.notifyListeners(c,a.msg):a.onDisconnect&&f&&(f.notifyListeners(d),f=null)});b.sendExtensionConnect(e.key,JSON.stringify(a),g);return f},sendMessage:function(a,c){var d=e.getResponseId(c);
b.sendExtensionMessage(e.key,JSON.stringify(a),d)},onMessage:{addListener:function(a){c.push(a)}}},i18n:{getMessage:function(){for(var a=[],c=0;c<arguments.length;c++)a.push(arguments[c]);a=b.getMessage(e.key,JSON.stringify(a));return f(a)}},tabs:{sendMessage:function(a,c,d){d=e.getResponseId(d);b.sendTabsRequest(e.key,a,JSON.stringify(c),d)},create:function(a){b.createTab(e.key,JSON.stringify(a))},query:function(a,c){var d=e.getResponseId(c);b.queryTab(e.key,JSON.stringify(a),d)},update:function(a,
c){b.updateTab(e.key,a,c)},onUpdated:{addListener:function(a){d.push(a)}}}};return e},z=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(f,a,c,d){_content?g.runResponse(c,Converter.encode(d)):Eventing.fireEvent({fn:"onContentResponse",args:b.getArgs(arguments)})},onResponse:function(f,a,c,d){if(_content)try{var e=Converter.encode(d);g.sendMessage("Context.chromeEmu.runResponse('"+
c+"', \""+e+'")')}catch(k){console.log("Error: processing onResponse")}else Eventing.fireEvent({fn:"onResponse",args:b.getArgs(arguments)})},onConnectResponse:function(f,a,c,d){if(_content)try{var e=Converter.encode(d);g.sendMessage("Context.chromeEmu.runConnectResponse('"+c+"', \""+e+'")')}catch(k){console.log("Error: processing onConnectResponse")}else Eventing.fireEvent({fn:"onConnectResponse",args:b.getArgs(arguments)})},onContentRequest:function(b,a,c){_content?b.id&&this.id&&b.id!=this.id||
(b=Converter.encode(JSON.stringify({sender:a,request:b})),g.sendMessage("Context.chromeEmu.runContentRequest('"+c+"', \""+b+'", 0);')):console.log("Warn: onContentRequest from non BG not supported")},onMessage:function(b,a,c){_content?b.id&&this.id&&b.id!=this.id||(b=Converter.encode(JSON.stringify({sender:a,request:b})),g.sendMessage("Context.chromeEmu.runRequest('"+c+"', \""+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(f,a,c){if(_content){var d=JSON.parse(a);d.responseId=c;rea.extension.sendMessage(d,function(a){b.onResponse(f,0,c,JSON.stringify(a))});d=null}else Eventing.fireEvent({fn:"sendExtensionMessage",args:b.getArgs(arguments)})},sendExtensionConnect:function(f,a,c){if(_content){var d=JSON.parse(a);d.responseId=c;var e=rea.extension.connect({name:d});e.onMessage.addListener(function(a){b.onConnectResponse(f,0,c,JSON.stringify({onMessage:!0,
msg:a}))});e.onDisconnect.addListener(function(a){b.onConnectResponse(f,0,c,JSON.stringify({onDisconnect:!0}));d=null});b.ports[c]=e}else Eventing.fireEvent({fn:"sendExtensionConnect",args:b.getArgs(arguments)})},sendExtensionPortMessage:function(f,a,c){if(_content){var d=b.ports[c];if(d){var e=JSON.parse(a);e.responseId=c;d.postMessage(e)}else this.log("Error: sendExtensionPortMessage unable to find port "+c)}else Eventing.fireEvent({fn:"sendExtensionPortMessage",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 z,y=function(){var b=function(a){return Context.exec(a)},f={safeWindow:window,safeDocument:document},a={topframe:window.self===window.top,contextId:null,rEventId:null,sEventId:null,eventSource:null,log:function(a,b,e,f){console.log((_content?"content":"page")+":"+a,b,e,f)},postMessageEventSource:{executor:b,element:{dispatchEvent:function(a){return f.safeWindow.postMessage.apply(document,[a,"*"])},addEventListener:function(a,
b){return f.safeWindow.addEventListener("message",function(e){e.source==window&&e.data.id==a&&b(e)},!1)},removeEventListener:function(a,b){return f.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 f.safeDocument.dispatchEvent.apply(document,arguments)},addEventListener:function(){return f.safeDocument.addEventListener.apply(document,
arguments)},removeEventListener:function(){return f.safeDocument.removeEventListener.apply(document,arguments)}},event:{encode:function(a,b){var e=f.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,d){f=d||f;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,d){void 0==d&&(d=a.sEventId+a.contextId);try{var e=a.eventSource.event.encode(d,b);a.eventSource.element.dispatchEvent(e)}catch(f){a.log("Error: fire event",d,"->",b,f)}},registerListener:function(){a.eventSource.element.addEventListener(a.rEventId+a.contextId,a.eventHandler,!1)},eventHandlerPage:function(b){try{var d=
a.eventSource.event.decode(b);try{a.eventSource.executor(d)}catch(e){console.log("page: Error: processing event! ",e.message,{fn:d})}}catch(f){a.log("Error: retrieving event!",f.message),a.log(b)}},eventHandlerContent:function(b){try{var d=a.eventSource.event.decode(b);try{for(var e=[],f=0,g;void 0!==(g=d.args[f]);f++)e.push(g);n[d.fn].apply(this,e)}catch(h){a.log("Error: processing event!",h.message,d)}}catch(m){a.log("Error: retrieving event!",m.message),a.log(b)}},cleanup:function(){a.eventSource&&
a.eventSource.element.removeEventListener(a.rEventId+a.contextId,a.eventHandler,!1)}};return a};Eventing=new y;var w=function(b){rea.extension.sendMessage({method:"unLoad",id:Eventing.contextId,topframe:Eventing.topframe,forced:!!b,url:window.location.href},function(a){});try{Eventing.cleanup()}catch(f){console.log("cleanup: Error: "+f.message)}window.removeEventListener("DOMContentLoaded",m.onDOMContentLoaded,!1);window.removeEventListener("load",m.onLoad,!1);window.removeEventListener("unload",
w,!1);null!=s?(s.finalize(),s=null):console.log("content: Warning: multiple unload events detected!!!")},A=function(b,f,a){if(!r)return window.setTimeout(function(){A(b,f,a)},10),!0;if(u){var c=g.getResponseId(a);n.onContentRequest(b,f,c)}else window.self===window.top&&("getSrc"==b.method?a({src:h.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",m.onDOMContentLoaded,!1);window.addEventListener("load",m.onLoad,!1);var x=1,B=function(){g.backupProps();n.id=h.createUUID();window.addEventListener("unload",w,!1);rea.extension.onMessage.addListener(A);rea.extension.sendMessage({method:"prepare",id:n.id,raw:[],topframe:Eventing.topframe,url:window.location.href},function(b){void 0===b?(D&&console.debug("content: _early_ execution, connection to bg failed -> retry!"),
window.setTimeout(B,x),x*=2):(t=b.logLevel,(D|=60<=t)&&console.log("content: Started ("+n.id+", "+window.location.origin+window.location.pathname+")"),b.contexters||b.scripts&&b.scripts.length?(D&&console.log("content: start event processing for "+n.id+" ("+b.scripts.length+" to run)"),u=!0,Eventing.init(n.id),g.initEnv({inIncognitoContext:b.inIncognitoContext,downloadMode:b.downloadMode},b.scripts||[])):(D&&console.log("content: disable event processing for "+n.id),w(!0),u=!1),r=!0)})};rea.content.onReady(B)}})();
