I wanna create a Chrome extension that needs to read a specific part which the website posts into the console. Here you can see this part:
I need specifically the "questionsList" part.
How do I do that in the JS of my Extension?
I wanna create a Chrome extension that needs to read a specific part which the website posts into the console. Here you can see this part:
I need specifically the "questionsList" part.
How do I do that in the JS of my Extension?
As the other user said, overriding the console.log method will be the easier way. You can then iterate over the arguments passed in to the console.log function and see if one of the arguments is an object that has a property named "questionList", and if it does it should store its value in a global object named questionList. You can inject this code in the console:
let oldConsole=window.console
let questionList=[];
window.console=(function(oldConsole){
return {
log:function(...args){
oldConsole.log(...args)
for(arg of args){
if(typeof arg==="object" && "questionList" in arg){
questionList=arg["questionList"]
}
}
},
error:function(...args){
oldConsole.error(...args)
}
}
})(oldConsole);
Now whenever the console logs that particular object you can access this array from the questionList global variable.
Edit: In order to inject this code in the page you can use the Debugger's API "Page.addScriptToEvaluateOnNewDocument" method. First you will have to add the debugger permission in your manifest.json file. Then you can add a listener to your browser action's onClicked event, and then attach the debugger to that particular tab. After that you should enable the Debugger's page domain using the method "Page.enable", then finally you can call the "Page.addScriptToEvaluateOnNewDocument" method:
let tabId;
chrome.browserAction.onClicked.addListener(handleClick)
function handleClick(tab){
tabId=tab.id
chrome.debugger.attach({tabId},"1.0")
chrome.debugger.sendCommand({tabId},"Page.enable");
source=`let oldConsole=window.console;
let questionList=[];
window.console=(function(oldConsole){
console.log("Overriding console")
return {
log:function(...args){
oldConsole.log(...args)
for(arg of args){
if(typeof arg==="object" && "questionList" in arg){
questionList=arg["questionList"]
}
}
},
error:function(...args){
oldConsole.error(...args)
}
}
})(oldConsole);
`
chrome.debugger.sendCommand({tabId},"Page.addScriptToEvaluateOnNewDocument",{source});
After that you need to navigate to the page in order for the script to be evaluated.