IdleScape - Chat.0

Better chat for IdleScape

  1. // ==UserScript==
  2. // @name IdleScape - Chat.0
  3. // @namespace D4IS
  4. // @version 1.2.1
  5. // @description Better chat for IdleScape
  6. // @author D4M4G3X
  7. // @match *://*.idlescape.com/*
  8. // @grant none
  9. // @require https://code.jquery.com/jquery-3.4.1.min.js
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.  
  15. /* INITIATE APPLICATION */
  16. let lib = {};
  17. let app = {};
  18. let initInterval = setInterval(()=> {
  19. if (window.D4IS) {
  20. clearInterval(initInterval);
  21. lib = window.D4IS.init('chat.0', '1.2.1');
  22. app = lib["chat.0"];
  23. lib.app.setUpdateLocation($('.chat-tab-private'));
  24. main();
  25. }
  26. }, 100);
  27. function main() {
  28. /* SET DEFAULT VALUES */
  29. app.chatHeight = 250;
  30.  
  31. /* RUN MAIN INTERVAL */
  32. let mainInterval = setInterval(()=> {
  33. if (app.ready && $('.status-bar').length) {
  34. if(!app.setup) {
  35. app.setup = true;
  36. if (lib.general.getStorage('ChatHeight')) {
  37. app.chatHeight = parseInt(lib.general.getStorage('ChatHeight'));
  38. }
  39. $('.play-area-chat-container').css('flex', '0 0 '+app.chatHeight+'px');
  40. }
  41. setupChannels();
  42. }
  43. }, 1000);
  44.  
  45. /* QUICK INTERVAL */
  46. let checkInterval = setInterval(()=> {
  47. if (app.ready && $('.status-bar').length) {
  48. checkUsernames();
  49. checkPrivates();
  50. checkCommands();
  51. checkMessageCount();
  52. whoisPopup();
  53. }
  54. }, 100);
  55.  
  56. /* UPDATE INTERVAL */
  57. let updateInterval = setInterval(()=> {
  58. if (app.ready && $('.status-bar').length) {
  59. lib.app.updateUser(app.name);
  60. }
  61. }, 5 * 60 * 1000);
  62.  
  63. function checkCommands() {
  64. $('.chat-message-system:not(.chat0-done)').each(function() {
  65. let $that = $(this);
  66. if (~$that.text().indexOf('Unknown command:')) {
  67. $that.addClass('chat0-done');
  68. let cmd = $that.text().split('Unknown command: ')[1];
  69. if (lib.app.commands) {
  70. if (lib.app.commands[cmd]) {
  71. lib.app.commands[cmd]();
  72. $that.css({
  73. 'height': '0',
  74. 'display': 'block',
  75. 'overflow': 'hidden'
  76. }).text('');
  77. }
  78. }
  79. }
  80. });
  81. }
  82.  
  83. function checkUsernames() {
  84. $('.message-username:not(.user-cloned)').each(function() {
  85. $(this).addClass('user-cloned');
  86. let $user = $('<div>', {
  87. 'class': $(this).attr('class') + ' user-menu'
  88. }).html($(this).html());
  89. $user.insertAfter($(this));
  90. $user.click(function(e) {
  91. openPopup($(this), e.pageX);
  92. });
  93. $(this).addClass('user-original');
  94. $(this).hide();
  95. });
  96. }
  97.  
  98. function gotoTab(tab, first = false) {
  99. $('.chat-tab').removeClass('selected-tab');
  100. $('.chat-tab-'+tab.toLowerCase()).addClass('selected-tab');
  101. $('.chat-interface-container').hide();
  102. $('.chat-interface-container.'+tab.toLowerCase()).show();
  103. if(first) {
  104. $('.chat-interface-container.'+tab.toLowerCase()).find('.chat-tabs > div:first-child').click();
  105. }
  106. }
  107.  
  108. function whoisPopup() {
  109. $('.chat-message-system:not(.chat0-done').each(function() {
  110. let $that = $(this);
  111. if (~$that.text().indexOf('total level')) {
  112. $that.addClass('chat0-done');
  113. let skills = ['mining', 'foraging', 'fishing', 'farming', 'smithing', 'crafting', 'cooking', 'constitution', 'attack', 'strength', 'defense', 'runecrafting', 'enchanting'];
  114. let user = $that.text().split('[SYSTEM]: ')[1].split(' ')[0];
  115. let league = $that.text().split('playing in ')[1].split('.')[0];
  116. let level = {};
  117.  
  118. let $html = $('<div>');
  119. let $wrap = $('<div>', {
  120. 'class': 'chat0-whois-level-wrap'
  121. }).appendTo($html);
  122.  
  123. $.each(skills, function(k, skill) {
  124. if (~$that.text().indexOf(lib.general.ucfirst(skill)+' Level: ')) {
  125. level[skill] = $that.html().split(lib.general.ucfirst(skill)+' Level: ')[1].split('<br>')[0];
  126. if(~level[skill].indexOf('+')) {
  127. level[skill] = level[skill].replace('(', '<span style="color: #7cd6ff">(');
  128. level[skill] = level[skill].replace(')', ')</span>');
  129. }
  130. let $level = $('<div>', {
  131. 'class': 'chat0-whois-level'
  132. }).appendTo($wrap);
  133. let $icon = $('<img>', {
  134. 'class': 'dialog-icon chat0-whois-level-icon'
  135. }).attr({
  136. 'src': lib.game.getSkillIcon(skill)
  137. }).appendTo($level);
  138. let $text = $('<span>', {
  139. 'class': 'dialog-text-big chat0-whois-level-text'
  140. }).html(lib.general.ucfirst(skill)+': '+level[skill]).appendTo($level);
  141. }
  142. });
  143. let $titlewrap = $('<div>');
  144.  
  145. let $title = $('<div>', {
  146. 'class': 'chat0-whois-title'
  147. }).appendTo($titlewrap);
  148.  
  149. let $text = $('<span>', {
  150. 'class': 'dialog-text-big chat0-whois-title-text'
  151. }).text(user).appendTo($title);
  152.  
  153. let $icon = $('<img>', {
  154. 'class': 'dialog-icon chat0-whois-title-icon'
  155. }).attr({
  156. 'src': lib.game.getLeagueIcon(league.split(' ')[0].toLowerCase())
  157. }).appendTo($title);
  158.  
  159. lib.game.dialog({
  160. 'title': $titlewrap.html(),
  161. 'text': $html.html(),
  162. 'class': 'chat0-whois',
  163. 'type': 'close',
  164. });
  165. $that.css({
  166. 'height': '0',
  167. 'display': 'block',
  168. 'overflow': 'hidden'
  169. }).text('');
  170. }
  171. });
  172. }
  173.  
  174. function openPopup($obj, posX) {
  175. if (lib.user.getName() == $obj.text()) { return false; }
  176. $('.chat-user-options').remove();
  177. let pos = $obj.offset();
  178. let $popup = $('<div>', {
  179. 'class': 'chat-user-options'
  180. }).appendTo($('body'));
  181.  
  182. popupButton('Whisper', function() {
  183. $obj.parent().find('.user-original').click();
  184. gotoTab('private');
  185. }).appendTo($popup);
  186.  
  187. popupButton('Whois', function() {
  188. $obj.parents('.chat-message-container').find('input').val('/whois ' + $obj.text());
  189. $obj.parents('.chat-message-container').find('input').focus();
  190. }).appendTo($popup);
  191.  
  192. popupButton('Block', function() {
  193. $obj.parents('.chat-message-container').find('input').val('/block ' + $obj.text());
  194. $obj.parents('.chat-message-container').find('input').focus();
  195. }).appendTo($popup);
  196.  
  197. popupButton('Close').appendTo($popup);
  198.  
  199. $popup.css({
  200. 'left': posX - 45,
  201. 'top': pos.top - ($popup.height() + 10)
  202. });
  203. }
  204.  
  205. function popupButton(text, cb = function(){}) {
  206. let $btn = $('<div>', {
  207. 'class': 'nav-tab no-select chat-user-option-' + text.toLowerCase()
  208. }).text(text);
  209. $btn.hover(function() {
  210. $(this).addClass('selected-tab');
  211. }, function() {
  212. $(this).removeClass('selected-tab');
  213. });
  214. $btn.click(function() {
  215. $(this).parent().remove();
  216. cb();
  217. });
  218. return $btn;
  219. }
  220.  
  221. function setupChannels() {
  222. if(!$('.chat-tab-wrap').length) {
  223. let $tabWrap = $('<ul>', {
  224. 'class': 'chat-tab-wrap'
  225. }).insertAfter($('.chat-buttons'));
  226. $tabWrap.css({
  227. 'display': 'flex',
  228. 'margin': '0',
  229. 'background':'#2a343e',
  230. 'border': '1px solid #222'
  231. });
  232. addChatTab('Public').appendTo($tabWrap);
  233. addChatTab('Private').appendTo($tabWrap);
  234.  
  235. $('.chat-buttons').remove();
  236.  
  237. let $options = $('<div>', {
  238. 'class': 'chat-option-wrap'
  239. }).appendTo($tabWrap);
  240.  
  241. let $option = [];
  242. $option[0] = $('<div>', {
  243. 'class': 'chat-button chat-option noselect'
  244. }).text('▲');
  245. $option[0].click(function() {
  246. if (app.chatHeight < 550) {
  247. app.chatHeight += 100;
  248. lib.general.setStorage('ChatHeight', app.chatHeight);
  249. $('.play-area-chat-container').css('flex', '0 0 '+app.chatHeight+'px');
  250. }
  251. });
  252. $option[1] = $('<div>', {
  253. 'class': 'chat-button chat-option noselect'
  254. }).text('▼');
  255. $option[1].click(function() {
  256. if (app.chatHeight > 150) {
  257. app.chatHeight -= 100;
  258. lib.general.setStorage('ChatHeight', app.chatHeight);
  259. $('.play-area-chat-container').css('flex', '0 0 '+app.chatHeight+'px');
  260. }
  261. });
  262.  
  263. $option[2] = $('<div>', {
  264. 'class': 'chat-button chat-option noselect'
  265. }).text('-');
  266. $option[2].click(function() {
  267. $.each($option, function() {
  268. $(this).hide();
  269. });
  270. $option[3].show();
  271. $('.play-area-chat-container').css('flex', '0 0 31px');
  272. });
  273.  
  274. $option[3] = $('<div>', {
  275. 'class': 'chat-button chat-option noselect'
  276. }).text('+').hide();
  277. $option[3].click(function() {
  278. $.each($option, function() {
  279. $(this).show();
  280. });
  281. $(this).hide();
  282. $('.play-area-chat-container').css('flex', '0 0 '+app.chatHeight+'px');
  283. });
  284.  
  285. $.each($option, function() {
  286. $(this).appendTo($options);
  287. });
  288. }
  289.  
  290. if(!$('.chat-interface-container.private').length) {
  291. $('.chat-message-container-box').css({
  292. 'background': '#222323',
  293. });
  294. let $publicChat = $('.chat-interface-container');
  295. let $privateChat = $publicChat.clone();
  296. $publicChat.addClass('public');
  297. $privateChat.addClass('private').insertAfter($publicChat);
  298. $privateChat.find('.chat-tab-channel').remove();
  299. $privateChat.find('.chat-message-container').remove();
  300. $privateChat.find('.chat-functions-item').remove();
  301. $privateChat.hide();
  302. }
  303. }
  304.  
  305. function checkPrivates() {
  306. $('.chat-interface-container.public').find('.chat-tabs > div:not(.closed)').each(function() {
  307. if ($(this).hasClass('chat-tab-whisper')) {
  308. let $publicChat = $('.chat-interface-container.public');
  309. let $privateChat = $('.chat-interface-container.private');
  310. let $whisperTab = $(this);
  311. let $chatBox = $publicChat.find('.chat-message-container-box > .chat-message-container:last-child');
  312.  
  313. $whisperTab.addClass('whisper-'+$(this).text().split(' ')[1]);
  314. $whisperTab.appendTo($privateChat.find('.chat-tabs'));
  315. $whisperTab.find('a').hide();
  316.  
  317. $chatBox.addClass('chatBox-'+$(this).text().split(' ')[1]);
  318. $chatBox.appendTo($privateChat.find('.chat-message-container-box'));
  319. }
  320. });
  321. }
  322.  
  323. function checkMessageCount() {
  324. let tabs = ['public', 'private']
  325. $.each(tabs, function(k, tab) {
  326. let count = 0;
  327. $('.chat-interface-container.' + tab).find('.chat-tabs > div').each(function() {
  328. if (~$(this).text().indexOf('(') && ~$(this).text().indexOf(')')) {
  329. count += parseInt($(this).text().split('(')[1].split(')')[0]);
  330. }
  331. });
  332. $('.chat-tab-'+tab).find('span').text('('+count+')');
  333. })
  334. }
  335.  
  336. function addChatTab(label) {
  337. let $chatTab = $('<li>', {
  338. 'class': 'nav-tab chat-tab chat-tab-' + label.toLowerCase()
  339. }).html(label+' <span>(0)</span>');
  340. if (label === 'Public') {
  341. $chatTab.addClass('selected-tab');
  342. }
  343. $chatTab.click(function() {
  344. gotoTab(label, true);
  345. });
  346. return $chatTab;
  347. }
  348. }
  349. })();
  350.  
  351. if (!window.D4IS_Socket) {
  352. window.D4IS_Socket = true;
  353. const sockets = [];
  354. const nativeWebSocket = window.WebSocket;
  355. window.WebSocket = function(...args){
  356. const socket = new nativeWebSocket(...args);
  357. sockets.push(socket);
  358. return socket;
  359. };
  360.  
  361. let setupSocket = setInterval(()=> {
  362. let lib = window.D4IS;
  363. if(sockets.length != 0){
  364. clearInterval(setupSocket);
  365. sockets[0].addEventListener('message', (e) => socketMessageHandler(e));
  366. }
  367. }, 10);
  368. }
  369. function socketMessageHandler(e) {
  370. let lib = window.D4IS;
  371. let getHandler = setInterval(()=> {
  372. if (lib) {
  373. clearInterval(getHandler);
  374. lib.app.messageHandler(e);
  375. }
  376. }, 10)
  377. }
  378.  
  379. function includeJS(file) {
  380. var script = document.createElement('script');
  381. script.src = file;
  382. script.type = 'text/javascript';
  383. script.defer = false;
  384.  
  385. document.getElementsByTagName('head').item(0).appendChild(script);
  386. }
  387.  
  388. function includeCSS(file) {
  389. var style = document.createElement('link');
  390. style.rel = 'stylesheet';
  391. style.href = file;
  392. style.type = 'text/css';
  393.  
  394. document.getElementsByTagName('head').item(0).appendChild(style);
  395. }
  396.  
  397. if (typeof window.D4IS_JS == 'undefined') {
  398. window.D4IS_JS = true;
  399. includeJS('https://digimol.net/idlescape/assets/js/lib.js');
  400. }
  401.  
  402.  
  403. if (typeof window.D4IS_CSS == 'undefined') {
  404. window.D4IS_CSS = true;
  405. includeCSS('https://digimol.net/idlescape/assets/css/game.css');
  406. }