遍历爬取js对象所有属性

该方法遍历传入参数的所有属性

可以用来获取本地变量下的值
该脚本可以直接在浏览器执行,也可以放到油猴里面
使用

  1. globeSearch.filterKeys=“”;
  2. globalSearch.globalSearch.searchText=“自然数之一”
  3. globalSearch.globalSearch.runSearch(“window”);
    其中有参数
    filterKeys: [],
    searchText: “”,//搜索内容
    methodExe:false,//是否执行无参有返回值方法
    deepLength:20,//递归深度
    可以控制
// ==UserScript==
// @name         爬取接口数据
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
//@include        *
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
})();
 window.globalSearch=(function(){
      'use strict';
     let globeSearch = {
    //搜索时需要过滤的路径关键词
    filterKeys: ['["observing"]','rightBtn', 'doSubmit','$options','render','_self','_watcher','_'],
    searchText: "",//搜索内容
    methodExe:false,//是否执行无参有返回值方法
    deepLength:20,//递归深度
    runSearch: function (pathString,currentDeepLength) {//递归方法
       // if(pathString=='temp1["getParams"]') debugger
        currentDeepLength=currentDeepLength||0;
        currentDeepLength+=1;
        if(currentDeepLength>this.deepLength){
         return;
        }
        //用来搜索路径
        let temp="/"+this.searchText+"/gi";
        if (pathString.toLowerCase().includes(this.searchText.toLowerCase())) {
        //if (temp.test(pathString)) {
            //console.log(pathString)
            this.hightLingth(pathString,this.searchText)
        }
//进行防治调用死链

let isMore = /(["[a-z]+"])/g;
let s = new Set();
let count = 0;
        let matched;
while ((matched = isMore.exec(pathString)) !== null) {
    s.add(matched[0]);
    count++;
    // debugger
    if (count / s.size > 8) {
        this.filterKeys.push(pathString.substring(0, pathString.lastIndexOf('[')));
        //  debugger
        // console.log(filterkeys)
        return;
    }
}
        //执行过滤
        if (this.filterKeys.filter((a) => pathString.indexOf(a) > -1).length > 0) {
            return;
        }

        //用来调试
        // Object.keys(WfForm).forEach((childAttr) => {
        //     let temp = pathString + '["' + childAttr + '"]';
        //     if (pathString == 'WfForm["' + childAttr + '"]') {
        //         //console.log("函数式",pathString)
        //         // debugger
        //     }
        // })
        try {
            if (eval(pathString) != null || eval(pathString) != undefined) {

                let type = eval('typeof ' + pathString);

                if (type == 'function') {

                    let command = pathString;

                    let hasReturn = /return |native code|[(][)]=>/;


                    //执行参数为0且有返回值的函数,并递归其返回值
                    if (this.methodExe==true&&eval(command + '.length') == 0 && hasReturn.test(eval(command + '.toString()'))) {
                      //  console.log("aaaaaaaaaaaa"+command)
                    //    debugger
                        this.runSearch(command + "()",currentDeepLength);
                    }


                } else if (type == 'object') {
                    if (eval(pathString) instanceof Array) {

                        for (let a = 0; a < eval(pathString).length; a++) {
                            let command = pathString + '[' + a + ']';
                            console.log("Array" + command);

                            this.runSearch(command,currentDeepLength);
                        }
                    } else {
                        console.log("eval(pathString)", eval(pathString));
                        Object.keys(eval(pathString)).forEach((childAttr) => {
                            let command = pathString + '["' + childAttr + '"]';
                            this.runSearch(command,currentDeepLength);
                        })
                    }
                } else {
                        let temp="/"+this.searchText+"/gi"
                         if (pathString.toLowerCase().includes(this.searchText.toLowerCase()) || (eval(pathString) + "").toLowerCase().includes(this.searchText.toLowerCase())) {
                    //if (temp.test(pathString) || temp.test((eval(pathString) + ""))) {
                        //console.log("---------------------------------" + pathString);
                             this.hightLingth("---------------------------------"+pathString,this.searchText)
                        console.log(eval(pathString));
                    }
                }
            }
        } catch (e) {
            console.error(e)
            console.error(pathString)
        }
    },
         hightLingth:function(str,highlightChar){
const regex = new RegExp(highlightChar, "g");
const highlightedStr = str.replace(regex, `%c${highlightChar}%c`);

const highlightStyle = "color: red; font-weight: bold;";
console.log(highlightedStr, highlightStyle, "");

         }

}

     return {globalSearch:globeSearch}})(window)




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值