Advertisement
Guest User

IP.Chat Pings + Highlight (modified by Euklyd)

a guest
Mar 23rd, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name    IP.Chat Pings + Highlight
  3. // @namespace   Makaze
  4. // @include *
  5. // @grant   none
  6. // @version 2.2.0.1
  7. // ==/UserScript==
  8.  
  9. var titlePing = true, // Show pings in the page title
  10. pingHighlight = true, // Highlight pinged words and messages
  11. pingSound = new Audio('https://dl.dropboxusercontent.com/u/45569424/Saved_FE7.mp3'), // The sound file
  12.  
  13. audiblePing,
  14. hasFocus = true,
  15. MakazeScriptStyles = document.createElement('style'),
  16. pingMenu = document.createElement('div'),
  17. menuButton = document.createElement('a'),
  18. IPChatMenuItems = document.createElement('div'),
  19. styleElem,
  20. defaultStyle =
  21.     'font-weight: bolder;\n' +
  22.     'font-size: 120%;\n' +
  23.     'color: #f24;\n' +
  24.     'text-shadow: 0px 1px #222;',
  25. opts,
  26. pingStyle,
  27. pings,
  28. namePings,
  29. userName;
  30.  
  31. // Global variable constructor
  32.  
  33. function GlobalVariable(variable) {
  34.     var output;
  35.  
  36.     this.input = variable;
  37.  
  38.     this.receiveEvent = function(event) {
  39.         output = event.detail;
  40.     }
  41.  
  42.     document.addEventListener('getGlobalVar_' + this.input, this.receiveEvent);
  43.  
  44.     this.dispatch = function() {
  45.         document.dispatchEvent(
  46.             new CustomEvent('getGlobalVar_' + this.input, {
  47.                 detail: global
  48.             })
  49.         );
  50.     }
  51.  
  52.     this.newDispatch = function(global) {
  53.         var getGlobalScript = document.createElement('script');
  54.         getGlobalScript.type = 'text/javascript';
  55.         getGlobalScript.id = 'getGlobalScript';
  56.         getGlobalScript.appendChild(
  57.             document.createTextNode(
  58.                 'var dispatch = '
  59.                 + this.dispatch.toString().replace(/global/g, global).replace(/this\.input/, '\'' + global + '\'') + ';'
  60.                 +' \n\ndispatch();'
  61.                 + '\n\ndocument.getElementById(\'getGlobalScript\').remove();'
  62.             )
  63.         );
  64.         (document.body || document.documentElement).appendChild(getGlobalScript);
  65.     }
  66.  
  67.     this.get = function() {
  68.         this.newDispatch(variable);
  69.         return output;
  70.     }
  71. }
  72.  
  73. // End global variable constructor
  74.  
  75. function fade(elem, type, speed) {
  76.    
  77.     // Default values:
  78.  
  79.     switch (arguments.length) {
  80.         case 1:
  81.             type = 'toggle';
  82.         case 2:
  83.             speed = 300;
  84.         break;
  85.     }
  86.  
  87.     switch (type) {
  88.         case 'in':
  89.             elem.style.display = '';
  90.             setTimeout(function() {
  91.                 elem.style.transition = 'all ' + speed + 'ms ease-in-out';
  92.                 elem.style.opacity = 0;
  93.                 elem.style.opacity = 1;
  94.             }, 1);
  95.         break;
  96.         case 'out':
  97.             elem.style.transition = 'all ' + speed + 'ms ease-in-out';
  98.             elem.style.opacity = 1;
  99.             elem.style.opacity = 0;
  100.             setTimeout(function() {
  101.                 elem.style.display = 'none';
  102.             }, speed);
  103.         break;
  104.         case 'toggle':
  105.         default:
  106.             if (elem.style.display === 'none') {
  107.                 elem.style.display = '';
  108.                 setTimeout(function() {
  109.                     elem.style.transition = 'all' + speed + 'ms ease-in-out';
  110.                     elem.style.opacity = 0;
  111.                     elem.style.opacity = 1;
  112.                 }, 1);
  113.             } else {
  114.                 elem.style.transition = 'all ' + speed + 'ms ease-in-out';
  115.                 elem.style.opacity = 1;
  116.                 elem.style.opacity = 0;
  117.                 setTimeout(function() {
  118.                     elem.style.display = 'none';
  119.                 }, speed);
  120.             }
  121.     }
  122. }
  123.  
  124. function empty(elem) {
  125.     while (elem.hasChildNodes()) {
  126.         elem.removeChild(elem.lastChild);
  127.     }
  128. }
  129.  
  130. function onEachMatch(str, regex, callback, nestedCall) {
  131.     var matches = [];
  132.     while ((match = regex.exec(str)) != null) {
  133.         matches.push({'text': match, 'index': match.index});
  134.     }
  135.     callback(str, matches);
  136. }
  137.  
  138. if (document.body.id === 'ipboard_body' && document.getElementById('storage_chatroom') != null) {
  139.     userName = new GlobalVariable('userName').get();
  140.     opts = (localStorage.getItem('MakazeScriptOptions')) ? JSON.parse(localStorage.getItem('MakazeScriptOptions')) : {};
  141.     pingStyle = (opts.hasOwnProperty('ipc_ping_style')) ? opts.ipc_ping_style : defaultStyle;
  142.     pings = (opts.hasOwnProperty('ipc_pings')) ? opts.ipc_pings : [userName];
  143.     namePings = (opts.hasOwnProperty('ipc_namePings')) ? opts.ipc_namePings :['Railgun', 'Blyverrn', 'Saethori', 'tuvarkz', 'Soulreaper', 'Makaze', 'Euklyd'];
  144.     audiblePing = (opts.hasOwnProperty('ipc_audiblePing')) ? opts.ipc_audiblePing : true;
  145.  
  146.     // Styling
  147.  
  148.     if (document.getElementById('MakazeScriptStyles') == null) {
  149.         MakazeScriptStyles.id = 'MakazeScriptStyles';
  150.         MakazeScriptStyles.setAttribute('type', 'text/css');
  151.         document.head.appendChild(MakazeScriptStyles);
  152.     }
  153.  
  154.     styleElem = document.getElementById('MakazeScriptStyles');
  155.  
  156.     if (styleElem.hasChildNodes()) {
  157.         styleElem.childNodes[0].nodeValue += '\n\n';
  158.     } else {
  159.         styleElem.appendChild(document.createTextNode(''));
  160.     }
  161.  
  162.     if (!styleElem.childNodes[0].nodeValue.match(/\.MakazeScriptMenu/gi)) {
  163.         styleElem.childNodes[0].nodeValue += '.MakazeScriptMenu { position: fixed; z-index: 99999; top: 50%; left: 50%; padding: 10px; background-color: rgba(255, 255, 255, .85); box-shadow: 0px 0px 3px #888; border-radius: 5px; }  .MakazeScriptMenu .thead { font-weight: bolder; }  .MakazeScriptMenu td { padding: 3px; }  .MakazeScriptMenu .menu-save { text-align: center; margin-top: 6px; }  .MakazeScriptMenu .menu-save > a { padding: 2px 10px; border: 1px solid #ccc; border-radius: 3px; font-weight: bolder; cursor: pointer; }  .MakazeScriptMenu .menuTitle { margin-bottom: 10px; font-weight: bolder; }  .MakazeScriptMenu .scrollableContent { width: 312px; height: 150px; overflow: auto; padding: 2px; }\n\n';
  164.     }
  165.  
  166.     styleElem.childNodes[0].nodeValue +=
  167.         '#pingsettings {\n' +
  168.             'margin-top: -122px;\n' +
  169.             'margin-left: -168px;\n' +
  170.         '}\n\n' +
  171.  
  172.         '#pingsettings textarea {\n' +
  173.             'width: 144px;\n' +
  174.             'height: 144px;\n' +
  175.             'font-family: Consolas, Ubuntu Mono, sans-serif;\n' +
  176.             'font-size: 10px;\n' +
  177.             'color: #333;\n' +
  178.             'padding: 3px;\n' +
  179.         '}\n\n' +
  180.  
  181.         '#pingsettings .td-centered {\n' +
  182.             'text-align: center;\n' +
  183.         '}\n\n' +
  184.  
  185.         '#pingsettings input {\n' +
  186.             'vertical-align: middle;\n' +
  187.         '}';
  188.  
  189.     // Menu creation
  190.  
  191.     var pingMenu_table = document.createElement('table'),
  192.     pingMenu_tablehead = document.createElement('thead'),
  193.     pingMenu_tablehead_row = document.createElement('tr'),
  194.     pingMenu_tablehead_firstcell = document.createElement('td'),
  195.     pingMenu_tablehead_firstcell_Txt = document.createTextNode('Style'),
  196.     pingMenu_tablehead_lastcell = document.createElement('td'),
  197.     pingMenu_tablehead_lastcell_Txt = document.createTextNode('Pings'),
  198.     pingMenu_tablebody = document.createElement('tbody'),
  199.     pingMenu_tablebody_firstrow = document.createElement('tr'),
  200.     pingMenu_tablebody_firstrow_firstcell = document.createElement('td'),
  201.     pingMenu_tablebody_firstrow_firstcell_styleinput = document.createElement('textarea'),
  202.     pingMenu_tablebody_firstrow_lastcell = document.createElement('td'),
  203.     pingMenu_tablebody_firstrow_lastcell_pinginput = document.createElement('textarea'),
  204.     pingMenu_tablebody_lastrow = document.createElement('tr'),
  205.     pingMenu_tablebody_lastrow_cell = document.createElement('td'),
  206.     pingMenu_tablebody_lastrow_cell_checkbox = document.createElement('input'),
  207.     pingMenu_tablebody_lastrow_cell_Txt = document.createTextNode(' Play sound when pinged'),
  208.     pingMenu_menusave = document.createElement('div'),
  209.     pingMenu_menusaveLink = document.createElement('a'),
  210.     pingMenu_menusaveLink_Txt = document.createTextNode('Save');
  211.  
  212.     // Tablehead
  213.  
  214.     pingMenu_tablehead_firstcell.appendChild(pingMenu_tablehead_firstcell_Txt);
  215.     pingMenu_tablehead_lastcell.appendChild(pingMenu_tablehead_lastcell_Txt);
  216.  
  217.     pingMenu_tablehead_row.appendChild(pingMenu_tablehead_firstcell);
  218.     pingMenu_tablehead_row.appendChild(pingMenu_tablehead_lastcell);
  219.  
  220.     pingMenu_tablehead.appendChild(pingMenu_tablehead_row);
  221.  
  222.     // Tablebody
  223.  
  224.     // First row
  225.  
  226.     pingMenu_tablebody_firstrow_firstcell_styleinput.id = 'pingStyle';
  227.     pingMenu_tablebody_firstrow_firstcell.appendChild(pingMenu_tablebody_firstrow_firstcell_styleinput);
  228.  
  229.     pingMenu_tablebody_firstrow_lastcell_pinginput.id = 'pingList';
  230.     pingMenu_tablebody_firstrow_lastcell.appendChild(pingMenu_tablebody_firstrow_lastcell_pinginput);
  231.  
  232.     pingMenu_tablebody_firstrow.appendChild(pingMenu_tablebody_firstrow_firstcell);
  233.     pingMenu_tablebody_firstrow.appendChild(pingMenu_tablebody_firstrow_lastcell);
  234.  
  235.     // Last row
  236.  
  237.     pingMenu_tablebody_lastrow_cell_checkbox.type = 'checkbox';
  238.     pingMenu_tablebody_lastrow_cell_checkbox.id = 'audiblePingSwitch';
  239.     pingMenu_tablebody_lastrow_cell.appendChild(pingMenu_tablebody_lastrow_cell_checkbox);
  240.     pingMenu_tablebody_lastrow_cell.appendChild(pingMenu_tablebody_lastrow_cell_Txt);
  241.  
  242.     pingMenu_tablebody_lastrow.appendChild(pingMenu_tablebody_lastrow_cell);
  243.  
  244.     pingMenu_tablebody.appendChild(pingMenu_tablebody_firstrow);
  245.     pingMenu_tablebody.appendChild(pingMenu_tablebody_lastrow);
  246.  
  247.     // Menu save
  248.  
  249.     pingMenu_menusaveLink.href = 'javascript:void(0)';
  250.     pingMenu_menusaveLink.id = 'pingsettings_save';
  251.     pingMenu_menusaveLink.appendChild(pingMenu_menusaveLink_Txt);
  252.     pingMenu_menusaveLink.onclick = function() {
  253.         opts = (localStorage.getItem('MakazeScriptOptions')) ? JSON.parse(localStorage.getItem('MakazeScriptOptions')) : {};
  254.  
  255.         if (styleElem.hasChildNodes()) {
  256.             styleElem.childNodes[0].nodeValue += '\n\n';
  257.         } else {
  258.             styleElem.appendChild(document.createTextNode(''));
  259.         }
  260.        
  261.         styleElem.childNodes[0].nodeValue = styleElem.childNodes[0].nodeValue.replace(/\.ipc_hilight\s{[^]+}/gi, '.ipc_hilight {\n' + document.getElementById('pingStyle').value + '\n' + '}');
  262.  
  263.         opts.ipc_ping_style = document.getElementById('pingStyle').value;
  264.         opts.ipc_pings = document.getElementById('pingList').value.split('\n');
  265. // work on this:       
  266.         opts.ipc_namePings = document.getElementById('namePingList').value.split('\n');
  267. // /work       
  268.         opts.ipc_audiblePing = document.getElementById('audiblePingSwitch').checked;
  269.         localStorage.setItem('MakazeScriptOptions', JSON.stringify(opts));
  270.         fade(this.parentNode.parentNode, 'out');
  271.     }
  272.  
  273.     pingMenu_menusave.className = 'menu-save';
  274.     pingMenu_menusave.appendChild(pingMenu_menusaveLink);
  275.  
  276.     // Build it all
  277.  
  278.     pingMenu_table.appendChild(pingMenu_tablehead);
  279.     pingMenu_table.appendChild(pingMenu_tablebody);
  280.  
  281.     pingMenu.id = 'pingsettings';
  282.     pingMenu.className = 'MakazeScriptMenu';
  283.     pingMenu.style.display =  'none';
  284.  
  285.     pingMenu.appendChild(pingMenu_table);
  286.     pingMenu.appendChild(pingMenu_menusave);
  287.  
  288.     document.body.appendChild(pingMenu);
  289.     document.getElementById('pingStyle').value = pingStyle;
  290.     document.getElementById('pingList').value = pings.join('\n');
  291.     document.getElementById('audiblePingSwitch').checked = audiblePing;
  292.  
  293.     // Button creation
  294.  
  295.     if (document.getElementById('IPChatMenuItems') == null) {
  296.         IPChatMenuItems.id = 'IPChatMenuItems';
  297.         IPChatMenuItems.setAttribute('style', 'text-align: right;');
  298.         document.getElementById('chatters-online-wrap').nextSibling.nextSibling.getElementsByTagName('ul')[0].appendChild(IPChatMenuItems);
  299.     }
  300.  
  301.     if (document.getElementById('IPChatMenuItems').hasChildNodes()) {
  302.         document.getElementById('IPChatMenuItems').appendChild(document.createElement('br'));
  303.     }
  304.  
  305.     menuButton.id = 'pingMenuButton';
  306.     menuButton.className = 'ipsButton_secondary';
  307.     menuButton.href = 'javascript:void(0)';
  308.     menuButton.setAttribute('style', 'margin-top: 10px;');
  309.     menuButton.appendChild(document.createTextNode('Ping Settings'));
  310.  
  311.     document.getElementById('IPChatMenuItems').appendChild(menuButton);
  312.  
  313.     document.getElementById('pingMenuButton').onclick = function() {
  314.         fade(document.getElementById('pingsettings'));
  315.     }
  316.  
  317.     if (pingHighlight) {
  318.         if (styleElem.hasChildNodes()) {
  319.             styleElem.childNodes[0].nodeValue += '\n\n';
  320.         } else {
  321.             styleElem.appendChild(document.createTextNode(''));
  322.         }
  323.         styleElem.childNodes[0].nodeValue +=
  324.             '.ipc_highlight {\n' +
  325.                 pingStyle + '\n' +
  326.             '}';
  327.     }
  328.  
  329.     document.addEventListener('DOMNodeInserted', function(event) {
  330.         var latestMessage,
  331.         nameToCheck,
  332.         textToCheck,
  333.         nodeToCheck,
  334.         nodeTextToCheck,
  335.         opts = (localStorage.getItem('MakazeScriptOptions')) ? JSON.parse(localStorage.getItem('MakazeScriptOptions')) : {},
  336.         pings = (opts.hasOwnProperty('ipc_pings')) ? opts.ipc_pings : [userName],
  337. // This is where names are changed:
  338.         namePings = (opts.hasOwnProperty('ipc_namePings')) ? opts.ipc_namePings :['Railgun', 'Blyvern', 'Makaze', 'Saethori', 'Tuvarkz', 'Alm', 'Euklyd'],
  339.         audiblePing = (opts.hasOwnProperty('ipc_audiblePing')) ? opts.ipc_audiblePing : true,
  340.         ping,
  341.         playSound;
  342.  
  343.         if (event.target.nodeType !== 1 || event.target.id !== 'storage_chatroom') {
  344.             return false;
  345.         }
  346.  
  347.         if (!pings.length) {
  348.             return false;
  349.         }
  350.  
  351.         latestMessage = event.target.parentNode.getElementsByTagName('div')[event.target.parentNode.getElementsByTagName('div').length - 1];
  352.  
  353.         if (!latestMessage.parentNode.className.match(/post/gi)) {
  354.             return false;
  355.         }
  356.  
  357.         if (latestMessage.parentNode.className.match(/chat-myown/gi)) {
  358. //          return false;
  359.         }
  360.  
  361. // sometimes this is undefined, need to fix
  362.         nameToCheck = latestMessage.parentNode.getElementsByTagName('label')[0].innerHTML;
  363.         console.log(nameToCheck);
  364.        
  365.         textToCheck = latestMessage.innerHTML;
  366.         console.log(textToCheck);
  367.  
  368.         if (titlePing) {
  369.             if (!hasFocus) {
  370.                 for (i = 0; i < namePings.length; i++) {
  371.                     if (nameToCheck === namePings[i] && textToCheck === '  has entered the room') {
  372.                         namePing = new RegExp(namePings[i], "gi");
  373.                         if (document.title.match(/\(P:/gi)) {
  374.                             if (!document.title.match(namePing)) {
  375.                                 document.title = document.title.replace(/\(P:\s(.*?)\)/gi, '(P: $1 ' + namePings[i] + ')');
  376.                             }
  377.                         } else {
  378.                             document.title = '(P: ' + namePings[i] + ') ' + document.title;
  379.                         }
  380.                     }
  381.                 }
  382.                 for (i = 0; i < pings.length; i++) {
  383.                     ping = new RegExp(pings[i], "gi");
  384.                     if (textToCheck.match(ping)) {
  385.                         if (document.title.match(/\(P:/gi)) {
  386.                             if (!document.title.match(ping)) {
  387.                                 document.title = document.title.replace(/\(P:\s(.*?)\)/gi, '(P: $1 ' + pings[i] + ')');
  388.                             }
  389.                         } else {
  390.                             document.title = '(P: ' + pings[i] + ') ' + document.title;
  391.                         }
  392.                     }
  393.  
  394.                 }
  395.             }
  396.         }
  397.  
  398.         if (pingHighlight) {
  399.             for (i = 0; i < namePings.length; i++) {
  400.                 namePing = new RegExp('(' + 'has entered the room' + ')', "gi");
  401.                 if (nameToCheck === namePings[i] && textToCheck === '  has entered the room') {
  402.                     console.log('p-p-ping, bitch (name)');
  403.                     latestMessage.parentNode.className += ' chat-myown';
  404.                     latestMessage.innerHTML = latestMessage.innerHTML.replace(namePing, '<span class="ipc_highlight">$1</span>');
  405.                 }
  406.             }
  407.             for (i = 0; i < pings.length; i++) {
  408.                 ping = new RegExp('(' + pings[i] + ')', "gi");
  409.                 if (textToCheck.match(ping)) {
  410.                     latestMessage.parentNode.className += ' chat-myown';
  411.                     latestMessage.innerHTML = latestMessage.innerHTML.replace(ping, '<span class="ipc_highlight">$1</span>');
  412.                 }
  413.             }
  414.         }
  415.  
  416.         if (audiblePing) {
  417.             playSound = false;
  418.             for (i = 0; i < namePings.length; i++) {
  419.                 ping = new RegExp('(' + 'has entered the room' + ')', "gi");
  420.                 if (nameToCheck === namePings[i] && textToCheck === '  has entered the room') {
  421.                     playSound = true;
  422.                 }
  423.             }
  424.             for (i = 0; i < pings.length; i++) {
  425.                 ping = new RegExp('(' + pings[i] + ')', "gi");
  426.                 if (textToCheck.match(ping)) {
  427.                     playSound = true;
  428.                 }
  429.             }
  430.             if (playSound) {
  431.                 pingSound.play();
  432.             }
  433.         }
  434.     });
  435.  
  436.     window.onblur = function() {
  437.         hasFocus = false;
  438.     }
  439.  
  440.     window.onfocus = function() {
  441.         hasFocus = true;
  442.         if (document.title.match(/\(P:/gi)) {
  443.             document.title = document.title.replace(/\(P:\s.*?\)\s/gi, '');
  444.         }
  445.     }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement