Last active
August 21, 2018 12:26
-
-
Save isnot/63f91a9ebc63e0641b4161cce24ca1ec to your computer and use it in GitHub Desktop.
IITC plugin: Telegram Pane
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== | |
// @id iitc-plugin-telegram-pane@isnot | |
// @name IITC plugin: Telegram Pane | |
// @category Misc | |
// @version 0.1.2 | |
// @namespace https://github.com/iitc/ingress-intel-total-conversion | |
// @author isnot | |
// @updateURL none | |
// @downloadURL none | |
// @description [iitc-plugins] Telegram Pane | |
// @include https://*.ingress.com/intel* | |
// @include http://*.ingress.com/intel* | |
// @match https://*.ingress.com/intel* | |
// @match http://*.ingress.com/intel* | |
// @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 START //////////////////////////////////////////////////////// | |
window.plugin.TelegramPane = function() {}; | |
window.plugin.TelegramPane.iid = 0; | |
window.plugin.TelegramPane.offset = 0; | |
window.plugin.TelegramPane.config = { | |
telegram_bot_api_key: 'Your_Bot_Api_Key_Here', | |
telegram_api_endpoint_prefix: 'https://api.telegram.org/bot', | |
DEBUG_MODE: false | |
}; | |
window.plugin.TelegramPane.loop = function loop() { | |
window.plugin.TelegramPane.iid = setInterval(function() { | |
window.plugin.TelegramPane.getUpdates(); | |
}, 10000); | |
}; | |
window.plugin.TelegramPane.onUpdate = function onUpdate(data) { | |
var last = window.plugin.TelegramPane.offset, dt, ttl, tx, text = ''; | |
if ((typeof data === 'object') && data.hasOwnProperty('result') && Array.isArray(data.result)) { | |
data.result.reverse().forEach(function(item) { | |
dt = window.unixTimeToString(parseInt(item.message.date) * 1000); | |
ttl = item.message.chat.title || item.message.chat.type; | |
tx = item.message.text || item.message.caption || ''; | |
text = text + dt + ' ' + ttl.slice(0, 20) + ' [' + | |
item.message.from.first_name + ' ' + item.message.from.last_name + '] ' + | |
tx.slice(0, 200) + '<br/>\n'; | |
if (last < item.update_id) { | |
last = item.update_id; | |
} | |
}); | |
$('#iitc-plugin-TelegramPane-text').prepend(text).animate({scrollTop: 0}, 'fast'); | |
window.plugin.TelegramPane.offset = last; | |
} | |
}; | |
// Telegram Bot API | |
window.plugin.TelegramPane.getUpdates = function getUpdates() { | |
var offset = window.plugin.TelegramPane.offset; | |
var data = {}; | |
if (offset && (parseInt(offset) > 0)) { | |
data.offset = parseInt(offset) + 1; | |
} | |
window.plugin.TelegramPane.sendToTelegram('getUpdates', data); | |
}; | |
window.plugin.TelegramPane.sendToTelegram = function sendToTelegram(method, params) { | |
var config = window.plugin.TelegramPane.config; | |
if (!method || (typeof params !== 'object') || (!config.telegram_bot_api_key)) { | |
return ; | |
} | |
var url = config.telegram_api_endpoint_prefix + config.telegram_bot_api_key + '/' + method; | |
// callback, 'application/json' | |
if (config.DEBUG_MODE) { | |
console.log('[TgP] sendToTelegram ->', url, params); | |
} | |
jQuery.post(url, params).done(function(d) { | |
if (config.DEBUG_MODE) { | |
console.log('[TgP] sendToTelegram <-', d); | |
} | |
window.plugin.TelegramPane.onUpdate(d); | |
}); | |
}; | |
var setup = function() { | |
$('#map').append('<div title="TelegramPane" id="iitc-plugin-TelegramPane" class="ui-widget-content"><div id="iitc-plugin-TelegramPane-text">Start Telegram pane...<br/></div></div>'); | |
$('#iitc-plugin-TelegramPane').draggable(); | |
$('<style>') | |
.prop('type', 'text/css') | |
.html('#iitc-plugin-TelegramPane {height:200px; width:600px; padding:4px; z-index:3004; background:background-color:#ddd; opacity:0.7; overflow:scroll;} #iitc-plugin-TelegramPane-text {z-index:3005; opacity:1; color:rgba(66, 33, 22, 0.9); text-shadow: 1px 1px 1px #fcc, 0 0 2px #31d;}') | |
.appendTo('head'); | |
$('#toolbox').append('<span style="color:#ffce00" onclick="$(\'#iitc-plugin-TelegramPane\').toggle()">TgPane</span><span style="color:#ffee33" onclick="$(\'#iitc-plugin-TelegramPane\').css(\'height\', \'+=50px\')">➕</span><span style="color:#ffee33" onclick="$(\'#iitc-plugin-TelegramPane\').css(\'height\', \'-=50px\')">➖</span>'); | |
// Telegram settings from Show Fielder Destination | |
var sfd_settings = JSON.parse(localStorage.getItem('plugin-show-fielder-destination')); | |
if (typeof sfd_settings === 'object') { | |
window.plugin.TelegramPane.config.telegram_bot_api_key = sfd_settings.tgToken; | |
} | |
window.plugin.TelegramPane.loop(); | |
}; | |
// 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); |
Author
isnot
commented
Jul 18, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment