I have no idea why I am reciving TypeError: GM.xmlHttpRequest is not a function .... Documentation is quite poor. Maybe someone knows a better option to send my "sumaAllUnits" to my PHP?
Please I need help! I am a beginner and I am trying my best, please have mercy.
My code is used to count my troops from a game. I have another Web application that I am using to do it. But I want to make it automatic. I recived a count with a javascript that was injected with tampermonkey. After I send "sumaAllUnits" I want to insert this map into database.
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.tampermonkey.net/index.php?version=4.9.6095&ext=fire&updated=true
// @grant none
// @include http://*.grepolis.com/*
// @include https://*.grepolis.com/*
// @include https://en121.grepolis.com/*
// @exclude forum.*.grepolis.*/*
// @exclude wiki.*.grepolis.*/*
// @connect https://spirtumzone.000webhostapp.com/
// @grant GM.xmlHttpRequest
// ==/UserScript==
setTimeout(function() {'use strict';
var interest = ['archer', 'attack_ship', 'big_transporter', 'bireme', 'calydonian_boar', 'catapult', 'centaur', 'cerberus', 'chariot', 'colonize_ship', 'demolition_ship', 'fury', 'godsent', 'griffin', 'harpy', 'hoplite', 'manticore', 'medusa', 'minotaur', 'pegasus', 'rider', 'sea_monster', 'slinger', 'small_transporter', 'sword', 'trireme', 'zyklop'];
let homeUnits = new Map();
function addToMap(key, value, map) {
//if the list is already created for the "key", then uses it
//else creates new list for the "key" to store multiple values in it.
map[key] = map[key] || [];
map[key].push(value);
}
var suma = new Map();
function doSum(map) {
map.reduce(function(acc, val) { return acc + val; }, 0)
}
var mar = [];
var x = document.getElementsByClassName("unit_icon40x40 sword unit")[0].childNodes[1].innerHTML;
console.log(x);
/* let collections = MM.getCollections();
console.log(collections.Units);
*/
let models = MM.getModels();
var name;
for (let v in models['Player'])
{
name = models['Player'][v]['attributes']['name'];
}
console.log(x);
//for (let v in models)
//{
// console.log([v]);
//}
//console.log(models['Units']);
// console.log(x);
for (let v in models['Units'])
{
if( models['Units'][v]['attributes']['current_town_id'] == models['Units'][v]['attributes']['home_town_id'] && models['Units'][v]['attributes']['current_town_player_name'] == name)
for (let homeU in models['Units'][v]['attributes'])
{
if (interest.indexOf(homeU) >= 0)
addToMap(homeU, models['Units'][v]['attributes'][homeU], homeUnits);
}
}
console.log(x);
console.log(homeUnits);
//console.log(doSum(homeUnits));
var sumaAllUnits = new Map();
for (var it in homeUnits){
addToMap(it, homeUnits[it].reduce(function(acc, val) { return acc + val; }, 0), suma);
}
console.log(models['Units']);
var allUnits = new Map();
for (let v in models['Units'])
{
for (let all in models['Units'][v]['attributes'])
{
if (interest.indexOf(all) >= 0)
addToMap(all, models['Units'][v]['attributes'][all], allUnits);
}
}
for (var at in allUnits){
addToMap(at, allUnits[at].reduce(function(acc, val) { return acc + val; }, 0), sumaAllUnits);
}
console.log(sumaAllUnits);
GM.xmlHttpRequest({
method: 'POST',
url: 'https://spirtumzone.000webhostapp.com/login.php',
headers: {'Content-Type': 'text/xml', "Accept": "text/xml" },
data: sumaAllUnits,
onload: function(response) { console.log('Loaded') }
});
}, 10000);