Chrome 扩展 console.log 覆盖

Chrome extension console.log override

我正在构建一个 chrome 扩展,它读取控制台日志并找到 ip 出现的位置,在字符串 "Connecting to" 之后,并获取 ip。

store = []; 
var oldf = console.log; 
console.log = function(){   
store.push(arguments);    
oldf.apply(console, arguments);
};

pos = 0 
server = ""

setTimeout(function(){
    for(i = 0; i < store.length; i++){
        if(store[i][0].indexOf("Connecting to") != -1){
             pos = i
        }
    }
    var goal = store[pos][0].split(" ")[self.length-1];
    server = goal
    console.log(server);
  }, 3000);

我已经用 Tampermonkey 尝试了这段代码并且工作正常,但是作为 chrome 扩展它不会 work.The 覆盖 console.log 函数正常工作所以它可能是关于chrome 扩展名的权限。我是第一个所以我不是很了解。我收到 Uncaught TypeError: Cannot read 属性 '0' of undefined 如果你还需要什么就告诉我

原因是因为 Tampermonkey 将代码注入站点的文档,而在 Chrome Extension 中不,如果你这样做,你编辑 Chrome 扩展程序的控制台。为此,你应该使用一种方法来注入脚本,你可以看到here