Hacker Experience Log Helper

Adds a log database to help manage logs and some other little stuff (details are listed on https://github.com/pohky/loghelper)

当前为 2015-07-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Hacker Experience Log Helper
  3. // @namespace https://github.com/pohky/loghelper
  4. // @version 0.4
  5. // @description Adds a log database to help manage logs and some other little stuff (details are listed on https://github.com/pohky/loghelper)
  6. // @author sakuzyo
  7. // @match *://hackerexperience.com/*
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_listValues
  12. // @grant GM_deleteValue
  13. // ==/UserScript==
  14.  
  15. // ############### Log DB page stuffs
  16. if (window.location.href.search('logdb') > 0) {
  17. $('#sidebar ul li.active').attr('class','');
  18. $('#sidebar ul').append('<li class="active"><a href="log?logdb"><i class="fa fa-inverse"></i> <span>Log Database</span></a></li>');
  19. modLogDBPage();
  20. } else {
  21. $('#sidebar ul').append('<li><a href="log?logdb"><i class="fa fa-inverse"></i> <span>Log Database</span></a></li>');
  22. }
  23.  
  24. function modLogDBPage(){
  25. document.title = 'Log Database';
  26. $('.nav.nav-tabs:first').html('<li class="link active" id="tablocal"><a href="#" id="locallog"><span class="icon-tab he16-internet_log"></span>Local Logs</a></li>');
  27. $('.nav.nav-tabs:first').append('<li class="link" id="tabweb"><a href="#" id="weblog"><span class="icon-tab he16-internet_log"></span>Internet Logs</a></li>');
  28. $('.label.label-info').remove();
  29. $('#link0').attr('href','log?logdb'); $('#link0').html('LogDB');
  30. $('#content-header h1').html('Log Database');
  31. setupLogDbPage('local', 'Local');
  32. loadLocalLogs();
  33. }
  34.  
  35. function setupLogDbPage(dbtype, dbname){
  36. $('.widget-content').html('<div class="span12"><div class="span4"><div class="widget-box text-left">' +
  37. '<div class="widget-title"><span class="icon"><span class="he16-collect_info"></span></span><h5>Select ' + dbname + ' Log</h5></div>' +
  38. '<div class="widget-content ' + dbtype + 'logdb">' +
  39. '<div id="logdblist"></div>' +
  40. '</div></div></div>'+
  41. '<div class="span8"><div class="widget-box text-left">' +
  42. '<div class="widget-title"><span class="icon"><span class="he16-collect_info"></span></span><h5>Log Data</h5></div>' +
  43. '<div class="widget-content">' +
  44. '<textarea name="log" class="logarea" id="logdatatext" rows="15" spellcheck="FALSE" style="width: 98%;height: 350px;resize: vertical;"></textarea>'+
  45. '</div></div>'
  46. );
  47. }
  48.  
  49. $('#tablocal').click(function(){
  50. $('#tablocal').attr('class','link active');
  51. $('#tabweb').attr('class','link');
  52. setupLogDbPage('local', 'Local');
  53. loadLocalLogs();
  54. });
  55. $('#tabweb').click(function(){
  56. $('#tabweb').attr('class','link active');
  57. $('#tablocal').attr('class','link');
  58. setupLogDbPage('web', 'Internet');
  59. loadWebLogs();
  60. });
  61.  
  62. // ############### Error/Success Message
  63. function logsuccess(message){
  64. if (typeof(message)==='undefined' || typeof(message)==='object') message = '';
  65. if($('.alert').length !== 0) {
  66. $('.alert').remove();
  67. }
  68. $('.widget-box:first').before('<div class="alert alert-success"><button class="close" data-dismiss="alert">x</button><strong>Success!</strong> '+ message +' </div>');
  69. }
  70.  
  71. function logerror(message){
  72. if (typeof(message)==='undefined' || typeof(message)==='object') message = '';
  73. if($('.alert').length !== 0) {
  74. $('.alert').remove();
  75. }
  76. $('.widget-box:first').before('<div class="alert alert-error"><button class="close" data-dismiss="alert">x</button><strong>Error!</strong> '+ message +' </div>');
  77. }
  78.  
  79. if ($('#link0[href=log]').length) {
  80. $('form.log input.btn').before('<input class="btn btn-inverse" id="backuplocallog" type="button" value="Backup" style="width: 80px;" title="Save Log to Database">');
  81. $('#backuplocallog').after('<span> </span><input class="btn btn-inverse" id="clearlocallog" type="button" value="Clear" style="width: 80px;"><span> </span>');
  82. } else if ($('.internet.page-log').length) {
  83. $('form.log input.btn').before('<input class="btn btn-inverse" id="backupweblog" type="button" value="Backup" style="width: 80px;" title="Save Log to Database">');
  84. $('#backupweblog').after('<span> </span><input class="btn btn-inverse" id="hidemeweb" type="button" value="Hide Me" style="width: 80px;" title="Clear only lines with your IP"><span> </span>');
  85. }
  86.  
  87. // ############### Local Log DB Function Stuff
  88.  
  89. $('#backuplocallog').click(function(){
  90. if ($('form.log').length) {
  91. var bckup = backupLocalLog();
  92. if(bckup === 0){
  93. logerror('Already saved.');
  94. } else {
  95. logsuccess('Log saved to database.');
  96. }
  97. }
  98. else {
  99. console.log('No log found');
  100. }
  101. });
  102.  
  103. $('#clearlocallog').click(function(){
  104. if ($('form.log').length) {
  105. $('form.log').find('.logarea').val('');
  106. $('form.log').submit();
  107. }
  108. else {
  109. console.log('No log found');
  110. }
  111. });
  112.  
  113. function backupLocalLog() {
  114. var logArea = $('form.log').find('.logarea');
  115. var logText = logArea.val();
  116. var user = $('a[href=profile] span').text();
  117. var bckText = GM_getValue('localhost.' + user);
  118. if (typeof(bckText)==='undefined' || typeof(bckText)==='object') bckText = '';
  119. var newBckText = logText + bckText;
  120. var newLogArray = newBckText.split('\n');
  121. newLogArray = newLogArray.filter(function(value, index, self){
  122. return self.indexOf(value) === index;
  123. });
  124. newBckText = newLogArray.join('\n');
  125.  
  126. if(newBckText !== bckText){
  127. GM_setValue('localhost.' + user, newBckText);
  128. return 1;
  129. } else {
  130. logerror('This log is already saved.');
  131. return 0;
  132. }
  133. }
  134.  
  135. function loadLocalLogs(){
  136. var localList = GM_listValues();
  137. for (var i = 0; i < localList.length; i++) {
  138. var elem = localList[i];
  139. if (elem.indexOf('localhost') >= 0){
  140. elem = elem.split('.')[1];
  141. $('#logdblist').append('<div id="'+ elem +'"><a href="#" id="loadlocal" name="' + elem + '">localhost ('+ elem +')</a>&nbsp;&nbsp;&nbsp;'+
  142. '<a href="#" id="clearlocal" name="'+ elem +'">[clear]</a>&nbsp;&nbsp;&nbsp;'+
  143. '<a href="#" id="deletelocal" name="'+ elem +'">[delete]</a>'+
  144. '</br></div>'
  145. );
  146. }
  147. }
  148.  
  149.  
  150. $('a[id=loadlocal]').click(function(){
  151. var user = $(this).attr('name');
  152. var logText = GM_getValue('localhost.' + user);
  153. $('#logdatatext').val(logText);
  154. });
  155.  
  156. $('a[id=clearlocal]').click(function(){
  157. var user = $(this).attr('name');
  158. GM_setValue('localhost.' + user,'');
  159. $('#logdatatext').val('');
  160. if(GM_getValue('localhost.' + user) === '') {
  161. logsuccess('Backup successfully cleared.');
  162. }
  163. });
  164.  
  165. $('a[id=deletelocal]').click(function(){
  166. var user = $(this).attr('name');
  167. GM_deleteValue('localhost.' + user);
  168. $('div[id="'+ user +'"]').remove();
  169. $('#logdatatext').val('');
  170. });
  171. }
  172.  
  173. // ############### Internet Log DB Function Stuff
  174.  
  175. $('#hidemeweb').click(function() {
  176. if ($('form.log').length) {
  177. var logLines = $('form.log').find('.logarea').val().split('\n');
  178. var newLines = [];
  179.  
  180. $.each(logLines, function(i, el) {
  181. if (el.indexOf($('.header-ip-show').text()) === -1)
  182. newLines.push(el);
  183. });
  184.  
  185. $('form.log').find('.logarea').val(newLines.join('\n'));
  186. $('form.log').submit();
  187. }
  188. else {
  189. console.log('No log found');
  190. }
  191. });
  192.  
  193. function loadWebLogs(){
  194. var ipList = GM_listValues();
  195. for (var i = 0; i < ipList.length; i++) {
  196. var elem = ipList[i];
  197. if (elem.indexOf('localhost') == -1){
  198. $('#logdblist').append('<div id="'+ elem +'"><a href="#" id="loadweblog" name="' + elem + '">'+ elem +'</a>&nbsp;&nbsp;&nbsp;'+
  199. '<a href="#" id="clearweblog" name="'+ elem +'">[clear]</a>&nbsp;&nbsp;&nbsp;'+
  200. '<a href="#" id="deleteweblog" name="'+ elem +'">[delete]</a>&nbsp;&nbsp;&nbsp;'+
  201. '<a href="internet?ip='+ elem +'">[open]</a>'+
  202. '</br></div>'
  203. );
  204. }
  205. }
  206.  
  207. $('a[id=loadweblog]').click(function(){
  208. var logIP = $(this).attr('name');
  209. $('#logdatatext').val(GM_getValue(logIP));
  210. });
  211.  
  212. $('a[id=clearweblog]').click(function(){
  213. var logIP = $(this).attr('name');
  214. GM_setValue(logIP,'');
  215. if(GM_getValue(logIP) === '') {
  216. $('#logdatatext').val('');
  217. logsuccess('Backup successfully cleared.');
  218. }
  219. });
  220.  
  221. $('a[id=deleteweblog]').click(function(){
  222. var logIP = $(this).attr('name');
  223. GM_deleteValue(logIP);
  224. $('div[id="'+ logIP +'"]').remove();
  225. $('#logdatatext').val('');
  226. });
  227. }
  228.  
  229. function backupWebLog() {
  230. var logArea = $('form.log').find('.logarea');
  231. var logText = logArea.val();
  232. var bckIP = $('#link1').text().slice(1);
  233. var bckText = GM_getValue(bckIP);
  234. if (typeof(bckText)==='undefined' || typeof(bckText)==='object') bckText = '';
  235. var newBckText = logText + bckText;
  236. var newLogArray = newBckText.split('\n');
  237. newLogArray = newLogArray.filter(function(value, index, self){
  238. return self.indexOf(value) === index;
  239. });
  240. newBckText = newLogArray.join('\n');
  241.  
  242. if(newBckText !== bckText){
  243. GM_setValue(bckIP, newBckText);
  244. return 1;
  245. } else {
  246. logerror('This log is already saved.');
  247. return 0;
  248. }
  249. }
  250.  
  251. $('#backupweblog').click(function(){
  252. if ($('form.log').length) {
  253. var bckup = backupWebLog();
  254. if(bckup === 0){
  255. logerror('Already saved.');
  256. } else {
  257. logsuccess('Log saved to database.');
  258. }
  259. }
  260. else {
  261. console.log('No log found');
  262. }
  263. });
  264.  
  265.  
  266. // ############### Bitcoin stuffs
  267. if ($('#link1').text() == ' 157.218.117.4') {
  268. var btcobserver = new MutationObserver(function(mutations) {
  269. mutations.forEach(function(mutationRecord) {
  270. doBtcStuff();
  271. });
  272. });
  273.  
  274. var target = document.getElementById('modal');
  275. btcobserver.observe(target, { attributes : true, childList: true, attributeFilter : ['style'] });
  276. }
  277.  
  278. function doBtcStuff() {
  279. if ($('.modal-header h3').text().match('Buy')) {
  280. $('#btc-submit').before('<input id="btc-submit-max" class="btn btn-info" value="Buy Max. BTC" style="width: 95px" title="Buys maximum BTC with all money from all accounts!">');
  281. var maxmoney = $('span[title="Finances"]').text().replace(/[$,]/g, '');
  282. var curbtcp = $(document).text().match(/1 BTC = \$([0-9]{1,})/)[1];
  283. var maxbtc = maxmoney/curbtcp | 0;
  284. $('#btc-submit-max').click(function(){
  285. $('#btc-amount').val(maxbtc + '.0 BTC');
  286. document.getElementById('btc-submit').click();
  287. });
  288. $('input[name="btc-amount"]').keyup(function() {
  289. var inp = $('input[name="btc-amount"]').val().replace(/(.BTC)/,'').replace(/[,]/g,'');
  290. var newval = Math.round(inp*curbtcp).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  291. $('#btc-total').text(newval);
  292. });
  293.  
  294. var btcaccobserver = new MutationObserver(function(mutations) {
  295. mutations.some(function(mutationRecord) {
  296. var elem = mutationRecord.target.childNodes[1].firstChild.firstElementChild;
  297. if (elem.className == 'select2-chosen') {
  298. $('.select2-chosen').bind("DOMSubtreeModified",function() {
  299. var curbtcp = $(document).text().match(/1 BTC = \$([0-9]{1,})/)[1];
  300. var maxmon = $('.select2-chosen').text().match(/\(\$(.*)\)/)
  301. if(maxmon != null){
  302. var maxmon = maxmon[1].replace(/[,]/g,'');
  303. var maxbtc = maxmon/curbtcp | 0;
  304. $('#btc-amount').val(maxbtc + '.0 BTC');
  305. }
  306. });
  307. return true;
  308. }
  309. return false;
  310. });
  311. });
  312.  
  313. var target = document.getElementById('desc-money');
  314. btcaccobserver.observe(target, { attributes : true, childList: true });
  315. }
  316.  
  317. if ($('.modal-header h3').text().match('Sell')) {
  318. $('input[name="btc-amount"]').keyup(function() {
  319. var inp = $('input[name="btc-amount"]').val().replace(/(.BTC)/,'').replace(/[,]/g,'');
  320. var curbtcp = $(document).text().match(/1 BTC = \$([0-9]{1,4})/)[1];
  321. var newval = Math.ceil(curbtcp*inp).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  322. $('#btc-total').text(newval);
  323. });
  324. }
  325.  
  326. }
  327.  
  328.  
  329.