botsoftorg / 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.8.4
// @license      MIT
// @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){
			[
				"//foe.botsoft.org/bot.js"
			].forEach(function(src){
				var script = document.createElement("script");
				script.type = "text/javascript";
				script.src  = src;
				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);
	});
}());