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

Commit 974e46e

Browse files
committed
simplifications of observer
fixes #299
1 parent 3a68c1c commit 974e46e

1 file changed

Lines changed: 34 additions & 18 deletions

File tree

src/mods/observer.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,44 @@ idrinth.observer = {
2424
*/
2525
var handleLink = function ( element ) {
2626
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 ( "&" );
27+
/**
28+
*
29+
* @param {Array} parts
30+
* @param {string} prefix
31+
* @returns {null|string}
32+
*/
33+
var getData = function(parts,prefix) {
3234
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;
35+
if ( parts[count].match ( prefix+'=' ) ) {
36+
return parts[count].split ( '=' )[1];
4137
}
4238
}
43-
if ( !id || !hash ) {
44-
return;
45-
}
46-
idrinth.raids.private[id] = hash;
47-
idrinth.core.ajax.runHome ( 'get-raid-service/' + id + '/' + hash + '/' );
39+
return null;
40+
};
41+
/**
42+
*
43+
* @param {string} href
44+
* @param {Boolean} isWorld
45+
* @returns {Boolean}
46+
*/
47+
var correctServer = function(href, isWorld) {
48+
return (!href.match ( 'serverid=2' )) === !isWorld;
49+
};
50+
if(!href || !href.match ( /action_type=raidhelp/ )) {
51+
return;
52+
}
53+
href = href.replace ( /^.*\?/, '' );
54+
if ( !correctServer(href, idrinth.settings.get ( "world" )) ) {
55+
return;
56+
}
57+
var parts = href.split ( "&" );
58+
var id = getData(parts, 'raid_id');
59+
var hash = getData(parts, 'hash');
60+
if ( !id || !hash ) {
61+
return;
4862
}
63+
idrinth.raids.private[id] = hash;
64+
idrinth.core.ajax.runHome ( 'get-raid-service/' + id + '/' + hash + '/' );
4965
};
5066
if ( node.tagName === 'A' || node.tagName === 'a' ) {
5167
handleLink ( node );

0 commit comments

Comments
 (0)