diff --git a/source/core/source/arena.js b/source/core/source/arena.js index bd28e022..756a2873 100644 --- a/source/core/source/arena.js +++ b/source/core/source/arena.js @@ -75,25 +75,43 @@ var gca_arena = { }, // Show Simulator - show_simulator : function(){ - let link = document.createElement('a'); - link.className = "gca_arena-simulator-link"; - link.href = gca_links.get('gladiatus-simulator'); - link.setAttribute("target","_blank"); - document.getElementById('content').getElementsByTagName('article')[0].parentNode.insertBefore(link, document.getElementById('content').getElementsByTagName('article')[0]); - - let image = document.createElement('div'); - image.className = "gca_arena-simulator-img"; - link.appendChild(image); - - let text = document.createElement('div'); - text.textContent = "Before attacking, use the..."; - image.appendChild(text); + show_simulator: function() { + let content = document.getElementById('content'); + + // Check if the 'content' element and the first 'article' element exist + if (content) { + let article = content.getElementsByTagName('article')[0]; + + if (article && article.parentNode) { + // Create the simulator link + let link = document.createElement('a'); + link.className = "gca_arena-simulator-link"; + link.href = gca_links.get('gladiatus-simulator'); + link.setAttribute("target", "_blank"); + + // Insert the link before the article + article.parentNode.insertBefore(link, article); + + // Create the image container + let image = document.createElement('div'); + image.className = "gca_arena-simulator-img"; + link.appendChild(image); + + // Create and append the text + let text = document.createElement('div'); + text.textContent = "Before attacking, use the..."; + image.appendChild(text); + } + } }, - + // Overhaul Arena and Circus tables - overhaul_tables : function(){ - document.getElementById("arenaPage").classList.add("overhaul_tables"); + overhaul_tables: function() { + let arenaPage = document.getElementById("arenaPage"); + + if (arenaPage) { + arenaPage.classList.add("overhaul_tables"); + } }, // GCA Global Arena @@ -117,7 +135,21 @@ var gca_arena = { }, getInfo : function() { - let arena_rows = document.getElementById('content').getElementsByTagName('article')[0].getElementsByClassName('right')[0].getElementsByTagName('tr'); + // Check if 'content' exists + let content = document.getElementById('content'); + if (!content) return; + + // Check if there are any 'article' elements and get the first one + let article = content.getElementsByTagName('article')[0]; + if (!article) return; + + // Check if there are any elements with class 'right' and get the first one + let rightSection = article.getElementsByClassName('right')[0]; + if (!rightSection) return; + + // Check if there are any 'tr' elements + let arena_rows = rightSection.getElementsByTagName('tr'); + if (!arena_rows) return; this.info = {}; this.info.locale_position = arena_rows[0].getElementsByTagName('th')[0].textContent.trim(); @@ -129,18 +161,21 @@ var gca_arena = { this.info.guild_name = gca_data.section.get("guild", "name", "-").trim(); }, - create : function() { - let article = document.getElementById('content').getElementsByTagName('article')[0]; + create: function() { + let article = document.getElementById('content')?.getElementsByTagName('article')[0]; + + // Check if article exists before performing any operations + if (!article) return; - // Add br + // Add
element article.appendChild(document.createElement('br')); - + // Add header let header = document.createElement('h2'); header.className = "section-header global_arena_header"; header.textContent = gca_locale.get("arena", "global_arena_title") + ' ' + '(Crazy Addon)'; article.appendChild(header); - + // Add box let box = document.createElement('section'); box.id = "global_arena_box"; @@ -148,22 +183,22 @@ var gca_arena = { article.appendChild(box); this.box = box; - // Add text + // Add description let description = document.createElement('p'); - description.textContent = gca_locale.get("arena", "global_arena_description")+" "; + description.textContent = gca_locale.get("arena", "global_arena_description") + " "; description.style = "text-align: center;"; box.appendChild(description); - // Add link to highscore + // Add highscore link let highscore_link = document.createElement('a'); highscore_link.className = "awesome-button"; highscore_link.textContent = gca_locale.get("arena", "global_highscore") + ' 🔗'; highscore_link.style = "margin-bottom: 15px;padding: 2px 6px;margin-right: 20px;"; highscore_link.href = gca_links.get('addon-page') + "/global-arena.php"; - highscore_link.setAttribute("target","_blank"); + highscore_link.setAttribute("target", "_blank"); box.appendChild(highscore_link); - // Add button + // Add load button let load_btn = document.createElement('input'); load_btn.type = "button"; load_btn.className = "awesome-button"; @@ -176,23 +211,25 @@ var gca_arena = { box.appendChild(load_btn); this.load_btn = load_btn; - // Status + // Add status let status = document.createElement('p'); status.id = 'alert_box'; status.style.display = 'none'; box.appendChild(status); this.status = status; - - // Spinner + + // Add spinner let spinner = document.createElement('div'); spinner.id = 'spiner_box'; spinner.style.display = 'none'; box.appendChild(spinner); this.spinner = spinner; + let img = document.createElement('img'); img.src = gca_tools.img.cdn('img/ui/spinner.gif'); spinner.appendChild(img); - + + // Add rankings table let rankings_table = document.createElement("table"); rankings_table.width = "100%"; rankings_table.style.display = "none"; @@ -200,7 +237,8 @@ var gca_arena = { rankings_table.style.wordBreak = 'break-word'; box.appendChild(rankings_table); this.rankings_table = rankings_table; - + + // Add main table let table = document.createElement("table"); table.width = "100%"; table.style.border = "0px"; @@ -208,9 +246,9 @@ var gca_arena = { table.style.wordBreak = 'break-word'; box.appendChild(table); this.table = table; - - // if link opens global arena - if ( document.location.href.match("#global_arena_box") ){ + + // If link opens global arena + if (document.location.href.match("#global_arena_box")) { this.loadList(); header.scrollIntoView(); } diff --git a/source/core/source/global.js b/source/core/source/global.js index daaaf1aa..45a93159 100644 --- a/source/core/source/global.js +++ b/source/core/source/global.js @@ -575,10 +575,10 @@ var gca_global = { } // Get recovery rate - let recover_rate = tooltip[0][1][0].match(/\d+/); - if (!recover_rate) return; // Exit if recovery rate is not found - recover_rate = parseInt(recover_rate[0], 10); - recover_rate = (recover_rate + 100) / 100; + let recover_rate = tooltip?.[0]?.[1]?.[0]?.match(/\d+/); + if (!recover_rate) return; + recover_rate = parseInt(recover_rate[0], 10); + recover_rate = (recover_rate + 100) / 100; // Get points left to fill let point_max_elem = document.getElementById(type + 'points_value_pointmax'); diff --git a/source/core/source/guild.bank.js b/source/core/source/guild.bank.js index c432f912..91f3f9e9 100644 --- a/source/core/source/guild.bank.js +++ b/source/core/source/guild.bank.js @@ -41,29 +41,41 @@ var gca_guild_bank = { ); }, - // Bank Improve - donateLayout : { - improve : function(self){ - // Get wrappers - var wrapper = document.getElementById("content").getElementsByTagName("article"); - if(!wrapper) return; - - // Improve gold in bank - this.goldInBank(self, wrapper[0].getElementsByTagName("table")[0]); - // Improve donate all my gold - this.insertAllPlayersGold(self, wrapper[0].getElementsByTagName("table")[1]); - - // Set gold input to number type - var input = wrapper[0].getElementsByTagName("table")[1].getElementsByTagName("input")[0]; - input.style.width = "128px"; - input.type = "number"; - - // Change donations book link to show the detailed donations of only 1 day - // this speeds up loading and off-loads the server - let headerTabs = document.getElementById('mainnav').getElementsByTagName('a'); - if(!headerTabs || headerTabs.length < 2) return; - headerTabs[1].href += '&l2=2'; - }, + // Bank Improve + donateLayout: { + improve: function (self) { + // Get wrappers + var wrapper = document.getElementById("content")?.getElementsByTagName("article"); + if (!wrapper || wrapper.length === 0) return; + + // Ensure the first article contains tables + var tables = wrapper[0]?.getElementsByTagName("table"); + if (!tables || tables.length === 0) return; + + // Improve gold in bank (ensure first table exists) + if (tables[0]) { + this.goldInBank(self, tables[0]); + } + + // Improve donate all my gold (ensure second table exists) + if (tables[1]) { + this.insertAllPlayersGold(self, tables[1]); + + // Set gold input to number type (ensure first input in second table exists) + var input = tables[1]?.getElementsByTagName("input")[0]; + if (input) { + input.style.width = "128px"; + input.type = "number"; + } + } + + // Change donations book link to show the detailed donations of only 1 day + // Ensure the navigation exists and has enough tabs + let headerTabs = document.getElementById('mainnav')?.getElementsByTagName('a'); + if (headerTabs && headerTabs.length > 1) { + headerTabs[1].href += '&l2=2'; + } + }, // Improve gold in bank goldInBank : function(self, wrapper){ diff --git a/source/core/source/guild.jail.js b/source/core/source/guild.jail.js index 712c06ac..1b5d8633 100644 --- a/source/core/source/guild.jail.js +++ b/source/core/source/guild.jail.js @@ -65,8 +65,27 @@ var gca_guild_jail = { var jailTableRows = document.getElementById('content').getElementsByTagName('table')[0].getElementsByTagName('tr').length; // If you have the rights to free monsters - var admin = ( document.getElementById('content').getElementsByTagName('table')[0].getElementsByTagName('tr')[1].getElementsByTagName('td')[3].getElementsByTagName('a')[1] )?true:false; - if(!admin) document.getElementById('content').className('noJailRights'); + let contentElement = document.getElementById('content'); + let admin = false; + + if (contentElement) { + let table = contentElement.getElementsByTagName('table')[0]; + if (table) { + let tr = table.getElementsByTagName('tr')[1]; + if (tr) { + let td = tr.getElementsByTagName('td')[3]; + if (td) { + let a = td.getElementsByTagName('a')[1]; + admin = !!a; // Set admin to true if the anchor element exists + } + } + } + } + + // If not an admin, add the 'noJailRights' class + if (!admin) { + contentElement?.classList.add('noJailRights'); + } // For every prisoner row for(var i=1;i 0 && recipes[0].getElementsByTagName('td').length > 4) { + recipes[0].getElementsByTagName('td')[4].style = 'width: 15%;'; + } // For each recipe for (var i = recipes.length - 1; i >= 1; i--) { @@ -139,9 +145,13 @@ var gca_guild_library = { container.id = 'gca-library-container'; // Get guild gold - var guildGold = container.parentNode.getElementsByClassName('span_right')[1].textContent; + let guildGoldElement = container?.parentNode?.getElementsByClassName('span_right')[1]; + // Ensure the element exists and has text content, otherwise set a default value + let guildGold = guildGoldElement ? guildGoldElement.textContent : '0'; + // Parse guild gold guildGold = gca_tools.strings.parseGold(guildGold); - if(guildGold === null) return; + // If guildGold is still null after parsing, return + if (guildGold === null) return; // Get recipes var recipes = container.getElementsByTagName('tr'); diff --git a/source/core/source/guild.storage.js b/source/core/source/guild.storage.js index 49087502..be77ff78 100644 --- a/source/core/source/guild.storage.js +++ b/source/core/source/guild.storage.js @@ -36,23 +36,26 @@ var gca_guild_storage = { // Setting Link gca_tools.create.settingsLink("guild"); }, - + // Items Shadow Inject - itemShadow : { - inject : function(){ - this.dollItems(); - }, - - // Add shadow to doll items - dollItems : function(){ - // Get doll items - var items = document.getElementById("char").getElementsByClassName("ui-draggable"); - - // Add shadow to each item - for(var i = items.length - 1; i >= 0; i--){ - gca_tools.item.shadow.add(items[i]); - } - } + itemShadow: { + inject: function() { + this.dollItems(); + }, + + // Add shadow to doll items + dollItems: function() { + // Get doll items safely + let charElement = document.getElementById("char"); + if (!charElement) return; // Exit if 'char' element doesn't exist + + let items = charElement.getElementsByClassName("ui-draggable"); + + // Add shadow to each item + for (let i = items.length - 1; i >= 0; i--) { + gca_tools.item.shadow.add(items[i]); + } + } }, rememberLastTab : { @@ -150,35 +153,33 @@ var gca_guild_storage = { }, // Double click sell/buy - doubleClickActions : { - init : function(){ - // Apply item events - this.apply(); - - // Add event - gca_tools.event.bag.onBagOpen(() => { - this.apply(); - }); - - // If bag not already loaded - if (document.getElementById("inv").className.match("unavailable")) { - // Wait first bag - gca_tools.event.bag.waitBag(() => { - this.apply(); - }); - } - - // On item move - gca_tools.event.request.onAjaxResponse((data) => { - if (data?.data?.to?.data && data?.elem?.length === 1 && data?.elem[0]?.dataset?.hash) { - let item = document.querySelector(`.ui-draggable[data-hash="${data.elem[0].dataset.hash}"]`); - if (item && typeof(item.dataset.gcaFlag_doubleClickEvent) !== 'undefined') { - delete item.dataset.gcaFlag_doubleClickEvent; - } - this.apply(); - } - }); - }, + doubleClickActions: { + init: function() { + // Apply item events + this.apply(); + // Add event + gca_tools.event.bag.onBagOpen(() => { + this.apply(); + }); + // Check if 'inv' element exists before checking its className + let invElement = document.getElementById("inv"); + if (invElement && invElement.className.match("unavailable")) { + // Wait for the first bag + gca_tools.event.bag.waitBag(() => { + this.apply(); + }); + } + // On item move + gca_tools.event.request.onAjaxResponse((data) => { + if (data?.data?.to?.data && data?.elem?.length === 1 && data?.elem[0]?.dataset?.hash) { + let item = document.querySelector(`.ui-draggable[data-hash="${data.elem[0].dataset.hash}"]`); + if (item && typeof(item.dataset.gcaFlag_doubleClickEvent) !== 'undefined') { + delete item.dataset.gcaFlag_doubleClickEvent; + } + this.apply(); + } + }); + }, apply : function(){ this.applyOn(jQuery('#inv .ui-draggable')); this.applyOn(jQuery('#shop .ui-draggable')); diff --git a/source/core/source/markets.js b/source/core/source/markets.js index 02655377..e2f64ccb 100644 --- a/source/core/source/markets.js +++ b/source/core/source/markets.js @@ -81,27 +81,38 @@ var gca_markets = { }, // Trigger sell with {enter} - enterTriggerSell : function() { + enterTriggerSell: function() { jQuery(document).ready(function() { // Monitor key events jQuery(document).keyup(function(event) { - // Check if ENTER + // Check if ENTER is pressed if (event.keyCode === 13) { // Check if there is an item to sell - if ( document.getElementById("market_sell").getElementsByClassName("ui-draggable").length > 0 ){ + var marketSellElement = document.getElementById("market_sell"); + if (marketSellElement && marketSellElement.getElementsByClassName("ui-draggable").length > 0) { //console.log("Item in sell position found"); - let focus = document.activeElement.type - if ( focus != "textarea" && focus != "text") + let focus = document.activeElement.type; + if (focus != "textarea" && focus != "text") { document.getElementsByName("anbieten")[0].click(); //console.log("Not a text area:"+document.activeElement.type); + } } } }); - + + // Create the span element with text let span = document.createElement("span"); - span.textContent = "("+gca_locale.get("markets", "click_enter_to_sell")+")";//"(click enter to sell)"; - span.style = "font-size: 0.8em;font-style: italic;" - document.getElementById("market_sell_box").getElementsByTagName("section")[0].appendChild(span); + span.textContent = "(" + gca_locale.get("markets", "click_enter_to_sell") + ")"; //"(click enter to sell)"; + span.style = "font-size: 0.8em; font-style: italic;"; + + // Check if market_sell_box exists and append the span + var marketSellBox = document.getElementById("market_sell_box"); + if (marketSellBox) { + let section = marketSellBox.getElementsByTagName("section")[0]; + if (section) { + section.appendChild(span); + } + } }); }, @@ -226,16 +237,24 @@ var gca_markets = { }, // Show warnings on item selling - sellWarnings : { - - init : function() { - if (this.icons) return; - this.icons = {}; - - // Icon wrapper - this.icons.wrapper = document.createElement('div'); - this.icons.wrapper.className = 'gca-market-sell-warnings'; - document.getElementById('market_sell_box').getElementsByTagName('h2')[0].appendChild(this.icons.wrapper); + sellWarnings: { + init: function() { + if (this.icons) return; + this.icons = {}; + + // Icon wrapper + this.icons.wrapper = document.createElement('div'); + this.icons.wrapper.className = 'gca-market-sell-warnings'; + + // Get the market_sell_box element and check if it exists + var marketSellBox = document.getElementById('market_sell_box'); + if (marketSellBox) { + // Get the h2 element inside market_sell_box + var header = marketSellBox.getElementsByTagName('h2')[0]; + if (header) { + header.appendChild(this.icons.wrapper); + } + } // Soulbound icon this.icons.soulbound = document.createElement('span'); @@ -390,11 +409,17 @@ var gca_markets = { }, // 1g mode - oneGoldMode : function(){ - // Create mode switch - let wrapper = document.createElement('div'); - let fields = document.getElementById("market_sell_fields"); - fields.parentNode.insertBefore(wrapper, fields.nextSibling); + oneGoldMode: function() { + // Create mode switch + let wrapper = document.createElement('div'); + + // Get the market_sell_fields element + let fields = document.getElementById("market_sell_fields"); + + // Check if the fields element exists before inserting the wrapper + if (fields && fields.parentNode) { + fields.parentNode.insertBefore(wrapper, fields.nextSibling); + } let selected_mode = gca_data.section.get("cache", "last_sell_1g_mode", 0); @@ -580,22 +605,22 @@ var gca_markets = { }, // On double click item to select for selling - doubleClickToSelect : { - init : function(){ - // Add event - gca_tools.event.bag.onBagOpen(() => { - this.apply(); - }); + doubleClickToSelect: { + init: function() { + // Add event + gca_tools.event.bag.onBagOpen(() => { + this.apply(); + }); - // If bag not already loaded - if (document.getElementById('inv').className.match('unavailable')) { - // Wait first bag - gca_tools.event.bag.waitBag(() => { - this.apply(); - }); - } - else { - this.apply(); + // Check if 'inv' element exists and handle its className safely + let invElement = document.getElementById('inv'); + if (invElement && invElement.className.match('unavailable')) { + // Wait for the first bag + gca_tools.event.bag.waitBag(() => { + this.apply(); + }); + } else if (invElement) { + this.apply(); } }, apply : function(){ diff --git a/source/core/source/player.js b/source/core/source/player.js index a2b516e3..d1ff1455 100644 --- a/source/core/source/player.js +++ b/source/core/source/player.js @@ -66,25 +66,29 @@ var gca_player = { }, // Items Shadow Inject - itemShadow : { - inject : function(){ - this.dollItems(); - }, - - // Add shadow to doll items - dollItems : function(){ - // Get doll divs - var items = document.getElementById("char").getElementsByClassName("ui-droppable"); - - // Add shadow to each item - for(var i = items.length - 1; i >= 0; i--){ - // If item - if(items[i].className.match("item-i-")){ - gca_tools.item.shadow.add(items[i]); - } - } - - } + itemShadow: { + inject: function () { + this.dollItems(); + }, + + // Add shadow to doll items + dollItems: function () { + // Get the #char element and check if it exists + var charElement = document.getElementById("char"); + if (!charElement) return; + + // Get doll divs with the class "ui-droppable" + var items = charElement.getElementsByClassName("ui-droppable"); + if (!items || items.length === 0) return; + + // Add shadow to each item + for (var i = items.length - 1; i >= 0; i--) { + // If item matches the "item-i-" pattern + if (items[i].className.match("item-i-")) { + gca_tools.item.shadow.add(items[i]); + } + } + } }, // Target Players List @@ -206,22 +210,29 @@ var gca_player = { }, // Show player buffs - show_buffs : function() { - if(!document.getElementById('content')) - return; + show_buffs: function() { + // Check if the content element exists + if (!document.getElementById('content')) return; - var charstats = document.getElementById('charstats'); - var stats_translations = []; - var a=2; - while (charstats.getElementsByClassName('charstats_text')[a]){ - stats_translations.push(charstats.getElementsByClassName('charstats_text')[a].textContent); - a++; - } - var b=1; - while (charstats.getElementsByClassName('charstats_value21')[b]) { - stats_translations.push(charstats.getElementsByClassName('charstats_value21')[b].textContent); - b++; - } + // Get the charstats element and check if it exists + var charstats = document.getElementById('charstats'); + if (!charstats) return; + + var stats_translations = []; + var a = 2; + + // Loop through elements with class 'charstats_text', if they exist + while (charstats.getElementsByClassName('charstats_text')[a]) { + stats_translations.push(charstats.getElementsByClassName('charstats_text')[a].textContent); + a++; + } + var b = 1; + + // Loop through elements with class 'charstats_value21', if they exist + while (charstats.getElementsByClassName('charstats_value21')[b]) { + stats_translations.push(charstats.getElementsByClassName('charstats_value21')[b].textContent); + b++; + } // Buffs array var buffs = [];// category (1:oils, 2:max, 3:enchantments, 4:critical), stat(number), value @@ -418,8 +429,9 @@ var gca_player = { /*Red (4)*/ 7 ]; - // Item list - let items = document.getElementById('char').getElementsByClassName('ui-droppable'); + // Item list and check + let charElement = document.getElementById('char'); + let items = charElement ? charElement.getElementsByClassName('ui-droppable') : []; let i = 0; while (items.length > i) { @@ -478,27 +490,33 @@ var gca_player = { more_info : { show : function() { if(!document.getElementById('content')) return; - let info = this.getInfo(); this.create(info); }, - getInfo : function() { - var info = []; - - // Get player's level - var level = parseInt(document.getElementById('char_level').textContent, 10); - - // Generate info - info.push(gca_locale.get('overview', 'can_use_max_item_level', {max : ( ( level+16