nizamidris / Bot for Forge of Empires

// ==UserScript==
// @name         Bot for Forge of Empires
// @namespace    https://foe.botsoft.org
// @description  Bot for Forge of Empires
// @author       green elephant
// @updateUrl    https://foe.botsoft.org/foe.user.js
// @version      1.1.7.5
// @run-at       document-end
// @include      /^(http|https)://[a-z]+[0-9]+\.forgeofempires\.com/game/.*/
// @connect      /^[a-z]+[0-9]+\.forgeofempires\.com/
// @connect      innogamescdn.com
// @connect      foe.botsoft.org
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function(){
	var hash = Math.random().toString(36).substr(2),
		eventRequest  = hash + "request",
		eventResponse = hash + "response";

	var init = {
		url   : "https://foe.botsoft.org/ajax/",
		method: "POST",
		data  : JSON.stringify({
			method: "bot:hash",
			data  : {hash: hash}
		}),
		onload: function(response){
			var script = document.createElement("script");
			script.type = "text/javascript";
			script.src = "//foe.botsoft.org/bot.js";
			document.getElementsByTagName("head")[0].appendChild(script);
		}
	};
	GM_xmlhttpRequest(init);


	document.addEventListener(eventRequest, function(e){
		var params = (typeof e.detail=="object") ? e.detail : JSON.parse(e.detail);
		params.onload = function(response){
			var out = {
				id    : params.id,
				status: response.status,
				text  : response.responseText
			};
			var cloned = (typeof cloneInto=="function") ? cloneInto(out, document) : out;
			var e2 = new CustomEvent(eventResponse, {detail: cloned, bubblies: true});
			document.dispatchEvent(e2);
		};
		if (typeof e.detail.headers=="object") params.headers = e.detail.headers;
		GM_xmlhttpRequest(params);
	});
}());