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

Commit 4c16278

Browse files
authored
Merge pull request #294 from IDotD/idrinth
#196 + #290
2 parents 955e2a7 + e7ed19f commit 4c16278

3 files changed

Lines changed: 103 additions & 77 deletions

File tree

src/mods/names.js

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ idrinth.names = {
1212
*/
1313
guilds: { },
1414
/**
15-
* @type {Number}
15+
* @type {Boolean}
1616
*/
17-
counter: 0,
17+
initialized: false,
1818
/**
1919
*
2020
* @param {HTMLElement} element
@@ -65,49 +65,16 @@ idrinth.names = {
6565
importNames
6666
);
6767
};
68-
/**
69-
* adds names from elements on the page
70-
* @returns {undefined}
71-
*/
72-
var add = function () {
73-
/**
74-
*
75-
* @param {HTMLElement} element
76-
* @returns {undefined}
77-
*/
78-
var processName = function ( element ) {
79-
var name = '';
80-
try {
81-
name = idrinth.names.parse ( element );
82-
} catch ( e ) {
83-
return;
84-
}
85-
if ( !name ) {
86-
return;
87-
}
88-
if ( !element.getAttribute ( 'data-idrinth-parsed' ) && idrinth.ui.childOf ( element, 'chat_message_window' ) ) {
89-
element.setAttribute ( 'data-idrinth-parsed', '1' );
90-
}
91-
if ( !idrinth.names.users[name.toLowerCase ()] && name.length > 0 ) {
92-
idrinth.names.users[name.toLowerCase ()] = { };
93-
idrinth.core.ajax.runHome ( 'users-service/add/' + encodeURIComponent ( name ) + '/' );
94-
}
95-
};
96-
var el = document.getElementsByClassName ( 'username' );
97-
for (var count = el.length - 1; count >= 0; count--) {
98-
processName ( el[count] );
99-
}
100-
};
10168
try {
102-
if ( idrinth.names.counter === 0 || Object.keys ( idrinth.names.users ).length === 0 ) {
103-
load ( Object.keys ( idrinth.names.classes ).length === 0 ? 'init/' : 'get/' );
104-
} else if ( Object.keys ( idrinth.names.users ).length > 0 ) {
105-
add ();
69+
if ( idrinth.names.initialized ) {
70+
load ( 'get/' );
71+
} else {
72+
load ( 'init/' );
10673
}
10774
} catch ( e ) {
10875
idrinth.core.log ( e );
10976
}
110-
idrinth.names.counter = ( idrinth.names.counter + 1 ) % 300;
77+
idrinth.names.initialized = true;
11178
},
11279
/**
11380
* initialises the module
@@ -258,7 +225,8 @@ idrinth.names = {
258225
};
259226
if ( idrinth.platform === 'kongregate' ) {
260227
idrinth.core.multibind.add ( 'mouseover', '.chat_message_window .username', showTooltip );
261-
idrinth.core.timeouts.add ( 'names', idrinth.names.run, 6666, -1 );
228+
idrinth.core.timeouts.add ( 'names', idrinth.names.run, 300000, -1 );
229+
idrinth.core.timeouts.add ( 'names-init', idrinth.names.run, 1 );
262230
build ();
263231
}
264232
}

src/mods/observer.js

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,85 @@ idrinth.observer = {
1212
*/
1313
handle: function ( mutations, isPrivate ) {
1414
/**
15-
*
15+
*
1616
* @param {HTMLElement} element
1717
* @returns {undefined}
1818
*/
19-
var handleLink = function ( element ) {
20-
var href = element.getAttribute ( 'href' );
21-
if ( href && href.match ( /action_type=raidhelp/ ) ) {
22-
var hash = '';
23-
var id = '';
24-
href = href.replace ( /^.*\?/, '' );
25-
var parts = href.split ( "&" );
26-
for (var count = 0; count < parts.length; count++) {
27-
if ( parts[count].match ( 'raid_id=' ) ) {
28-
id = parts[count].split ( '=' )[1];
29-
} else if ( parts[count].match ( 'hash=' ) ) {
30-
hash = parts[count].split ( '=' )[1];
31-
} else if ( parts[count].match ( 'serverid=2' ) && !idrinth.settings.get ( "world" ) ) {
32-
return;
33-
} else if ( !parts[count].match ( 'server_id=2' ) && idrinth.settings.get ( "world" ) ) {
19+
var checkLinks = function ( node ) {
20+
/**
21+
*
22+
* @param {HTMLElement} element
23+
* @returns {undefined}
24+
*/
25+
var handleLink = function ( element ) {
26+
var href = element.getAttribute ( 'href' );
27+
if ( href && href.match ( /action_type=raidhelp/ ) ) {
28+
var hash = '';
29+
var id = '';
30+
href = href.replace ( /^.*\?/, '' );
31+
var parts = href.split ( "&" );
32+
for (var count = 0; count < parts.length; count++) {
33+
if ( parts[count].match ( 'raid_id=' ) ) {
34+
id = parts[count].split ( '=' )[1];
35+
} else if ( parts[count].match ( 'hash=' ) ) {
36+
hash = parts[count].split ( '=' )[1];
37+
} else if ( parts[count].match ( 'serverid=2' ) && !idrinth.settings.get ( "world" ) ) {
38+
return;
39+
} else if ( !parts[count].match ( 'server_id=2' ) && idrinth.settings.get ( "world" ) ) {
40+
return;
41+
}
42+
}
43+
if ( !id || !hash ) {
3444
return;
3545
}
46+
idrinth.raids.private[id] = hash;
47+
idrinth.core.ajax.runHome ( 'get-raid-service/' + id + '/' + hash + '/' );
48+
}
49+
};
50+
if ( node.tagName === 'A' || node.tagName === 'a' ) {
51+
handleLink ( node );
52+
} else {
53+
var elements = node.getElementsByTagName ( 'a' );
54+
for (var count = 0; count < elements.length; count++) {
55+
handleLink ( elements[count] );
56+
}
57+
}
58+
};
59+
/**
60+
*
61+
* @param {HTMLElement} element
62+
* @returns {undefined}
63+
*/
64+
var checkNames = function ( node ) {
65+
/**
66+
*
67+
* @param {HTMLElement} element
68+
* @returns {undefined}
69+
*/
70+
var processName = function ( element ) {
71+
var name = '';
72+
try {
73+
name = idrinth.names.parse ( element );
74+
} catch ( e ) {
75+
return;
3676
}
37-
if ( !id || !hash ) {
77+
if ( !name ) {
3878
return;
3979
}
40-
idrinth.raids.private[id] = hash;
41-
idrinth.core.ajax.runHome ( 'get-raid-service/' + id + '/' + hash + '/' );
80+
if ( !idrinth.names.users[name.toLowerCase ()] && name.length > 0 ) {
81+
idrinth.names.users[name.toLowerCase ()] = { };
82+
idrinth.core.ajax.runHome ( 'users-service/add/' + encodeURIComponent ( name ) + '/' );
83+
}
84+
};
85+
var elements = node.getElementsByClassName ( 'username' );
86+
for (var count = elements.length - 1; count >= 0; count--) {
87+
processName ( elements[count] );
4288
}
4389
};
4490
mutations.forEach ( function ( mutation ) {
4591
mutation.addedNodes.forEach ( function ( node ) {
46-
if ( node.tagName === 'A' || node.tagName === 'a' ) {
47-
handleLink ( node );
48-
} else {
49-
var elements = node.getElementsByTagName ( 'a' );
50-
for (var count = 0; count < elements.length; count++) {
51-
handleLink ( elements[count] );
52-
}
53-
}
92+
checkLinks ( node );
93+
checkNames ( node );
5494
} );
5595
} );
5696
},
@@ -62,12 +102,12 @@ idrinth.observer = {
62102
if ( idrinth.platform !== 'kongregate' ) {
63103
return;
64104
}
65-
if(
66-
!document.getElementById ( "chat_rooms_container" ) ||
67-
!document.getElementById ( "chat_rooms_container" ).children[1] ||
68-
!document.getElementById ( "chat_rooms_container" ).children[1].children[2]
69-
) {
70-
idrinth.core.timeouts.add ('observer',idrinth.observer.start,500,1);
105+
if (
106+
!document.getElementById ( "chat_rooms_container" ) ||
107+
!document.getElementById ( "chat_rooms_container" ).children[1] ||
108+
!document.getElementById ( "chat_rooms_container" ).children[1].children[2]
109+
) {
110+
idrinth.core.timeouts.add ( 'observer', idrinth.observer.start, 500, 1 );
71111
return;
72112
}
73113
idrinth.observer.list.chat = new MutationObserver ( function ( mutations ) {

src/mods/ui.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ idrinth.ui = {
686686
rType: '#input',
687687
type: 'checkbox',
688688
label: "setting.enableFavRequest"
689-
},{
689+
}, {
690690
name: 'raidWhitelist',
691691
rType: '#input',
692692
type: 'checkbox',
@@ -1088,11 +1088,11 @@ idrinth.ui = {
10881088
children: children
10891089
} );
10901090
idrinth.ui.base.appendChild ( idrinth.ui.controls );
1091-
document.getElementById ( 'idrinth-favs' ).setAttribute ( 'onkeyup', 'this.value=this.value.replace(/[^a-f0-9,]/g,\'\')' );
1091+
document.getElementById ( 'idrinth-favs' ).setAttribute ( 'onkeyup', 'idrinth.ui.replaceInValue(this);' );
10921092
};
10931093
idrinth.ui.base = document.createElement ( 'div' );
10941094
idrinth.ui.base.setAttribute ( 'id', 'idotd-base' );
1095-
document.getElementsByTagName ( 'body' )[0].appendChild ( idrinth.ui.base )
1095+
document.getElementsByTagName ( 'body' )[0].appendChild ( idrinth.ui.base );
10961096
document.getElementsByTagName ( 'head' )[0].appendChild ( idrinth.ui.buildElement ( {
10971097
type: 'link',
10981098
attributes: [ {
@@ -1104,5 +1104,23 @@ idrinth.ui = {
11041104
} ]
11051105
} ) );
11061106
build ();
1107+
},
1108+
/**
1109+
*
1110+
* @param {HTMLElement} element
1111+
* @returns {undefined}
1112+
*/
1113+
replaceInValue: function ( element ) {
1114+
var pos = element.selectionStart;
1115+
var part = element.value.substr ( 0, pos + 1 );
1116+
var pre = part.length;
1117+
part = part.replace ( /[^a-f0-9,]/g, '' );
1118+
part = part.replace ( /,{2,}/g, ',' );
1119+
pos = pos + part.length - pre;
1120+
element.value = element.value.replace ( /[^a-f0-9,]/g, '' );
1121+
element.value = element.value.replace ( /,{2,}/g, ',' );
1122+
pos = Math.min ( pos, element.value.length );
1123+
pos = Math.max ( pos, 0 );
1124+
element.setSelectionRange ( pos, pos );
11071125
}
11081126
};

0 commit comments

Comments
 (0)