Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #294 from IDotD/idrinth
Browse files Browse the repository at this point in the history
  • Loading branch information
w20k authored Mar 24, 2017
2 parents 955e2a7 + e7ed19f commit 4c16278
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 77 deletions.
50 changes: 9 additions & 41 deletions src/mods/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ idrinth.names = {
*/
guilds: { },
/**
* @type {Number}
* @type {Boolean}
*/
counter: 0,
initialized: false,
/**
*
* @param {HTMLElement} element
Expand Down Expand Up @@ -65,49 +65,16 @@ idrinth.names = {
importNames
);
};
/**
* adds names from elements on the page
* @returns {undefined}
*/
var add = function () {
/**
*
* @param {HTMLElement} element
* @returns {undefined}
*/
var processName = function ( element ) {
var name = '';
try {
name = idrinth.names.parse ( element );
} catch ( e ) {
return;
}
if ( !name ) {
return;
}
if ( !element.getAttribute ( 'data-idrinth-parsed' ) && idrinth.ui.childOf ( element, 'chat_message_window' ) ) {
element.setAttribute ( 'data-idrinth-parsed', '1' );
}
if ( !idrinth.names.users[name.toLowerCase ()] && name.length > 0 ) {
idrinth.names.users[name.toLowerCase ()] = { };
idrinth.core.ajax.runHome ( 'users-service/add/' + encodeURIComponent ( name ) + '/' );
}
};
var el = document.getElementsByClassName ( 'username' );
for (var count = el.length - 1; count >= 0; count--) {
processName ( el[count] );
}
};
try {
if ( idrinth.names.counter === 0 || Object.keys ( idrinth.names.users ).length === 0 ) {
load ( Object.keys ( idrinth.names.classes ).length === 0 ? 'init/' : 'get/' );
} else if ( Object.keys ( idrinth.names.users ).length > 0 ) {
add ();
if ( idrinth.names.initialized ) {
load ( 'get/' );
} else {
load ( 'init/' );
}
} catch ( e ) {
idrinth.core.log ( e );
}
idrinth.names.counter = ( idrinth.names.counter + 1 ) % 300;
idrinth.names.initialized = true;
},
/**
* initialises the module
Expand Down Expand Up @@ -258,7 +225,8 @@ idrinth.names = {
};
if ( idrinth.platform === 'kongregate' ) {
idrinth.core.multibind.add ( 'mouseover', '.chat_message_window .username', showTooltip );
idrinth.core.timeouts.add ( 'names', idrinth.names.run, 6666, -1 );
idrinth.core.timeouts.add ( 'names', idrinth.names.run, 300000, -1 );
idrinth.core.timeouts.add ( 'names-init', idrinth.names.run, 1 );
build ();
}
}
Expand Down
106 changes: 73 additions & 33 deletions src/mods/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,85 @@ idrinth.observer = {
*/
handle: function ( mutations, isPrivate ) {
/**
*
*
* @param {HTMLElement} element
* @returns {undefined}
*/
var handleLink = function ( element ) {
var href = element.getAttribute ( 'href' );
if ( href && href.match ( /action_type=raidhelp/ ) ) {
var hash = '';
var id = '';
href = href.replace ( /^.*\?/, '' );
var parts = href.split ( "&" );
for (var count = 0; count < parts.length; count++) {
if ( parts[count].match ( 'raid_id=' ) ) {
id = parts[count].split ( '=' )[1];
} else if ( parts[count].match ( 'hash=' ) ) {
hash = parts[count].split ( '=' )[1];
} else if ( parts[count].match ( 'serverid=2' ) && !idrinth.settings.get ( "world" ) ) {
return;
} else if ( !parts[count].match ( 'server_id=2' ) && idrinth.settings.get ( "world" ) ) {
var checkLinks = function ( node ) {
/**
*
* @param {HTMLElement} element
* @returns {undefined}
*/
var handleLink = function ( element ) {
var href = element.getAttribute ( 'href' );
if ( href && href.match ( /action_type=raidhelp/ ) ) {
var hash = '';
var id = '';
href = href.replace ( /^.*\?/, '' );
var parts = href.split ( "&" );
for (var count = 0; count < parts.length; count++) {
if ( parts[count].match ( 'raid_id=' ) ) {
id = parts[count].split ( '=' )[1];
} else if ( parts[count].match ( 'hash=' ) ) {
hash = parts[count].split ( '=' )[1];
} else if ( parts[count].match ( 'serverid=2' ) && !idrinth.settings.get ( "world" ) ) {
return;
} else if ( !parts[count].match ( 'server_id=2' ) && idrinth.settings.get ( "world" ) ) {
return;
}
}
if ( !id || !hash ) {
return;
}
idrinth.raids.private[id] = hash;
idrinth.core.ajax.runHome ( 'get-raid-service/' + id + '/' + hash + '/' );
}
};
if ( node.tagName === 'A' || node.tagName === 'a' ) {
handleLink ( node );
} else {
var elements = node.getElementsByTagName ( 'a' );
for (var count = 0; count < elements.length; count++) {
handleLink ( elements[count] );
}
}
};
/**
*
* @param {HTMLElement} element
* @returns {undefined}
*/
var checkNames = function ( node ) {
/**
*
* @param {HTMLElement} element
* @returns {undefined}
*/
var processName = function ( element ) {
var name = '';
try {
name = idrinth.names.parse ( element );
} catch ( e ) {
return;
}
if ( !id || !hash ) {
if ( !name ) {
return;
}
idrinth.raids.private[id] = hash;
idrinth.core.ajax.runHome ( 'get-raid-service/' + id + '/' + hash + '/' );
if ( !idrinth.names.users[name.toLowerCase ()] && name.length > 0 ) {
idrinth.names.users[name.toLowerCase ()] = { };
idrinth.core.ajax.runHome ( 'users-service/add/' + encodeURIComponent ( name ) + '/' );
}
};
var elements = node.getElementsByClassName ( 'username' );
for (var count = elements.length - 1; count >= 0; count--) {
processName ( elements[count] );
}
};
mutations.forEach ( function ( mutation ) {
mutation.addedNodes.forEach ( function ( node ) {
if ( node.tagName === 'A' || node.tagName === 'a' ) {
handleLink ( node );
} else {
var elements = node.getElementsByTagName ( 'a' );
for (var count = 0; count < elements.length; count++) {
handleLink ( elements[count] );
}
}
checkLinks ( node );
checkNames ( node );
} );
} );
},
Expand All @@ -62,12 +102,12 @@ idrinth.observer = {
if ( idrinth.platform !== 'kongregate' ) {
return;
}
if(
!document.getElementById ( "chat_rooms_container" ) ||
!document.getElementById ( "chat_rooms_container" ).children[1] ||
!document.getElementById ( "chat_rooms_container" ).children[1].children[2]
) {
idrinth.core.timeouts.add ('observer',idrinth.observer.start,500,1);
if (
!document.getElementById ( "chat_rooms_container" ) ||
!document.getElementById ( "chat_rooms_container" ).children[1] ||
!document.getElementById ( "chat_rooms_container" ).children[1].children[2]
) {
idrinth.core.timeouts.add ( 'observer', idrinth.observer.start, 500, 1 );
return;
}
idrinth.observer.list.chat = new MutationObserver ( function ( mutations ) {
Expand Down
24 changes: 21 additions & 3 deletions src/mods/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ idrinth.ui = {
rType: '#input',
type: 'checkbox',
label: "setting.enableFavRequest"
},{
}, {
name: 'raidWhitelist',
rType: '#input',
type: 'checkbox',
Expand Down Expand Up @@ -1088,11 +1088,11 @@ idrinth.ui = {
children: children
} );
idrinth.ui.base.appendChild ( idrinth.ui.controls );
document.getElementById ( 'idrinth-favs' ).setAttribute ( 'onkeyup', 'this.value=this.value.replace(/[^a-f0-9,]/g,\'\')' );
document.getElementById ( 'idrinth-favs' ).setAttribute ( 'onkeyup', 'idrinth.ui.replaceInValue(this);' );
};
idrinth.ui.base = document.createElement ( 'div' );
idrinth.ui.base.setAttribute ( 'id', 'idotd-base' );
document.getElementsByTagName ( 'body' )[0].appendChild ( idrinth.ui.base )
document.getElementsByTagName ( 'body' )[0].appendChild ( idrinth.ui.base );
document.getElementsByTagName ( 'head' )[0].appendChild ( idrinth.ui.buildElement ( {
type: 'link',
attributes: [ {
Expand All @@ -1104,5 +1104,23 @@ idrinth.ui = {
} ]
} ) );
build ();
},
/**
*
* @param {HTMLElement} element
* @returns {undefined}
*/
replaceInValue: function ( element ) {
var pos = element.selectionStart;
var part = element.value.substr ( 0, pos + 1 );
var pre = part.length;
part = part.replace ( /[^a-f0-9,]/g, '' );
part = part.replace ( /,{2,}/g, ',' );
pos = pos + part.length - pre;
element.value = element.value.replace ( /[^a-f0-9,]/g, '' );
element.value = element.value.replace ( /,{2,}/g, ',' );
pos = Math.min ( pos, element.value.length );
pos = Math.max ( pos, 0 );
element.setSelectionRange ( pos, pos );
}
};

0 comments on commit 4c16278

Please sign in to comment.