我不知道为什么我收到TypeError:GM.xmlHttpRequest不是一个函数...。 文档非常差。也许有人知道将“ sumaAllUnits”发送到PHP的更好的选择?
请帮助我!我是一个初学者,我正在尽力而为,请留意。
我的代码用于计算游戏中的部队人数。我有另一个正在使用的Web应用程序。但是我想使其自动化。我使用注入了tampermonkey的javascript进行计数。发送“ sumaAllUnits”后,我想将此地图插入数据库。
// ==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);