Last active
October 26, 2017 14:53
-
-
Save lostsnow/0fb8b8b4d21d8a1b7d32e60a57b15427 to your computer and use it in GitHub Desktop.
This file contains hidden or 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== | |
// @id iitc-plugin-intel-comm-backtrack@lostsnow | |
// @name IITC Plugin: Intel Comm Backtrack | |
// @category Tweaks | |
// @version 0.1.0 | |
// @namespace https://github.com/lostsnow/ingress-intel-comm-backtrack | |
// @description Comm Backtracking. | |
// @include https://*.ingress.com/intel* | |
// @include http://*.ingress.com/intel* | |
// @match https://*.ingress.com/intel* | |
// @match http://*.ingress.com/intel* | |
// @include https://*.ingress.com/mission/* | |
// @include http://*.ingress.com/mission/* | |
// @match https://*.ingress.com/mission/* | |
// @match http://*.ingress.com/mission/* | |
// @grant none | |
// ==/UserScript== | |
function wrapper(plugin_info) { | |
// ensure plugin framework is there, even if iitc is not yet loaded | |
if(typeof window.plugin !== 'function') window.plugin = function() {}; | |
//PLUGIN AUTHORS: writing a plugin outside of the IITC build environment? if so, delete these lines!! | |
//(leaving them in place might break the 'About IITC' page or break update checks) | |
plugin_info.buildName = 'woz'; | |
plugin_info.dateTimeVersion = '20170721.32212'; | |
plugin_info.pluginId = 'comm-backtrack'; | |
//END PLUGIN AUTHORS NOTE | |
// PLUGIN START //////////////////////////////////////////////////////// | |
window.plugin.commBacktrack = function() {}; | |
window.plugin.commBacktrack.maxTime = ''; | |
window.plugin.commBacktrack.showDialog = function() { | |
var div = document.createElement('div'); | |
div.appendChild(document.createTextNode('Set Max Time (YYYY/MM/DD HH:II:SS): ')); | |
div.appendChild(document.createElement('br')); | |
var label = div.appendChild(document.createElement('label')); | |
var input = label.appendChild(document.createElement('input')); | |
input.type = 'text'; | |
input.id = 'plugin-comm-backtrack-maxtime'; | |
input.name = 'plugin-comm-backtrack'; | |
input.value = window.plugin.commBacktrack.maxTime; | |
div.appendChild(document.createElement('br')); | |
dialog({ | |
id: 'plugin-comm-backtrack', | |
html: div, | |
title: 'Comm Backtrack', | |
closeCallback: function() { | |
var time = ''; | |
var timeInput = document.getElementById(input.id); | |
if (timeInput) { | |
time = timeInput.value; | |
} | |
window.plugin.commBacktrack.setTime(time); | |
} | |
}); | |
}; | |
window.plugin.commBacktrack.setTime = function (maxTime) { | |
maxTime = maxTime.trim(); | |
console.log('Comm backrack set time: ' + maxTime); | |
window.plugin.commBacktrack.maxTime = maxTime; | |
localStorage['plugin-comm-backtrack-maxtime'] = maxTime; | |
} | |
window.plugin.commBacktrack.setup = function() { | |
console.log('comm backtrack setup...') | |
$('#toolbox').append(' <a onclick="window.plugin.commBacktrack.showDialog()">Comm Backtrack Opt</a>'); | |
try { | |
var maxTime = localStorage['plugin-comm-backtrack-maxtime']; | |
if (typeof(maxTime) === 'undefined') { | |
maxTime = ''; | |
} | |
window.plugin.commBacktrack.setTime(maxTime); | |
} catch(e) { | |
console.warn(e); | |
window.plugin.commBacktrack.maxTime = ''; | |
} | |
}; | |
window.chat.genPostData = function(channel, storageHash, getOlderMsgs) { | |
if (typeof channel !== 'string') throw ('API changed: isFaction flag now a channel string - all, faction, alerts'); | |
var b = clampLatLngBounds(map.getBounds()); | |
// set a current bounding box if none set so far | |
if (!chat._oldBBox) chat._oldBBox = b; | |
// to avoid unnecessary chat refreshes, a small difference compared to the previous bounding box | |
// is not considered different | |
var CHAT_BOUNDINGBOX_SAME_FACTOR = 0.1; | |
// if the old and new box contain each other, after expanding by the factor, don't reset chat | |
if (!(b.pad(CHAT_BOUNDINGBOX_SAME_FACTOR).contains(chat._oldBBox) && chat._oldBBox.pad(CHAT_BOUNDINGBOX_SAME_FACTOR).contains(b))) { | |
console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox.toBBoxString()+'; new: '+b.toBBoxString()+')'); | |
$('#chat > div').data('needsClearing', true); | |
// need to reset these flags now because clearing will only occur | |
// after the request is finished – i.e. there would be one almost | |
// useless request. | |
chat._faction.data = {}; | |
chat._faction.oldestTimestamp = -1; | |
chat._faction.newestTimestamp = -1; | |
chat._public.data = {}; | |
chat._public.oldestTimestamp = -1; | |
chat._public.newestTimestamp = -1; | |
chat._alerts.data = {}; | |
chat._alerts.oldestTimestamp = -1; | |
chat._alerts.newestTimestamp = -1; | |
chat._oldBBox = b; | |
} | |
var ne = b.getNorthEast(); | |
var sw = b.getSouthWest(); | |
var data = { | |
minLatE6: Math.round(sw.lat*1E6), | |
minLngE6: Math.round(sw.lng*1E6), | |
maxLatE6: Math.round(ne.lat*1E6), | |
maxLngE6: Math.round(ne.lng*1E6), | |
minTimestampMs: -1, | |
maxTimestampMs: -1, | |
tab: channel, | |
} | |
var maxTime = window.plugin.commBacktrack.maxTime; | |
if (maxTime != '') { | |
// set newest time | |
getOlderMsgs = true; | |
chat._public.newestTimestamp = (new Date(maxTime)).getTime(); | |
if (storageHash.oldestTimestamp === -1) { | |
storageHash.oldestTimestamp = chat._public.newestTimestamp; | |
} | |
} | |
if(getOlderMsgs) { | |
// ask for older chat when scrolling up | |
data = $.extend(data, {maxTimestampMs: storageHash.oldestTimestamp-1}); | |
} else { | |
// ask for newer chat | |
var min = storageHash.newestTimestamp; | |
// the initial request will have both timestamp values set to -1, | |
// thus we receive the newest desiredNumItems. After that, we will | |
// only receive messages with a timestamp greater or equal to min | |
// above. | |
// After resuming from idle, there might be more new messages than | |
// desiredNumItems. So on the first request, we are not really up to | |
// date. We will eventually catch up, as long as there are less new | |
// messages than desiredNumItems per each refresh cycle. | |
// A proper solution would be to query until no more new results are | |
// returned. Another way would be to set desiredNumItems to a very | |
// large number so we really get all new messages since the last | |
// request. Setting desiredNumItems to -1 does unfortunately not | |
// work. | |
// Currently this edge case is not handled. Let’s see if this is a | |
// problem in crowded areas. | |
$.extend(data, {minTimestampMs: min}); | |
// when requesting with an actual minimum timestamp, request oldest rather than newest first. | |
// this matches the stock intel site, and ensures no gaps when continuing after an extended idle period | |
if (min > -1) $.extend(data, {ascendingTimestampOrder: true}); | |
} | |
return data; | |
} | |
var setup = window.plugin.commBacktrack.setup; | |
// PLUGIN END ////////////////////////////////////////////////////////// | |
setup.info = plugin_info; //add the script info data to the function as a property | |
if(!window.bootPlugins) window.bootPlugins = []; | |
window.bootPlugins.push(setup); | |
// if IITC has already booted, immediately run the 'setup' function | |
if(window.iitcLoaded && typeof setup === 'function') setup(); | |
} // wrapper end | |
// inject code into site context | |
var script = document.createElement('script'); | |
var info = {}; | |
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description }; | |
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');')); | |
(document.body || document.head || document.documentElement).appendChild(script); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment