Last active
December 18, 2015 00:09
-
-
Save t-f-m/5694739 to your computer and use it in GitHub Desktop.
モバマス単騎凸サポーター
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name mbga single attack supporter | |
// @namespace http://d.hatena.ne.jp/t_f_m/ | |
// @include http://sp.pf.mbga.jp/12008305/* | |
// ==/UserScript== | |
var api = 'http://dojo.sekai.in/api/2/query.json'; | |
// thanks for @sekai67 | |
// http://momoka-sakurai.com/docs/dojolist_api_v2.html | |
var domain = 'http://sp.pf.mbga.jp/12008305/'; | |
var params = '?url=http%3A%2F%2F125.6.169.35%2Fidolmaster%2Fbattles%2Fbattle_check%2F'; | |
var live = domain + params; | |
function getCache(name, defaultValue) { | |
var r = JSON.parse(GM_getValue(name, defaultValue)); | |
return r; | |
} | |
function setCache(name, newValue) { | |
GM_setValue(name, JSON.stringify(newValue)); | |
} | |
var list = getCache('list', ' {"dojo": [], "expire": "", "access": 0}'); | |
function wrap_xmlhttpRequest(options) { | |
var req = new XMLHttpRequest(); | |
try { | |
req.onreadystatechange = function () { | |
if (req.readyState == 4) { | |
if (req.status == 200) { | |
if (options.onload) { | |
options.onload(req); | |
} | |
} | |
} | |
}; | |
req.open(options.method || "GET", options.url, true); | |
if (options.headers) { | |
var key; | |
for (key in options.headers) { | |
req.setRequestHeader(key, options.headers[key]); | |
} | |
} | |
req.send(options.data ? options.data : null); | |
} catch (e) { | |
if (options.onerror) { | |
options.onerror(req, e); | |
} | |
} | |
} | |
function goNext(list) { | |
location.href = live + list.dojo[0]; | |
} | |
function checkIdAndBattleCheck() { | |
location.href.match(/battle_check%2F(\d+)/); | |
return RegExp.$1; | |
} | |
function insertRemoveLink(anchor) { | |
var id = checkIdAndBattleCheck(); | |
if (!id) { | |
return; | |
} | |
id = parseInt(id, 10); | |
var r = document.createElement('a'); | |
r.innerText = ' next'; | |
anchor.parentNode.insertBefore(r, anchor.nextSibling); | |
r.href = "#"; | |
r.addEventListener('click', function () { | |
list.dojo.splice(list.dojo.indexOf(id), 1); | |
list.access = 0; | |
setCache('list', list); | |
goNext(list); | |
}, false); | |
} | |
function processMain(list) { | |
var a = insertLink(list.dojo[0]); | |
watchBattleSubmit(list); | |
insertRemoveLink(a); | |
} | |
function updateList(list, name) { | |
if (!list.dojo.length || !list.expire || checkDate(list.expire)) { /*GM*/ | |
wrap_xmlhttpRequest({ | |
url: api + '?len=300', | |
onload: function (res) { | |
var dojoIds = parseDojo(res.responseText); | |
list = { | |
"dojo": dojoIds, | |
"expire": makeLimit(), | |
"access": 0 | |
}; | |
setCache(name, list); | |
processMain(list); | |
}, | |
onerror: function () { | |
alert('error'); | |
} | |
}); | |
} else { | |
processMain(list); | |
} | |
} | |
function watchBattleSubmit(list) { | |
var id = checkIdAndBattleCheck(); | |
if (!id) { | |
return; | |
} | |
var inputs = document.evaluate(' //input[@type="submit"]', document.body, null, 7, null); | |
var clickLive = function (e) { | |
if (e.target.value == '回復する') { | |
--(list.access); | |
} | |
if (id == list.dojo[0]) { | |
if (list.access < 2) { | |
++(list.access); | |
} else { | |
list.dojo.splice(0, 1); | |
list.access = 0; | |
} | |
} else if (id in list.dojo) { | |
list.dojo.splice(list.dojo.indexOf(id), 1); | |
list.dojo.shift(id); | |
list.access = 1; | |
} | |
setCache('list', list); | |
setCache('live', 1); | |
}; | |
var l = inputs.snapshotLength; | |
var i; | |
for (i = 0; i < l; ++i) { | |
inputs.snapshotItem(i).addEventListener('click', clickLive, false); | |
} | |
} | |
if (getCache('live', 0)) { | |
if (location.href.match(/error/)) { | |
if (location.href.match(/battle_error2/)) { | |
list.dojo.shift(); | |
list.access = 0; | |
} else if (list.access > 0) { | |
--(list.access); | |
} | |
setCache('list', list); | |
} | |
setCache('live', 0); | |
goNext(list); | |
} | |
updateList(list, 'list'); | |
function checkDate(expire) { | |
return new Date() > new Date(expire); | |
} | |
function makeLimit() { | |
var now = new Date(); | |
if (now.getHours() >= 5) { | |
now.setDate(now.getDate() + 1); | |
} | |
now.setHours(5); | |
now.setMinutes(0); | |
now.setSeconds(0); | |
now.setMilliseconds(0); | |
return now.getTime(); | |
} | |
function parseDojo(data) { | |
var parsed = JSON.parse(data); | |
var dojoIds = parsed.data.records.map(function (i) { | |
return i.Data.ID; | |
}); | |
return dojoIds; | |
} | |
function insertLink(dojoId) { | |
if (!location.href.match(/battle/)) { | |
return; | |
} | |
var a = document.createElement('a'); | |
a.href = live + dojoId; | |
a.innerText = 'dojo'; | |
var Acd = document.getElementsByTagName('header')[0]; | |
var insP = Acd.childNodes[1]; | |
insP.parentNode.insertBefore(a, insP); | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment