简体   繁体   中英

Looking for a way to grab real-time console logs for a website I do not own

Here is what I have going on. I have a rPI that launches chrome into three tabs that I have set using xdotool to cycle between the three tabs. Everything is working great with that functionality, but I am looking to have it stop cycling and stay on one of the tabs when an event on that website happens. I have the code done to go back to that tab and stay there for x-amount of time. What I need help with is getting the code to recognize the event happening. I have watched the console when the event occurs and there is a log of the function call as well as the object that is passed from the JS code. If there is a way to monitor that console log real-time in the background and catch that function call being printed to the log then I could use that to fire the rest of the logic to lock the screen to that tab.

Or if anyone can come up with a different/easier plan that would be greatly appreciated. When the function call happens there is a list of names that displays on the website. Maybe we could check that list for any name and then lock the screen.

I tried to use selenium to grab the logs. I was able to get it to start chrome and then go to the website and pull up the logs. That worked as it was supposed to from the documentation that I have read. The problem is I need something to run on an already running instance of chrome. Maybe have it in the code that when it goes to the tab where the function would be called it would check the log and execute code, not launch and then close an instance of chrome.

If there is a way to monitor that console log real-time in the background and catch that function call being printed to the log

There is (though not in the background). Here's how you can do it

function myConsoleLogFunc(info) {
  // examine the info being logged
  this.log(info);
}
myConsoleLogFunc.log = console.log;
console.log = myConsoleLogFunc;

So I was able to get the answer I was looking for with puppeteer and some navigation and bash scripts. Below is the code that I used to complete the task.

const puppeteer = require('puppeteer-core');

async function start(){

const browser = await puppeteer.launch({executablePath: '/usr/bin/chromium-browser'});  //launch browser window in bg
const page = await browser.newPage();                           //get new page in browser
await page.setViewport({width: 1280, height: 800});                 //set window size
await page.goto('https://auth.iamresponding.com/login/member');             //open i am responding page

await page.click('#accept-policy');                         //click accept cookies
await page.type('#Input_Agency', '#########');                  //input agency name
await page.type('#Input_Username', '#########');                    //input user name
await page.type('#Input_Password', '#########');                    //input password


await Promise.all([page.click('button[name="Input.button"'), page.waitForNavigation()]) //click login button and wait for new page to load

var messageTest = ""                                    // var to hold console message for testing
var testDone = false
var loaded = 0                                      // var to only fire code on first pushrespond notice

page.on('console', message => {                             //get the console logs from the browser and pass them to the test method
        messageTest = message.text()
        console.log(messageTest)

        testDone = testValue(messageTest)

})

function testValue(cLog){                               //method to test the console message for responding and clear responding
    if (cLog.includes("pushrespond")) {                     //check to see if value is pushrespond
        loaded += 1                             // if it is increment the loaded var
        if (loaded == 1){                           // check if loaded = 1 and if so open new chrome window and execute login
            require('child_process').exec('sh /home/pi/open.sh',
                (error, stdout, stderr) => {
                console.log(stdout);
                console.log(stderr);
                if (error !== null) {
                    console.log(`exec error: ${error}`);
                }
            });
            return                              //return out of the method
        }else {
            return                              // if loaded is more than one return out of method without doing anything
        }
        return
    }else if (cLog.includes("pushautoclear")){                  //check to see if console message is push autoclear
        if(loaded >= 1){                            //make sure that there is a valid window to close out of as to not close main browser if no one was responding
            require('child_process').exec('sh /home/pi/exit.sh',        //close the window that was launched on responding
                                    (error, stdout, stderr) => {
                                    console.log(stdout);
                                    console.log(stderr);
                                    if (error !== null) {
                                            console.log(`exec error: ${error}`);
                                    }
                            });
            loaded = 0                          //reset loaded to 0 so all functions work properly on next iteration
        }else{
            return
        }
        return
    }else{                                      //exit out of the method if message does not contain pushrespond or pushautoclear
        return
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM