Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name IP.Chat Pings + Highlight
- // @namespace Makaze
- // @include *
- // @grant none
- // @version 2.2.0.1
- // ==/UserScript==
- var titlePing = true, // Show pings in the page title
- pingHighlight = true, // Highlight pinged words and messages
- pingSound = new Audio('https://dl.dropboxusercontent.com/u/45569424/Saved_FE7.mp3'), // The sound file
- audiblePing,
- hasFocus = true,
- MakazeScriptStyles = document.createElement('style'),
- pingMenu = document.createElement('div'),
- menuButton = document.createElement('a'),
- IPChatMenuItems = document.createElement('div'),
- styleElem,
- defaultStyle =
- 'font-weight: bolder;\n' +
- 'font-size: 120%;\n' +
- 'color: #f24;\n' +
- 'text-shadow: 0px 1px #222;',
- opts,
- pingStyle,
- pings,
- namePings,
- userName;
- // Global variable constructor
- function GlobalVariable(variable) {
- var output;
- this.input = variable;
- this.receiveEvent = function(event) {
- output = event.detail;
- }
- document.addEventListener('getGlobalVar_' + this.input, this.receiveEvent);
- this.dispatch = function() {
- document.dispatchEvent(
- new CustomEvent('getGlobalVar_' + this.input, {
- detail: global
- })
- );
- }
- this.newDispatch = function(global) {
- var getGlobalScript = document.createElement('script');
- getGlobalScript.type = 'text/javascript';
- getGlobalScript.id = 'getGlobalScript';
- getGlobalScript.appendChild(
- document.createTextNode(
- 'var dispatch = '
- + this.dispatch.toString().replace(/global/g, global).replace(/this\.input/, '\'' + global + '\'') + ';'
- +' \n\ndispatch();'
- + '\n\ndocument.getElementById(\'getGlobalScript\').remove();'
- )
- );
- (document.body || document.documentElement).appendChild(getGlobalScript);
- }
- this.get = function() {
- this.newDispatch(variable);
- return output;
- }
- }
- // End global variable constructor
- function fade(elem, type, speed) {
- // Default values:
- switch (arguments.length) {
- case 1:
- type = 'toggle';
- case 2:
- speed = 300;
- break;
- }
- switch (type) {
- case 'in':
- elem.style.display = '';
- setTimeout(function() {
- elem.style.transition = 'all ' + speed + 'ms ease-in-out';
- elem.style.opacity = 0;
- elem.style.opacity = 1;
- }, 1);
- break;
- case 'out':
- elem.style.transition = 'all ' + speed + 'ms ease-in-out';
- elem.style.opacity = 1;
- elem.style.opacity = 0;
- setTimeout(function() {
- elem.style.display = 'none';
- }, speed);
- break;
- case 'toggle':
- default:
- if (elem.style.display === 'none') {
- elem.style.display = '';
- setTimeout(function() {
- elem.style.transition = 'all' + speed + 'ms ease-in-out';
- elem.style.opacity = 0;
- elem.style.opacity = 1;
- }, 1);
- } else {
- elem.style.transition = 'all ' + speed + 'ms ease-in-out';
- elem.style.opacity = 1;
- elem.style.opacity = 0;
- setTimeout(function() {
- elem.style.display = 'none';
- }, speed);
- }
- }
- }
- function empty(elem) {
- while (elem.hasChildNodes()) {
- elem.removeChild(elem.lastChild);
- }
- }
- function onEachMatch(str, regex, callback, nestedCall) {
- var matches = [];
- while ((match = regex.exec(str)) != null) {
- matches.push({'text': match, 'index': match.index});
- }
- callback(str, matches);
- }
- if (document.body.id === 'ipboard_body' && document.getElementById('storage_chatroom') != null) {
- userName = new GlobalVariable('userName').get();
- opts = (localStorage.getItem('MakazeScriptOptions')) ? JSON.parse(localStorage.getItem('MakazeScriptOptions')) : {};
- pingStyle = (opts.hasOwnProperty('ipc_ping_style')) ? opts.ipc_ping_style : defaultStyle;
- pings = (opts.hasOwnProperty('ipc_pings')) ? opts.ipc_pings : [userName];
- namePings = (opts.hasOwnProperty('ipc_namePings')) ? opts.ipc_namePings :['Railgun', 'Blyverrn', 'Saethori', 'tuvarkz', 'Soulreaper', 'Makaze', 'Euklyd'];
- audiblePing = (opts.hasOwnProperty('ipc_audiblePing')) ? opts.ipc_audiblePing : true;
- // Styling
- if (document.getElementById('MakazeScriptStyles') == null) {
- MakazeScriptStyles.id = 'MakazeScriptStyles';
- MakazeScriptStyles.setAttribute('type', 'text/css');
- document.head.appendChild(MakazeScriptStyles);
- }
- styleElem = document.getElementById('MakazeScriptStyles');
- if (styleElem.hasChildNodes()) {
- styleElem.childNodes[0].nodeValue += '\n\n';
- } else {
- styleElem.appendChild(document.createTextNode(''));
- }
- if (!styleElem.childNodes[0].nodeValue.match(/\.MakazeScriptMenu/gi)) {
- 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';
- }
- styleElem.childNodes[0].nodeValue +=
- '#pingsettings {\n' +
- 'margin-top: -122px;\n' +
- 'margin-left: -168px;\n' +
- '}\n\n' +
- '#pingsettings textarea {\n' +
- 'width: 144px;\n' +
- 'height: 144px;\n' +
- 'font-family: Consolas, Ubuntu Mono, sans-serif;\n' +
- 'font-size: 10px;\n' +
- 'color: #333;\n' +
- 'padding: 3px;\n' +
- '}\n\n' +
- '#pingsettings .td-centered {\n' +
- 'text-align: center;\n' +
- '}\n\n' +
- '#pingsettings input {\n' +
- 'vertical-align: middle;\n' +
- '}';
- // Menu creation
- var pingMenu_table = document.createElement('table'),
- pingMenu_tablehead = document.createElement('thead'),
- pingMenu_tablehead_row = document.createElement('tr'),
- pingMenu_tablehead_firstcell = document.createElement('td'),
- pingMenu_tablehead_firstcell_Txt = document.createTextNode('Style'),
- pingMenu_tablehead_lastcell = document.createElement('td'),
- pingMenu_tablehead_lastcell_Txt = document.createTextNode('Pings'),
- pingMenu_tablebody = document.createElement('tbody'),
- pingMenu_tablebody_firstrow = document.createElement('tr'),
- pingMenu_tablebody_firstrow_firstcell = document.createElement('td'),
- pingMenu_tablebody_firstrow_firstcell_styleinput = document.createElement('textarea'),
- pingMenu_tablebody_firstrow_lastcell = document.createElement('td'),
- pingMenu_tablebody_firstrow_lastcell_pinginput = document.createElement('textarea'),
- pingMenu_tablebody_lastrow = document.createElement('tr'),
- pingMenu_tablebody_lastrow_cell = document.createElement('td'),
- pingMenu_tablebody_lastrow_cell_checkbox = document.createElement('input'),
- pingMenu_tablebody_lastrow_cell_Txt = document.createTextNode(' Play sound when pinged'),
- pingMenu_menusave = document.createElement('div'),
- pingMenu_menusaveLink = document.createElement('a'),
- pingMenu_menusaveLink_Txt = document.createTextNode('Save');
- // Tablehead
- pingMenu_tablehead_firstcell.appendChild(pingMenu_tablehead_firstcell_Txt);
- pingMenu_tablehead_lastcell.appendChild(pingMenu_tablehead_lastcell_Txt);
- pingMenu_tablehead_row.appendChild(pingMenu_tablehead_firstcell);
- pingMenu_tablehead_row.appendChild(pingMenu_tablehead_lastcell);
- pingMenu_tablehead.appendChild(pingMenu_tablehead_row);
- // Tablebody
- // First row
- pingMenu_tablebody_firstrow_firstcell_styleinput.id = 'pingStyle';
- pingMenu_tablebody_firstrow_firstcell.appendChild(pingMenu_tablebody_firstrow_firstcell_styleinput);
- pingMenu_tablebody_firstrow_lastcell_pinginput.id = 'pingList';
- pingMenu_tablebody_firstrow_lastcell.appendChild(pingMenu_tablebody_firstrow_lastcell_pinginput);
- pingMenu_tablebody_firstrow.appendChild(pingMenu_tablebody_firstrow_firstcell);
- pingMenu_tablebody_firstrow.appendChild(pingMenu_tablebody_firstrow_lastcell);
- // Last row
- pingMenu_tablebody_lastrow_cell_checkbox.type = 'checkbox';
- pingMenu_tablebody_lastrow_cell_checkbox.id = 'audiblePingSwitch';
- pingMenu_tablebody_lastrow_cell.appendChild(pingMenu_tablebody_lastrow_cell_checkbox);
- pingMenu_tablebody_lastrow_cell.appendChild(pingMenu_tablebody_lastrow_cell_Txt);
- pingMenu_tablebody_lastrow.appendChild(pingMenu_tablebody_lastrow_cell);
- pingMenu_tablebody.appendChild(pingMenu_tablebody_firstrow);
- pingMenu_tablebody.appendChild(pingMenu_tablebody_lastrow);
- // Menu save
- pingMenu_menusaveLink.href = 'javascript:void(0)';
- pingMenu_menusaveLink.id = 'pingsettings_save';
- pingMenu_menusaveLink.appendChild(pingMenu_menusaveLink_Txt);
- pingMenu_menusaveLink.onclick = function() {
- opts = (localStorage.getItem('MakazeScriptOptions')) ? JSON.parse(localStorage.getItem('MakazeScriptOptions')) : {};
- if (styleElem.hasChildNodes()) {
- styleElem.childNodes[0].nodeValue += '\n\n';
- } else {
- styleElem.appendChild(document.createTextNode(''));
- }
- styleElem.childNodes[0].nodeValue = styleElem.childNodes[0].nodeValue.replace(/\.ipc_hilight\s{[^]+}/gi, '.ipc_hilight {\n' + document.getElementById('pingStyle').value + '\n' + '}');
- opts.ipc_ping_style = document.getElementById('pingStyle').value;
- opts.ipc_pings = document.getElementById('pingList').value.split('\n');
- // work on this:
- opts.ipc_namePings = document.getElementById('namePingList').value.split('\n');
- // /work
- opts.ipc_audiblePing = document.getElementById('audiblePingSwitch').checked;
- localStorage.setItem('MakazeScriptOptions', JSON.stringify(opts));
- fade(this.parentNode.parentNode, 'out');
- }
- pingMenu_menusave.className = 'menu-save';
- pingMenu_menusave.appendChild(pingMenu_menusaveLink);
- // Build it all
- pingMenu_table.appendChild(pingMenu_tablehead);
- pingMenu_table.appendChild(pingMenu_tablebody);
- pingMenu.id = 'pingsettings';
- pingMenu.className = 'MakazeScriptMenu';
- pingMenu.style.display = 'none';
- pingMenu.appendChild(pingMenu_table);
- pingMenu.appendChild(pingMenu_menusave);
- document.body.appendChild(pingMenu);
- document.getElementById('pingStyle').value = pingStyle;
- document.getElementById('pingList').value = pings.join('\n');
- document.getElementById('audiblePingSwitch').checked = audiblePing;
- // Button creation
- if (document.getElementById('IPChatMenuItems') == null) {
- IPChatMenuItems.id = 'IPChatMenuItems';
- IPChatMenuItems.setAttribute('style', 'text-align: right;');
- document.getElementById('chatters-online-wrap').nextSibling.nextSibling.getElementsByTagName('ul')[0].appendChild(IPChatMenuItems);
- }
- if (document.getElementById('IPChatMenuItems').hasChildNodes()) {
- document.getElementById('IPChatMenuItems').appendChild(document.createElement('br'));
- }
- menuButton.id = 'pingMenuButton';
- menuButton.className = 'ipsButton_secondary';
- menuButton.href = 'javascript:void(0)';
- menuButton.setAttribute('style', 'margin-top: 10px;');
- menuButton.appendChild(document.createTextNode('Ping Settings'));
- document.getElementById('IPChatMenuItems').appendChild(menuButton);
- document.getElementById('pingMenuButton').onclick = function() {
- fade(document.getElementById('pingsettings'));
- }
- if (pingHighlight) {
- if (styleElem.hasChildNodes()) {
- styleElem.childNodes[0].nodeValue += '\n\n';
- } else {
- styleElem.appendChild(document.createTextNode(''));
- }
- styleElem.childNodes[0].nodeValue +=
- '.ipc_highlight {\n' +
- pingStyle + '\n' +
- '}';
- }
- document.addEventListener('DOMNodeInserted', function(event) {
- var latestMessage,
- nameToCheck,
- textToCheck,
- nodeToCheck,
- nodeTextToCheck,
- opts = (localStorage.getItem('MakazeScriptOptions')) ? JSON.parse(localStorage.getItem('MakazeScriptOptions')) : {},
- pings = (opts.hasOwnProperty('ipc_pings')) ? opts.ipc_pings : [userName],
- // This is where names are changed:
- namePings = (opts.hasOwnProperty('ipc_namePings')) ? opts.ipc_namePings :['Railgun', 'Blyvern', 'Makaze', 'Saethori', 'Tuvarkz', 'Alm', 'Euklyd'],
- audiblePing = (opts.hasOwnProperty('ipc_audiblePing')) ? opts.ipc_audiblePing : true,
- ping,
- playSound;
- if (event.target.nodeType !== 1 || event.target.id !== 'storage_chatroom') {
- return false;
- }
- if (!pings.length) {
- return false;
- }
- latestMessage = event.target.parentNode.getElementsByTagName('div')[event.target.parentNode.getElementsByTagName('div').length - 1];
- if (!latestMessage.parentNode.className.match(/post/gi)) {
- return false;
- }
- if (latestMessage.parentNode.className.match(/chat-myown/gi)) {
- // return false;
- }
- // sometimes this is undefined, need to fix
- nameToCheck = latestMessage.parentNode.getElementsByTagName('label')[0].innerHTML;
- console.log(nameToCheck);
- textToCheck = latestMessage.innerHTML;
- console.log(textToCheck);
- if (titlePing) {
- if (!hasFocus) {
- for (i = 0; i < namePings.length; i++) {
- if (nameToCheck === namePings[i] && textToCheck === ' has entered the room') {
- namePing = new RegExp(namePings[i], "gi");
- if (document.title.match(/\(P:/gi)) {
- if (!document.title.match(namePing)) {
- document.title = document.title.replace(/\(P:\s(.*?)\)/gi, '(P: $1 ' + namePings[i] + ')');
- }
- } else {
- document.title = '(P: ' + namePings[i] + ') ' + document.title;
- }
- }
- }
- for (i = 0; i < pings.length; i++) {
- ping = new RegExp(pings[i], "gi");
- if (textToCheck.match(ping)) {
- if (document.title.match(/\(P:/gi)) {
- if (!document.title.match(ping)) {
- document.title = document.title.replace(/\(P:\s(.*?)\)/gi, '(P: $1 ' + pings[i] + ')');
- }
- } else {
- document.title = '(P: ' + pings[i] + ') ' + document.title;
- }
- }
- }
- }
- }
- if (pingHighlight) {
- for (i = 0; i < namePings.length; i++) {
- namePing = new RegExp('(' + 'has entered the room' + ')', "gi");
- if (nameToCheck === namePings[i] && textToCheck === ' has entered the room') {
- console.log('p-p-ping, bitch (name)');
- latestMessage.parentNode.className += ' chat-myown';
- latestMessage.innerHTML = latestMessage.innerHTML.replace(namePing, '<span class="ipc_highlight">$1</span>');
- }
- }
- for (i = 0; i < pings.length; i++) {
- ping = new RegExp('(' + pings[i] + ')', "gi");
- if (textToCheck.match(ping)) {
- latestMessage.parentNode.className += ' chat-myown';
- latestMessage.innerHTML = latestMessage.innerHTML.replace(ping, '<span class="ipc_highlight">$1</span>');
- }
- }
- }
- if (audiblePing) {
- playSound = false;
- for (i = 0; i < namePings.length; i++) {
- ping = new RegExp('(' + 'has entered the room' + ')', "gi");
- if (nameToCheck === namePings[i] && textToCheck === ' has entered the room') {
- playSound = true;
- }
- }
- for (i = 0; i < pings.length; i++) {
- ping = new RegExp('(' + pings[i] + ')', "gi");
- if (textToCheck.match(ping)) {
- playSound = true;
- }
- }
- if (playSound) {
- pingSound.play();
- }
- }
- });
- window.onblur = function() {
- hasFocus = false;
- }
- window.onfocus = function() {
- hasFocus = true;
- if (document.title.match(/\(P:/gi)) {
- document.title = document.title.replace(/\(P:\s.*?\)\s/gi, '');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement