OChat

Ochat - An Ogame Ally Chat which gets displayed ingame

  1. // ==UserScript==
  2. // @name OChat
  3. // @author Mertyr_u82
  4. // @namespace http://ochat.crypt-msg.de/
  5. // @version 0.12
  6. // @description Ochat - An Ogame Ally Chat which gets displayed ingame
  7. // @include http://*.ogame.*/game/index.php?*page=*
  8. // @include http://*-*.ogame.gameforge.com/game/index.php?page=*
  9. // @grant GM_listValues
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_deleteValue
  13. // @grant GM_xmlhttpRequest
  14. // @copyright 2014+, Mertyr
  15. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
  16. // ==/UserScript==
  17. var OJconfig;
  18. var mouseDragging = null;
  19. var lastAllyMsg = 0;
  20. var lastAllyMsgRead = 0;
  21. var allyChatTimer;
  22. var allyChatStatusTimer;
  23. var allyChatRate;
  24. var panelIsOpen = 0;
  25. var allyChatIsOpen = 0;
  26. var onlineListIsOpen = 0;
  27. var register = 0;
  28. var allyChatContinueLoading = 0;
  29.  
  30. var OJVersion = "0.12";
  31. var devTrigger = 0;
  32. var devAlert = 0;
  33. var OJIsBeta = 0;
  34.  
  35. jQuery.fn.exists = function() {return this.length>0;};
  36. jQuery.fn.slider = function() {
  37. $(this).children("h3").each(function(index, element){
  38. $(element).css("font-weight", "600");
  39. $(element).css("text-align", "center");
  40. $(element).css("height", "20px");
  41. $(element).css("line-height", "20px");
  42. $(element).css("margin-bottom", "2px");
  43. $(element).css("border", "1px solid #CCCCCC");
  44. $(element).css("border-radius", "5px");
  45. $(element).next().css("height", "auto");
  46. $(element).next().css("line-height", "20px");
  47. $(element).next().css("margin-bottom", "2px");
  48. $(element).next().css("border", "1px solid #999999");
  49. $(element).next().css("border-radius", "5px");
  50. $(element).css("cursor", "pointer");
  51. $(element).next().hide();
  52. $(element).click(function(event) {
  53. $(this).next().toggle();
  54. });
  55. });
  56. };
  57.  
  58. function OJgetCurrentUniverse()
  59. {
  60. var url = document.URL;
  61. url = url.replace("http://s", "");
  62. url = url.replace(/\.ogame\.gameforge\.com\/.*/, "");
  63. return url;
  64. }
  65.  
  66. function api(action, func, data) //data starts with "&key=value&key2=....."
  67. {
  68. if(typeof data === 'undefined')
  69. {
  70. data = "";
  71. }
  72. if(action == "login" || action == "register")
  73. {
  74. data = "action=" + action + data;
  75. }
  76. else
  77. {
  78. var uni = OJgetCurrentUniverse();
  79. var name = OJconfig["userName_" + uni];
  80. var passHash = OJconfig["password_" + uni];
  81. var hashed = 0;
  82. if(typeof OJconfig["hashed_" + uni] !== 'undefined')
  83. {
  84. hashed = OJconfig["hashed_" + uni];
  85. }
  86. data = "hashed=" + hashed + "&name=" + name + "&passHash=" + passHash + "&uni=" + uni + "&action=" + action + data;
  87. }
  88. var url = "http://ojobs.crypt-msg.de/api.php";
  89. if(devTrigger == 1)
  90. {
  91. url = "http://localhost/ojobs/api.php";
  92. }
  93. GM_xmlhttpRequest({
  94. method: "POST",
  95. url: url,
  96. data:data,
  97. headers: {
  98. "Content-Type": "application/x-www-form-urlencoded"
  99. },
  100. onabort: function(response) {
  101. alert("abort" + response.responseText);
  102. },
  103. onerror: function(response) {
  104. alert("error" + response.responseText);
  105. },
  106. ontimeout: function(response) {
  107. alert("timeout" + response.responseText);
  108. },
  109. onload: function(response) {
  110. if(devAlert == 1)
  111. {
  112. //$("#OJobs_panel").append("<span>" + response.responseText + "</span><br/>\n");
  113. dAlert(response.responseText);
  114. }
  115. func(response);
  116. var json = JSON.parse(response.responseText);
  117. return json;
  118. }
  119. });
  120. }
  121.  
  122. function OJReadMetas(uni)
  123. {
  124. if(typeof uni === 'undefined')
  125. {
  126. uni = OJgetCurrentUniverse();
  127. }
  128. var metas = document.getElementsByTagName('meta');
  129. var result = new Array();
  130. for (i=0; i<metas.length; i++) {
  131. var name = metas[i].getAttribute("name");
  132. if (name == "ogame-player-id") {
  133. result["ogameId_" + uni] = metas[i].getAttribute("content");
  134. }
  135. else if (name == "ogame-player-name") {
  136. result["userName_" + uni] = metas[i].getAttribute("content");
  137. }
  138. else if (name == "ogame-alliance-tag") {
  139. result["alliance_" + uni] = metas[i].getAttribute("content");
  140. }
  141. }
  142. return result;
  143. }
  144.  
  145. function OJAllyChatBlink(lastMsg, hidden)
  146. {
  147. if(typeof hidden === 'undefined')
  148. {
  149. hidden = 0;
  150. }
  151. if(lastMsg > 0 && allyChatContinueLoading == 1 && allyChatIsOpen == 0 && panelIsOpen == 1)
  152. {
  153. $("#OJobs_allyChat_header").css("background-color", "#26aa2b");
  154. $(".OJobs_allyChat_blinkText").css("color", "#6464dc");
  155. }
  156. else if( lastMsg > 0 && allyChatContinueLoading == 1 && panelIsOpen == 0)
  157. {
  158. $("#OJobs_tb").css("color", "#26aa2b");
  159. $(".OJobs_allyChat_blinkText").css("color", "#6464dc");
  160. }
  161. else if(lastMsg > 0 && lastMsg > lastAllyMsgRead)
  162. {
  163. $(".OJobs_allyChat_blinkText").css("color", "#6464dc");
  164. }
  165. else if(lastMsg == -1 || lastMsg == lastAllyMsgRead)
  166. {
  167. $(".OJobs_allyChat_from").css("color", "#6F9FC8");
  168. $(".OJobs_allyChat_blinkText").removeClass("OJobs_allyChat_blinkText");
  169. $("#OJobs_tb").css("color", "");
  170. $("#OJobs_allyChat_header").css("background-color", "");
  171. }
  172. else if(lastMsg == 0)
  173. {
  174. $("#OJobs_tb").css("color", "");
  175. $("#OJobs_allyChat_header").css("background-color", "");
  176. }
  177. }
  178.  
  179. function OJReadConfig()
  180. {
  181. OJconfig = new Array();
  182. GM_listValues().forEach(function(val) {
  183. OJconfig[val] = GM_getValue(val);
  184. });
  185. var metas = OJReadMetas();
  186. for(var m in metas)
  187. {
  188. OJconfig[m] = metas[m];
  189. }
  190. OJconfig["uni"] = OJgetCurrentUniverse();
  191. if((typeof OJconfig["userName_" + OJconfig["uni"]] === 'undefined' || OJconfig["userName_" + OJconfig["uni"]] == "" || OJconfig["userName_" + OJconfig["uni"]] == 'undefined') && $("#playerName").children("span").html().replace(/\s*/g, "") != 'undefined')
  192. {
  193. OJconfig["userName_" + OJconfig["uni"]] = $("#playerName").children("span").html().replace(/\s*/g, "");
  194. }
  195. if(typeof OJconfig["panelIsOpen_" + OJconfig["uni"]] == 'undefined')
  196. {
  197. OJconfig["panelIsOpen_" + OJconfig["uni"]] = "0";
  198. GM_setValue("panelIsOpen_" + OJconfig["uni"], "0");
  199. }
  200. if(typeof OJconfig["allyChatIsOpen_" + OJconfig["uni"]] === 'undefined')
  201. {
  202. OJconfig["allyChatIsOpen_" + OJconfig["uni"]] = "0";
  203. GM_setValue("allyChatIsOpen_" + OJconfig["uni"], "0");
  204. }
  205. if(typeof OJconfig["onlineListIsOpen_" + OJconfig["uni"]] === 'undefined')
  206. {
  207. OJconfig["onlineListIsOpen_" + OJconfig["uni"]] = "0";
  208. GM_setValue("onlineListIsOpen_" + OJconfig["uni"], "0");
  209. }
  210. if(typeof OJconfig["allyChat_refresh_" + OJconfig["uni"]] === "undefined")
  211. {
  212. OJconfig["allyChat_refresh_" + OJconfig["uni"]] = 10;
  213. GM_setValue("allyChat_refresh_" + OJconfig["uni"], 10);
  214. }
  215. if(typeof OJconfig["allyChat_fontSize_" + OJconfig["uni"]] === "undefined")
  216. {
  217. OJconfig["allyChat_fontSize_" + OJconfig["uni"]] = 12;
  218. GM_setValue("allyChat_fontSize_" + OJconfig["uni"], 12);
  219. }
  220. if(typeof OJconfig["lastAllyMsgRead_" + OJconfig["uni"]] === "undefined")
  221. {
  222. OJconfig["lastAllyMsgRead_" + OJconfig["uni"]] = 0;
  223. GM_setValue("lastAllyMsgRead_" + OJconfig["uni"], 0);
  224. }
  225. if(typeof OJconfig["allyChat_continueLoading_" + OJconfig["uni"]] === "undefined")
  226. {
  227. OJconfig["allyChat_continueLoading_" + OJconfig["uni"]] = 1;
  228. GM_setValue("allyChat_continueLoading_" + OJconfig["uni"], 1);
  229. }
  230. }
  231.  
  232. function OJobsAllyChatSend(message)
  233. {
  234. lastAllyMsgRead = lastAllyMsg;
  235. var func = function(response) {
  236. result = JSON.parse(response.responseText);
  237. if(result.status >= 1)
  238. {
  239. lastAllyMsgRead = result.messageId;
  240. OJconfig["lastAllyMsgRead_" + OJconfig["uni"]] = lastAllyMsgRead;
  241. GM_setValue("lastAllyMsgRead_" + OJconfig["uni"], lastAllyMsgRead);
  242. $("#OJobs_allyChat_input").val("");
  243. OJAllyChatBlink(-1);
  244. }
  245. else
  246. {
  247. $("#OJobs_allyChat_input").val(result.message);
  248. OJAllyChatBlink(lastAllyMsg);
  249. }
  250. };
  251. api("allyChat_send", func, "&message=" + message);
  252. }
  253.  
  254. function OJobsAllyChatGetMessages(hidden)
  255. {
  256. allyChatContinueLoading = allyChatContinueLoading%2;
  257. if(allyChatIsOpen != "1" && allyChatContinueLoading != 1)
  258. {
  259. clearTimeout(allyChatTimer);
  260. }
  261. else
  262. {
  263. var func = function(response) {
  264. result = JSON.parse(response.responseText);
  265. if(result.status == 1)
  266. {
  267. for (var key in result.messages) {
  268. var id = result.messages[key].id;
  269. var from = result.messages[key].name;
  270. var message = result.messages[key].message;
  271. var at = result.messages[key].at;
  272. if(id > lastAllyMsgRead)
  273. {
  274. $("#OJobs_allyChat_log").append("<span title=\"" + at + "\" class=\"OJobs_allyChat_blinkText OJobs_allyChat_from\" ><b>" + from + ":</b>&nbsp;</span>" + message + "<br />\n");
  275. }
  276. else
  277. {
  278. $("#OJobs_allyChat_log").append("<span title=\"" + at + "\" class=\"OJobs_allyChat_from\" ><b>" + from + ":</b>&nbsp;</span>" + message + "<br />\n");
  279. }
  280. if(id > lastAllyMsg)
  281. {
  282. lastAllyMsg = id;
  283. }
  284. }
  285. if(result.messages.length > 0)
  286. {
  287. $(".OJobs_allyChat_from").css("cursor", "pointer").css("color", "#6F9FC8");
  288. $(".OJobs_allyChat_blinkText").css("color", "#6464dc");
  289. $("#OJobs_allyChat_log").scrollTop($("#OJobs_allyChat_log")[0].scrollHeight);
  290. if(typeof hidden === 'undefined')
  291. {
  292. hiddenn = 0;
  293. }
  294. if(hidden == 0 || lastAllyMsg > lastAllyMsgRead)
  295. {
  296. OJAllyChatBlink(lastAllyMsg, hidden);
  297. }
  298. }
  299. }
  300. else
  301. {
  302. alert(result.message);
  303. }
  304. allyChatTimer = setTimeout(function() {OJobsAllyChatGetMessages()}, OJconfig["allyChat_refresh_" + OJconfig["uni"]]*1000);
  305. };
  306. api("allyChat_getMessages", func, "&lastMsg=" + lastAllyMsg);
  307. }
  308. }
  309.  
  310. function OJobsAllyChatGetOnlineStatus(hidden)
  311. {
  312. allyChatContinueLoading = allyChatContinueLoading%2;
  313. if(onlineListIsOpen == 1 && allyChatContinueLoading != 1)
  314. {
  315. clearTimeout(allyChatStatusTimer);
  316. }
  317. else
  318. {
  319. var func = function(response) {
  320. result = JSON.parse(response.responseText);
  321. if(result.status == 1)
  322. {
  323. $("#OJobs_allyChat_whoIsOnline").html("");
  324. for (var key in result.users) {
  325. var id = result.users[key].id;
  326. var name = result.users[key].name;
  327. var time = result.users[key].time;
  328. var minutes = result.users[key].minutes;
  329. var hours = result.users[key].hours;
  330. var statusClass = "OJobs_statusOnline";
  331. if(time > 900 && time < 3600)
  332. {
  333. statusClass = "OJobs_statusShort";
  334. }
  335. else if(time > 3600)
  336. {
  337. statusClass = "OJobs_statusLong";
  338. }
  339. var timeString = minutes + "m";
  340. if(hours > 0)
  341. {
  342. timeString = hours + "h" + timeString;
  343. }
  344. $("#OJobs_allyChat_whoIsOnline").append("<span ogameId=\"" + id + "\" class=\"" + statusClass + "\">" + name + "</span><span class=\"" + statusClass + " OJobs_statusTime\">" + timeString + "</span><span class=\"clearRight\">&nbsp;</span><br />\n");
  345. }
  346. $(".OJobs_statusTime").css("float", "right");
  347. $(".clearRight").css("clear", "right");
  348. $(".OJobs_statusOnline").css("color", "#26aa2b");
  349. $(".OJobs_statusShort").css("color", "#e1d000");
  350. $(".OJobs_statusLong").css("color", "#a63000");
  351. }
  352. else
  353. {
  354. alert(result.message);
  355. }
  356. allyChatStatusTimer = setTimeout(function() {OJobsAllyChatGetOnlineStatus()}, 15000);
  357. };
  358. api("allyChat_whoisOnline", func);
  359. }
  360. }
  361.  
  362. function OJCreateContent()
  363. {
  364. $("#OJobs_panel").append("<div id=\"OJobs_slider\">");
  365. $("#OJobs_slider").append("<h3>Config</h3><div id=\"OJobs_config\"></div>");
  366. $("#OJobs_slider").append("<h3 id=\"OJobs_allyChat_header\" >Ally-Chat</h3><div id=\"OJobs_allyChat\"></div>");
  367. $("#OJobs_slider").append("<h3 id=\"OJobs_allyChat_whoIsOnline_header\" >Online-Liste</h3><div id=\"OJobs_allyChat_whoIsOnline\"></div>");
  368. $("#OJobs_slider").slider();
  369. $("#OJobs_config").append("<table><tr><td>" +
  370. "Refresh:&nbsp;</td><td><textarea id=\"OJobs_config_allyChat_refresh\" \" /></textarea>" +
  371. "</td></tr><tr><td>" +
  372. "FontSize:&nbsp;</td><td><textarea id=\"OJobs_config_allyChat_fontSize\" /></textarea>" +
  373. "</td></tr><tr><td>" +
  374. "<span title=\"Sollen wie Nachrichten auch dann abgerufen werden, wenn das Fenster minimiert ist?\">Durchgehend laden</span>:&nbsp;</td><td><input type=\"checkbox\" name=\"OJobs_config_allyChat_continueLoading\" id=\"OJobs_config_allyChat_continueLoading\" /><i>(Zeigt Online-Status)</i>" +
  375. "</td></tr></table>");
  376. $("#OJobs_allyChat").append("<div id=\"OJobs_allyChat_log\"></div>");
  377. $("#OJobs_allyChat").children(0).css("height", "280px");
  378. $("#OJobs_allyChat").append("<textarea id=\"OJobs_allyChat_input\" ></textarea>");
  379. $("#OJobs_allyChat_input").css("width", "70%").css("height", "1.2em").css("min-height", "1.2em").css("max-height", "1.2em").css("top", "5px");
  380. $("#OJobs_allyChat").append("&nbsp;<button id=\"OJobs_allyChat_send\">Send</button>");
  381. $("#OJobs_allyChat_log").css("overflow", "auto");
  382. $("#OJobs_allyChat_log").click(function(event) {
  383. OJAllyChatBlink(-1);
  384. if(lastAllyMsg != 0)
  385. {
  386. lastAllyMsgRead = lastAllyMsg;
  387. OJconfig["lastAllyMsgRead_" + OJconfig["uni"]] = lastAllyMsgRead;
  388. GM_setValue("lastAllyMsgRead_" + OJconfig["uni"], lastAllyMsgRead);
  389. }
  390. });
  391. $("#OJobs_allyChat_log").css("word-wrap", "break-word");
  392. $("#OJobs_allyChat_header").click(function(event) {
  393. OJAllyChatBlink(0);
  394. $("#OJobs_allyChat_log").scrollTop($("#OJobs_allyChat_log")[0].scrollHeight);
  395. if(lastAllyMsg != 0)
  396. {
  397. OJconfig["lastAllyMsgRead_" + OJconfig["uni"]] = lastAllyMsgRead;
  398. GM_setValue("lastAllyMsgRead_" + OJconfig["uni"], lastAllyMsgRead);
  399. }
  400. });
  401. $("#OJobs_allyChat_input").css("margin-left", "3px");
  402. OJconfig["allyChat_continueLoading_" + OJconfig["uni"]] = OJconfig["allyChat_continueLoading_" + OJconfig["uni"]]%2;
  403. allyChatContinueLoading = OJconfig["allyChat_continueLoading_" + OJconfig["uni"]];
  404. if(allyChatContinueLoading == 1)
  405. {
  406. $("#OJobs_config_allyChat_continueLoading").attr("checked", "checked");
  407. }
  408. $("#OJobs_config_allyChat_continueLoading").change(function(event) {
  409. if($("#OJobs_config_allyChat_continueLoading").is(":checked")){
  410. allyChatContinueLoading = 1;
  411. }
  412. else
  413. {
  414. allyChatContinueLoading = 0;
  415. }
  416. OJconfig["allyChat_continueLoading_" + OJconfig["uni"]] = allyChatContinueLoading;
  417. GM_setValue("allyChat_continueLoading_" + OJconfig["uni"], allyChatContinueLoading);
  418. });
  419. allyChatRate = OJconfig["allyChat_refresh_" + OJconfig["uni"]];
  420. $("#OJobs_config_allyChat_refresh").val(allyChatRate);
  421. $("#OJobs_config_allyChat_refresh").change(function(event) {
  422. if($("#OJobs_config_allyChat_refresh").val() < 3)
  423. {
  424. $("#OJobs_config_allyChat_refresh").val("3");
  425. }
  426. allyChatRate = $("#OJobs_config_allyChat_refresh").val();
  427. OJconfig["allyChat_refresh_" + OJconfig["uni"]] = allyChatRate;
  428. GM_setValue("allyChat_refresh_" + OJconfig["uni"], allyChatRate);
  429. }).css("height", "1.2em").css("min-height", "1.2em").css("max-height", "1.2em");
  430. allyChatRate = OJconfig["allyChat_refresh_" + OJconfig["uni"]];
  431. $("#OJobs_config_allyChat_refresh").val(allyChatRate);
  432. $("#OJobs_config_allyChat_refresh").change(function(event) {
  433. if($("#OJobs_config_allyChat_refresh").val() < 3)
  434. {
  435. $("#OJobs_config_allyChat_refresh").val("3");
  436. }
  437. allyChatRate = $("#OJobs_config_allyChat_refresh").val();
  438. OJconfig["allyChat_refresh_" + OJconfig["uni"]] = allyChatRate;
  439. GM_setValue("allyChat_refresh_" + OJconfig["uni"], allyChatRate);
  440. }).css("height", "1.2em").css("min-height", "1.2em").css("max-height", "1.2em");
  441. $("#OJobs_config_allyChat_fontSize").val(OJconfig["allyChat_fontSize_" + OJconfig["uni"]]);
  442. $("#OJobs_allyChat").css("fontSize", OJconfig["allyChat_fontSize_" + OJconfig["uni"]] + "px");
  443. $("#OJobs_config_allyChat_fontSize").change(function(event) {
  444. OJconfig["allyChat_fontSize_" + OJconfig["uni"]] = $("#OJobs_config_allyChat_fontSize").val();
  445. GM_setValue("allyChat_fontSize_" + OJconfig["uni"], $("#OJobs_config_allyChat_fontSize").val());
  446. $("#OJobs_allyChat").css("fontSize", OJconfig["allyChat_fontSize_" + OJconfig["uni"]] + "px");
  447. }).css("height", "1.2em").css("min-height", "1.2em").css("max-height", "1.2em");
  448. MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  449.  
  450. var observer = new MutationObserver(function(mutations, observer) {
  451. if($("#OJobs_allyChat_input").hasClass("markItUpEditor"))
  452. {
  453. $("#OJobs_allyChat_input").parent().parent().parent()[0].outerHTML = $("#OJobs_allyChat_input")[0].outerHTML;
  454. $("#OJobs_allyChat_input").next().remove();
  455. $("#OJobs_allyChat_input").removeClass("markItUpEditor");
  456. }
  457. if($("#OJobs_config_allyChat_refresh").hasClass("markItUpEditor"))
  458. {
  459. $("#OJobs_config_allyChat_refresh").parent().parent().parent()[0].outerHTML = $("#OJobs_config_allyChat_refresh")[0].outerHTML;
  460. $("#OJobs_config_allyChat_refresh").next().remove();
  461. $("#OJobs_config_allyChat_refresh").removeClass("markItUpEditor");
  462. $("#OJobs_config_allyChat_refresh").val(OJconfig["allyChat_refresh_" + OJconfig["uni"]]);
  463. }
  464. if($("#OJobs_config_allyChat_fontSize").hasClass("markItUpEditor"))
  465. {
  466. $("#OJobs_config_allyChat_fontSize").parent().parent().parent()[0].outerHTML = $("#OJobs_config_allyChat_fontSize")[0].outerHTML;
  467. $("#OJobs_config_allyChat_fontSize").next().remove();
  468. $("#OJobs_config_allyChat_fontSize").removeClass("markItUpEditor");
  469. $("#OJobs_config_allyChat_fontSize").val(OJconfig["allyChat_fontSize_" + OJconfig["uni"]]);
  470. }
  471. observer.observe(document.getElementById("OJobs_allyChat_input"), {
  472. attributes: true
  473. });
  474. });
  475. observer.observe(document.getElementById("OJobs_allyChat_input"), {
  476. attributes: true
  477. });
  478. var OJAllySend = function(event) {
  479. event.preventDefault();
  480. if($("#OJobs_allyChat_input").val() != "")
  481. {
  482. var result = OJobsAllyChatSend($("#OJobs_allyChat_input").val());
  483. event.preventDefault();
  484. }
  485. };
  486. $("#OJobs_allyChat_send").click(function(event) {
  487. OJAllySend(event);
  488. }).css("height", "25px").css("min-height", "25px").css("max-height", "25px");
  489. $("#OJobs_allyChat_input").keydown(function(event) {
  490. if(event.keyCode == 13)
  491. {
  492. OJAllySend(event);
  493. }
  494. });
  495. $("#OJobs_allyChat").prev().click(function(event) {
  496. allyChatIsOpen = (allyChatIsOpen+1)%2;
  497. if(allyChatIsOpen == 1)
  498. {
  499. OJconfig["allyChatIsOpen_" +"uni"] = "1";
  500. GM_setValue("allyChatIsOpen_" + OJconfig["uni"], "1");
  501. OJobsAllyChatGetMessages(0);
  502. OJobsAllyChatGetOnlineStatus(0);
  503. }
  504. else
  505. {
  506. OJconfig["allyChatIsOpen_" +"uni"] = "0";
  507. GM_setValue("allyChatIsOpen_" + OJconfig["uni"], "0");
  508. }
  509. });
  510. $("#OJobs_allyChat_whoIsOnline_header").click(function(event) {
  511. onlineListIsOpen = (onlineListIsOpen+1)%2;
  512. if(onlineListIsOpen == 1)
  513. {
  514. OJconfig["onlineListIsOpen_" +"uni"] = "1";
  515. GM_setValue("onlineListIsOpen_" + OJconfig["uni"], "1");
  516. OJobsAllyChatGetOnlineStatus(0);
  517. }
  518. else
  519. {
  520. OJconfig["onlineListIsOpen_" +"uni"] = "0";
  521. GM_setValue("onlineListIsOpen_" + OJconfig["uni"], "0");
  522. }
  523. });
  524. if(GM_getValue("allyChatIsOpen_" + OJconfig["uni"]) == 1 && GM_getValue("panelIsOpen_" + OJconfig["uni"]) == 1)
  525. {
  526. OJobsAllyChatGetMessages(1);
  527. $("#OJobs_allyChat").toggle();
  528. allyChatIsOpen = (allyChatIsOpen+1)%2;
  529. }
  530. else if(allyChatContinueLoading == 1)
  531. {
  532. OJobsAllyChatGetMessages(1);
  533. }
  534. if(GM_getValue("onlineListIsOpen_" + OJconfig["uni"]) == 1)
  535. {
  536. OJobsAllyChatGetOnlineStatus(1);
  537. $("#OJobs_allyChat_whoIsOnline").toggle();
  538. onlineListIsOpen = (onlineListIsOpen+1)%2;
  539. }
  540. else if(allyChatContinueLoading == 1)
  541. {
  542. OJobsAllyChatGetOnlineStatus(1);
  543. }
  544. }
  545.  
  546. function OJLogout()
  547. {
  548. clearTimeout(allyChatTimer);
  549. var uni = OJgetCurrentUniverse();
  550. GM_deleteValue("userName_" + uni);
  551. GM_deleteValue("password_" + uni);
  552. GM_deleteValue("hashed_" + uni);
  553. delete OJconfig["userName_" + uni];
  554. delete OJconfig["password_" + uni];
  555. delete OJconfig["hashed_" + uni];
  556. $("#OJUsernameSpan").remove();
  557. $("#OJobs_slider").remove();
  558. $("#OJobs_loginDiv").show();
  559. allyChatIsOpen = 0;
  560. OJconfig["allyChatIsOpen_" + OJconfig["uni"]] = 0;
  561. GM_setValue("allyChatIsOpen_" + OJconfig["uni"], 0);
  562. onlineListIsOpen = 0;
  563. OJconfig["onlineListIsOpen_" + OJconfig["uni"]] = 0;
  564. GM_setValue("onlineListIsOpen_" + OJconfig["uni"], 0);
  565. allyChatContinueLoading = 0;
  566. OJconfig["OJobs_deleteUserSpanallyChatContinueLoading_" + OJconfig["uni"]] = 0;
  567. $("#OJobs_allyChat").hide();
  568. }
  569.  
  570. function OJLogin(force, hidden)
  571. {
  572. var uni = OJgetCurrentUniverse();
  573. var name;
  574. var passHashed;
  575. var hashed;
  576. var hashed = 0;
  577. if(typeof force === 'undefined')
  578. {
  579. force = 0;
  580. }
  581. if(force != 1 && typeof OJconfig["userName_" + uni] !== 'undefined' && OJconfig["userName_" + uni].length > 0)
  582. {
  583. name = OJconfig["userName_" + uni];
  584. }
  585. else
  586. {
  587. name = $("#OJobs_loginText").val();
  588. }
  589. if(force != 1 && typeof OJconfig["hashed_" + uni] !== 'undefined' && OJconfig["hashed_" + uni] == 1 && $("#OJobs_loginPass").val().length < 1)
  590. {
  591. passHashed = OJconfig["password_" + uni];
  592. hashed = 1;
  593. }
  594. else
  595. {
  596. if(force != 1 && typeof OJconfig["password_" + uni] !== 'undefined' && OJconfig["password_" + uni].length > 0 && $("#OJobs_loginPass").val().length < 1)
  597. {
  598. passHashed = OJconfig["password_" + uni];
  599. hashed = 0;
  600. }
  601. else
  602. {
  603. passHashed = $("#OJobs_loginPass").val();
  604. }
  605. }
  606. var func = function(response) {
  607. var result = JSON.parse(response.responseText);
  608. if(result.status >= 1)
  609. {
  610. GM_setValue("userName_" + uni, result.name);
  611. GM_setValue("password_" + uni, result.passHash);
  612. GM_setValue("hashed_" + uni, "1");
  613. OJconfig["userName_" + uni] = result.name;
  614. OJconfig["password_" + uni] = result.passHash;
  615. OJconfig["hashed_" + uni] = "1";
  616. $("#OJobs_loginDiv").hide();
  617. $("#OJobs_loginDiv").before("<span id =\"OJUsernameSpan\">Angemeldet:&nbsp;<b>" + result.name + "</b>&nbsp;<i id=\"OJLogout\">Logout<span id=\"OJobs_deleteUserSpan\" style=\"float: right;\">Delete</span><span style=\"clear: right;\"></span></i><br /></span>\n");
  618. $("#OJLogout").click(function (event) {
  619. OJLogout();
  620. }).css("cursor", "pointer");
  621. $("#OJobs_deleteUserSpan").click(function (event) {
  622. var ays = prompt("Soll der OChat-Account wircklich gelöscht weden?(\"YES\")");
  623. if(ays == "YES")
  624. {
  625. OJDeleteUser(ays);
  626. }
  627. else
  628. {
  629. alert("Bitte auch auf Groß- und Kleinschreibung achten.");
  630. }
  631. }).css("cursor", "pointer");
  632. OJCreateContent();
  633. if(allyChatContinueLoading == 0 && OJconfig["allyChatContinueLoading_" + OJconfig["uni"]] == 1)
  634. {
  635. allyChatContinueLoading = OJconfig["allyChatContinueLoading_" + OJconfig["uni"]];
  636. OJobsAllyChatGetMessages();
  637. OJobsAllyChatGetOnlineStatus();
  638. }
  639. var version1 = OJVersion;
  640. var version2;
  641. if(OJIsBeta == 1)
  642. {
  643. version2 = result.betaVersion;
  644. }
  645. else
  646. {
  647. version2 = result.clientVersion;
  648. }
  649. if(version1 != version2)
  650. {
  651. version1 = version1.split(".");
  652. version2 = version2.split(".");
  653.  
  654. var version2Old = 0;
  655. var versionBigger = 0;
  656. version2.forEach(function(element, index) {
  657. if(typeof version1[index] === 'undefined')
  658. {
  659. version1[index] = 0;
  660. }
  661. });
  662. version1.forEach(function(element, index) {
  663. if(typeof version2[index] === 'undefined')
  664. {
  665. version2[index] = 0;
  666. }
  667. version1[index] = parseInt(version1[index]);
  668. version2[index] = parseInt(version2[index]);
  669. if(version2Old == 0 && versionBigger == 0 && version1[index] < version2[index])
  670. {
  671. version2Old = 1;
  672. }
  673. else if(version2Old != 1 && version1[index] > version2[index])
  674. {
  675. versionBigger = 1
  676. }
  677. });
  678. if(version2Old == 1)
  679. {
  680. if(OJIsBeta != 1)
  681. {
  682. $("#OJobs_versionSpan").css("color", "#FF0000");
  683. $("#OJobs_versionSpan").append("<b>Update</b>");
  684. $("#OJobs_versionSpan").click(function(event) {
  685. window.location = "http://ochat.crypt-msg.de/OChat.user.js";
  686. }).css("cursor", "pointer");
  687. }
  688. else
  689. {
  690. $("#OJobs_versionSpan").css("color", "#FF0000");
  691. $("#OJobs_versionSpan").append("<b>Update</b>");
  692. $("#OJobs_versionSpan").click(function(event) {
  693. window.location = "http://ochat.crypt-msg.de/OChat.beta.user.js";
  694. }).css("cursor", "pointer");
  695. }
  696. }
  697. }
  698. }
  699. else
  700. {
  701. if(typeof hidden === "undefined")
  702. {
  703. hidden = 0;
  704. }
  705. if(hidden != 1)
  706. {
  707. alert("Falsche Benutzerdaten!");
  708. }
  709. OJLogout();
  710. }
  711. };
  712. var result = api("login", func, "&hashed=" + hashed + "&name=" + name + "&passHash=" + passHashed + "&uni=" + uni + "&version=" + OJVersion);
  713. }
  714.  
  715. function OJDeleteUser(areYouSure)
  716. {
  717. var func = function(response) {
  718. result = JSON.parse(response.responseText);
  719. if(result.status >= 1)
  720. {
  721. alert("Erfolgreich gelöscht!");
  722. }
  723. else
  724. {
  725. alert("Fehlgeschlagen:\n" + result.message);
  726. }
  727. };
  728. api("deleteUser", func, "&areYouSure=" + areYouSure);
  729. }
  730.  
  731. function OJRegister()
  732. {
  733. if($("#OJobs_registerPass1").val() == $("#OJobs_registerPass2").val())
  734. {
  735. var passHash = $("#OJobs_registerPass1").val();
  736. var func = function(response) {
  737. result = JSON.parse(response.responseText);
  738. if(result.status >= 1)
  739. {
  740. OJconfig["hashed_" + OJconfig["uni"]] = "1";
  741. GM_setValue("hashed_" + OJconfig["uni"], "1");
  742. OJconfig["password_" + OJconfig["uni"]] = result.passHash;
  743. GM_setValue("password_" + OJconfig["uni"], result.passHash);
  744. OJLogin(0,1);
  745. $("#OJobs_registerPass1").val("");
  746. $("#OJobs_registerPass2").val("");
  747. }
  748. else
  749. {
  750. alert("Anmeldung Fehlgeschlagen:\n" + result.message);
  751. }
  752. };
  753. api("register", func, "&hashed=0&name=" + $("#OJobs_loginText").val() + "&passHash=" + passHash + "&uni=" + OJconfig["uni"]);
  754. }
  755. else
  756. {
  757. alert("Die Passwörter stimmen nicht überein.");
  758. }
  759. }
  760.  
  761. function OJcreateMenu(hidden, event)
  762. {
  763. var uni = OJgetCurrentUniverse();
  764. $("#box").append("<div id=\"OJobs_panel\"></div>");
  765. $("#OJobs_panel").css("position", "absolute");
  766. $("#OJobs_panel").width("300px");
  767. $("#OJobs_panel").css("z-index", "3001").css("right", "-158px").css("height", "auto").css("background-color", "#192026");
  768. //$("#OJobs_panel").css("margin-left", "133px");
  769. $("#OJobs_panel").css("padding-top", "20px").css("padding-left", "5px").css("padding-right", "5px").css("padding-bottom", "20px");
  770. $("#OJobs_panel").append("<div id=\"OJobs_loginDiv\"><b>Nur auf der &Uuml;bersichtsseite einloggen und registrieren!</b><br /><br /></div>");
  771. //$("#OJobs_loginDiv").append("<form id=\"OJobs_loginForm\"><input type=\"text\" id=\"OJobs_loginText\" tabindex=\"1\" value=\"" + OJconfig["userName_" + uni] + "\"/><br />" +
  772. $("#OJobs_loginDiv").append("Login:<br /><form id=\"OJobs_loginForm\"><table><tr><td>Name</td><td><input type=\"text\" id=\"OJobs_loginText\" tabindex=\"1\" disabled=\"disabled\" value=\"" + OJconfig["userName_" + uni] + "\"/></td></tr>" +
  773. "<tr><td>Passwort:</td><td><input type=\"password\" id=\"OJobs_loginPass\" tabindex=\"2\" /></td></tr>" +
  774. "<tr><td>&nbsp;</td><td><button id=\"OJobs_loginButton\" tabindex=\"3\">Login</button></td></tr></table></form><br /><br />");
  775. $("#OJobs_loginDiv").append("Register:<br /><form id=\"OJobs_registerForm\"><table><tr><td>Name</td><td><input type=\"text\" id=\"OJobs_loginText\" tabindex=\"4\" title=\"MUSS mit namen aus diesem Universum übereinstimmen\" value=\"" + OJconfig["userName_" + uni] + "\"/></td></tr>" +
  776. "<tr><td>Passwort:</td><td><input type=\"password\" id=\"OJobs_registerPass1\" tabindex=\"5\" /></td></tr>" +
  777. "<tr><td>Passwort wdh.:</td><td><input type=\"password\" id=\"OJobs_registerPass2\" tabindex=\"6\" /></td></tr>" +
  778. "<tr><td>&nbsp;</td><td><button id=\"OJobs_registerButton\" tabindex=\"7\">Register</button></td></tr></table></form>");
  779. $("#OJobs_panel").append("<div id=\"OJobs_header\"><b>OChat</b>&nbsp;&nbsp;&nbsp;<i><span id=\"OJobs_versionSpan\">v" + OJVersion + "</span></i></div>");
  780. $("#OJobs_header").append("<span id=\"OJobs_minimizePanel\">[-]</span><span></span>");
  781. $("#OJobs_minimizePanel").css("float", "right").css("cursor", "pointer");
  782. $("#OJobs_minimizePanel").click(function (event) {
  783. OJdisplayMenu();
  784. });
  785. $("#OJobs_minimizePanel").next().css("clear", "right");
  786. $("#OJobs_panel").append("<div id=\"OJobs_footer\">&nbsp;</div>");
  787. $("#OJobs_loginForm").submit(function(event){
  788. event.preventDefault();
  789. });
  790. $("#OJobs_loginButton").click(function (event) {
  791. OJLogin(1);
  792. $("#OJobsloginPass").val("");
  793. event.preventDefault();
  794. });
  795. $("#OJobs_loginPass").keydown(function(event)
  796. {
  797. if(event.keyCode == 13)
  798. {
  799. event.preventDefault();
  800. }
  801. });
  802. $("#OJobs_footer").css("bottom", "0px").css("width", "100%").css("position", "absolute").css("left", "0px").css("background-color", "#222329");
  803. $("#OJobs_header").css("top", "0px").css("width", "306px").css("position", "absolute").css("left", "0px").css("background-color", "#222329").css("padding", "2px");
  804. $("#OJobs_header").css("cursor", "move");
  805. if(typeof OJconfig["userName_" + OJconfig["uni"]] !== 'undefined' && OJconfig["userName_" + OJconfig["uni"]].length > 0)
  806. {
  807. $("#OJobs_loginText").val(OJconfig["userName_" + OJconfig["uni"]]);
  808. if(OJconfig["hashed_" + OJconfig["uni"]] == 1)
  809. {
  810. if(typeof hidden === undefined)
  811. {
  812. OJLogin(0, 0);
  813. }
  814. else
  815. {
  816. OJLogin(0, hidden);
  817. }
  818. }
  819. }
  820. $("#OJobs_registerButton").click(function (event) {
  821. OJRegister();
  822. event.preventDefault();
  823. });
  824. var offLeft = GM_getValue("offsetLeft_" + OJconfig["uni"]);
  825. var offTop = GM_getValue("offsetTop_" + OJconfig["uni"]);
  826. if(offLeft != '0')
  827. {
  828. $("#OJobs_panel").offset({
  829. top: offTop,
  830. left: offLeft
  831. });
  832. }
  833. $( document ).on("mousemove", function(e) {
  834. if (mouseDragging == 1) {
  835. $("#OJobs_panel").offset({
  836. top: e.pageY-10,
  837. left: e.pageX-100
  838. });
  839. }
  840. });
  841. $("#OJobs_header").mousedown(function(event) {
  842. mouseDragging = 1;
  843. });
  844. $("#OJobs_header").on("mouseup", function(event) {
  845. mouseDragging = 0;
  846. GM_setValue("offsetTop_" + OJconfig["uni"], $("#OJobs_panel").offset().top);
  847. GM_setValue("offsetLeft_" + OJconfig["uni"], $("#OJobs_panel").offset().left);
  848. });
  849. }
  850.  
  851. function OJdisplayMenu(hidden, event)
  852. {
  853. if($("#OJobs_panel").exists() )
  854. {
  855. if($("#OJobs_panel").is(":visible"))
  856. {
  857. $("#OJobs_panel").hide();
  858. panelIsOpen = 0;
  859. OJconfig["panelIsOpen_" + OJconfig["uni"]] = 0;
  860. GM_setValue("panelIsOpen_" + OJconfig["uni"], 0);
  861. }
  862. else
  863. {
  864. $("#OJobs_panel").show();
  865. panelIsOpen = 1;
  866. OJconfig["panelIsOpen_" + OJconfig["uni"]] = 1;
  867. GM_setValue("panelIsOpen_" + OJconfig["uni"], 1);
  868. }
  869. }
  870. else
  871. {
  872. if(hidden == 1 && GM_getValue("panelIsOpen_" + OJconfig["uni"]) == 1)
  873. {
  874. panelIsOpen = 1;
  875. OJconfig["panelIsOpen_" + OJconfig["uni"]] = 1;
  876. GM_setValue("panelIsOpen_" + OJconfig["uni"], 1);
  877. OJcreateMenu(1, null);
  878. }
  879. else
  880. {
  881. if(hidden == 0)
  882. {
  883. panelIsOpen = 1;
  884. OJconfig["panelIsOpen_" + OJconfig["uni"]] = 1;
  885. GM_setValue("panelIsOpen_" + OJconfig["uni"], 1);
  886. OJcreateMenu(0, event);
  887. }
  888. else
  889. {
  890. panelIsOpen = 0;
  891. OJconfig["panelIsOpen_" + OJconfig["uni"]] = 0;
  892. GM_setValue("panelIsOpen_" + OJconfig["uni"], 0);
  893. OJcreateMenu(1, event);
  894. }
  895. }
  896. }
  897. }
  898.  
  899. $( document ).ready(function() {
  900. OJReadConfig();
  901. panelIsOpen = OJconfig["panelIsOpen_" + OJconfig["uni"]];
  902. allyChatIsOpen = OJconfig["allyChatIsOpen_" + OJconfig["uni"]];
  903. onlineListIsOpen = OJconfig["onlineListIsOpen_" + OJconfig["uni"]];
  904. allyChatContinueLoading = OJconfig["allyChatContinueLoading_" + OJconfig["uni"]];
  905. lastAllyMsgRead = OJconfig["lastAllyMsgRead_" + OJconfig["uni"]];
  906. var i = 0;
  907. var list = $("#bar").children("ul");
  908. var html = "<li><a href='#' id='OJobs_tb'>OChat</a></li>";
  909. list.append(html);
  910. $("#OJobs_tb").click(function(event) {
  911. OJdisplayMenu(0, event);
  912. if(allyChatIsOpen == 1 && panelIsOpen == 1 && !$("#OJobs_allyChat_log").is(":visible"))
  913. {
  914. $("#OJobs_allyChat_header").click();
  915. }
  916. });
  917. if(GM_getValue("panelIsOpen_" + OJconfig["uni"]) == "1")
  918. {
  919. OJdisplayMenu(1);
  920. }
  921. else if(OJconfig["allyChat_continueLoading_" + OJconfig["uni"]] == 1)
  922. {
  923. OJcreateMenu(1);
  924. $("#OJobs_panel").hide();
  925. }
  926. });
  927.  
  928. function dAlert(text)
  929. {
  930. var arr = text.split(",");
  931. var alertText ="";
  932. arr.forEach(function(element, index) {
  933. while(element.length > 45)
  934. {
  935. var t = element.substr(0, 45);
  936. element = element.substr(45);
  937. alertText = alertText + "\n" + t;
  938. }
  939. alertText = alertText + "\n" + element;
  940. });
  941. alert(alertText);
  942. }