Skip to content

在控制台全局搜索的一种办法 #2

@MasterPolymath

Description

@MasterPolymath

function findKey(obj, target, path = 'terra.export', visited = new WeakSet()) {
// 1. 基础校验:如果不是对象或为 null,直接返回
if (typeof obj !== 'object' || obj === null) return;

// 2. 核心防死循环:如果当前对象已经被访问过,直接跳过
if (visited.has(obj)) return;
visited.add(obj);

for (let key in obj) {
    // 防止报错:跳过原型链上的属性
    if (!obj.hasOwnProperty(key)) continue; 

    try {
        // 3. 找到目标
        if (key === target) {
            console.log(`🎯 Found: ${path}.${key} =`, obj[key]);
        }
        
        // 4. 继续深入查找
        if (typeof obj[key] === 'object' && obj[key] !== null) {
            findKey(obj[key], target, `${path}.${key}`, visited);
        }
    } catch (e) {
        // 忽略某些无法访问的属性(如跨域或 getter 报错)
    }
}

}

// 执行搜索
findKey(terra.export, 'GUARD_PARRY_REPEAT_TIME');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions