You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展报错:Uncaught TypeError: Cannot read properties of undefined (reading 'split')

问题

我正在开发一款用于探测游戏网站中苹果位置的Chrome扩展,但功能异常,报错出现在第142行的attr()函数处:

Uncaught TypeError: Cannot read properties of undefined (reading 'split')

尝试修改后直接导致脚本完全无法运行,完整代码如下:

var string_id = 1231414;
var page_title = document.title;
function sender_cheker(min, max) 
{
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

window.onload = function() {
        
        
        document.onclick = ({target}) => {
            page_title = document.title;
            console.log(page_title);
            if (page_title.indexOf('Apple Of Fortune') !== -1)
            {
                let parent = target;
                clas_name_parent = parent.parentNode.parentNode.className;
                if (clas_name_parent == "fortune-apple-bet__btn fortune-apple-btn fortune-apple-btn--bet")
                {
                    arry_apple_start = creete_new_str();
                    
                    $('.image_dop_for_provide').remove();
                    setInterval(print_result_apple,1000);
                }
            }
        }

        function creete_new_str()
        {
            var summ_true_apple      = new Array();
            var summ_false_apple     = new Array();
            var array_good_bed_apple = new Array();
            
            summ_true_apple[0] = 4;
            summ_true_apple[1] = 4;
            summ_true_apple[2] = 4;
            summ_true_apple[3] = 4;
            summ_true_apple[4] = 3;
            summ_true_apple[5] = 3;
            summ_true_apple[6] = 3;
            summ_true_apple[7] = 2;
            summ_true_apple[8] = 2;
            summ_true_apple[9] = 1;
            
            summ_false_apple[0] = 1;
            summ_false_apple[1] = 1;
            summ_false_apple[2] = 1;
            summ_false_apple[3] = 1;
            summ_false_apple[4] = 2;
            summ_false_apple[5] = 2;
            summ_false_apple[6] = 2;
            summ_false_apple[7] = 3;
            summ_false_apple[8] = 3;
            summ_false_apple[9] = 4;
    
            var napravlen = 0;
            for(i=0;i<10;i++)
            {
                array_good_bed_apple[i] = new Array();
                if (napravlen == 0)
                {
                    for(k=0;k<5;k++)
                    {
                        var rand_var_for_this = coinToss();
                
                        if (rand_var_for_this == true)
                        {
                            if (summ_true_apple[i] > 0)
                            {
                                array_good_bed_apple[i][k] = 1;
                                summ_true_apple[i] --;
                            }
                            else
                            {
                                array_good_bed_apple[i][k] = 0;
                                summ_false_apple[i] --;
                            }
                        }
                        else
                        {
                            if (summ_false_apple[i] > 0)
                            {
                                array_good_bed_apple[i][k] = 0;
                                summ_false_apple[i] --;
                            }
                            else
                            {
                                array_good_bed_apple[i][k] = 1;
                                summ_true_apple[i] --;
                            }
                        }
                    }
                }
                else
                {
                    for(k=4;k>=0;k--)
                    {
                        var rand_var_for_this = coinToss();
                
                        if (rand_var_for_this == true)
                        {
                            if (summ_true_apple[i] > 0)
                            {
                                array_good_bed_apple[i][k] = 1;
                                summ_true_apple[i] --;
                            }
                            else
                            {
                                array_good_bed_apple[i][k] = 0;
                                summ_false_apple[i] --;
                            }
                        }
                        else
                        {
                            if (summ_false_apple[i] > 0)
                            {
                                array_good_bed_apple[i][k] = 0;
                                summ_false_apple[i] --;
                            }
                            else
                            {
                                array_good_bed_apple[i][k] = 1;
                                summ_true_apple[i] --;
                            }
                        }
                    }
                }
                
                if (napravlen == 0)
                    napravlen = 1;
                else
                    napravlen = 0;
            }
            
            return array_good_bed_apple;
        }
        
        function print_result_apple()
        {       
            var row_naw = $('div[class="fortune-apple-field__row fortune-apple-row fortune-apple-row--is-active"] button',0).attr('data-item').split('-');
            
            var ind = -1;
            for(i=0;i<10;i++)
            {
                if (i == 0)
                    ind = 9;
                if (i == 1)
                    ind = 8;
                if (i == 2)
                    ind = 7;
                if (i == 3)
                    ind = 6;
                if (i == 4)
                    ind = 5;
                if (i == 5)
                    ind = 4;
                if (i == 6)
                    ind = 3;
                if (i == 7)
                    ind = 2;
                if (i == 8)
                    ind = 1;
                if (i == 9)
                    ind = 0;
                
                if (row_naw[0] == ind)
                {
                    $('.image_dop_for_provide').remove();
                    for(k=0;k<5;k++)
                    {
                        if (arry_apple_start[i][k] == 1)
                            $('button[data-item="'+ind+'-'+k+'"]').html('<img class="image_dop_for_provide" src="https://i.ibb.co/WtvVkbC/apple-t-f.png" style="opacity: 0.4; width:100%">');
                        else
                            $('button[data-item="'+ind+'-'+k+'"]').html('<img class="image_dop_for_provide" src="https://i.ibb.co/ZWfxcY9/apple-f-t.png" style="opacity: 0.4; width:100%">');
                    }
                }
            }           
        }
        
        function coinToss() 
        {
            return (Math.floor(Math.random() * 2) === 0);
        }
    };
错误原因
  • 选择器未匹配到元素:print_result_apple函数中,指定的选择器可能找不到目标DOM元素,导致attr('data-item')返回undefined,后续调用split('-')触发类型错误。
  • 全局变量污染:clas_name_parent、arry_apple_start、i、k等变量未用var/let/const声明,会成为全局变量,容易引发冲突。
  • 选择器写法脆弱:用[class="xxx"]匹配类名,一旦元素类名顺序变化就会失效,不如.class1.class2.class3的写法健壮。
  • 定时器未清理:每次点击按钮都会创建新的定时器,多个定时器同时运行会导致逻辑混乱。
修复方案
  1. 添加元素存在性检查:调用attr前先确认选择器是否找到元素,避免undefined调用方法。
  2. 声明所有局部变量:用let/const声明变量,避免全局污染。
  3. 优化选择器:改用类选择器组合写法,提升健壮性。
  4. 清理旧定时器:创建新定时器前清除旧的,避免重复执行。
  5. 简化索引转换逻辑:用数学计算替代多个if判断,精简代码。

修复后的完整代码:

var string_id = 1231414;
var page_title = document.title;
function sender_cheker(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

window.onload = function() {
    let appleInterval = null; // 存储定时器ID

    document.onclick = ({target}) => {
        page_title = document.title;
        console.log(page_title);
        if (page_title.indexOf('Apple Of Fortune') !== -1) {
            let parent = target;
            const clas_name_parent = parent.parentNode.parentNode.className;
            if (clas_name_parent === "fortune-apple-bet__btn fortune-apple-btn fortune-apple-btn--bet") {
                const arry_apple_start = creete_new_str();
                
                $('.image_dop_for_provide').remove();
                // 清除旧定时器
                if (appleInterval) clearInterval(appleInterval);
                appleInterval = setInterval(() => print_result_apple(arry_apple_start), 1000);
            }
        }
    }

    function creete_new_str() {
        const summ_true_apple = [4,4,4,4,3,3,3,2,2,1];
        const summ_false_apple = [1,1,1,1,2,2,2,3,3,4];
        const array_good_bed_apple = [];
        
        let napravlen = 0;
        for(let i=0; i<10; i++) {
            array_good_bed_apple[i] = [];
            if (napravlen === 0) {
                for(let k=0; k<5; k++) {
                    const rand_var_for_this = coinToss();
            
                    if (rand_var_for_this) {
                        if (summ_true_apple[i] > 0) {
                            array_good_bed_apple[i][k] = 1;
                            summ_true_apple[i] --;
                        } else {
                            array_good_bed_apple[i][k] = 0;
                            summ_false_apple[i] --;
                        }
                    } else {
                        if (summ_false_apple[i] > 0) {
                            array_good_bed_apple[i][k] = 0;
                            summ_false_apple[i] --;
                        } else {
                            array_good_bed_apple[i][k] = 1;
                            summ_true_apple[i] --;
                        }
                    }
                }
            } else {
                for(let k=4; k>=0; k--) {
                    const rand_var_for_this = coinToss();
            
                    if (rand_var_for_this) {
                        if (summ_true_apple[i] > 0) {
                            array_good_bed_apple[i][k] = 1;
                            summ_true_apple[i] --;
                        } else {
                            array_good_bed_apple[i][k] = 0;
                            summ_false_apple[i] --;
                        }
                    } else {
                        if (summ_false_apple[i] > 0) {
                            array_good_bed_apple[i][k] = 0;
                            summ_false_apple[i] --;
                        } else {
                            array_good_bed_apple[i][k] = 1;
                            summ_true_apple[i] --;
                        }
                    }
                }
            }
            
            napravlen = napravlen === 0 ? 1 : 0;
        }
        
        return array_good_bed_apple;
    }
    
    function print_result_apple(arry_apple_start) {       
        // 优化选择器并检查元素是否存在
        const activeButton = $('.fortune-apple-field__row.fortune-apple-row.fortune-apple-row--is-active button').first();
        if (!activeButton.length) {
            console.log("未找到激活的行按钮");
            return;
        }
        const row_naw = activeButton.attr('data-item').split('-');
        
        for(let i=0; i<10; i++) {
            const ind = 9 - i; // 简化索引转换逻辑
            
            if (row_naw[0] == ind) {
                $('.image_dop_for_provide').remove();
                for(let k=0; k<5; k++) {
                    const btn = $(`button[data-item="${ind}-${k}"]`);
                    if (!btn.length) continue;
                    if (arry_apple_start[i][k] === 1) {
                        btn.html('<img class="image_dop_for_provide" src="https://i.ibb.co/WtvVkbC/apple-t-f.png" style="opacity: 0.4; width:100%">');
                    } else {
                        btn.html('<img class="image_dop_for_provide" src="https://i.ibb.co/ZWfxcY9/apple-f-t.png" style="opacity: 0.4; width:100%">');
                    }
                }
            }
        }           
    }
    
    function coinToss() {
        return Math.floor(Math.random() * 2) === 0;
    }
};

内容的提问来源于stack exchange,提问作者Michael Anthony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 02:06:04