I done this by disable interval and timeout, some site use this to prevent others watching code.
I insert code before onload by using chrome headless
Start headless
chrome --headless --disable-gpu <URL> --remote-debugging-port=9222
Other shell session
yarn add chrome-remote-interface
test.es6
const CDP = require('chrome-remote-interface');
async function test() {
const protocol = await CDP({port: 9222});
// Extract the DevTools protocol domains we need and enable them.
// See API docs: https://chromedevtools.github.io/devtools-protocol/
const {Page, Runtime, Debugger, Log, Console} = protocol;
try {
await Promise.all([
Page.enable(),
Runtime.enable(),
Log.enable(),
Console.enable(),
Debugger.disable(),
]);
} catch (e) {
console.log('Failed', e)
return
}
Log.entryAdded(({entry: e}) =>
console.log(`${new Date(e.timestamp).toISOString()} ${e.source}:${e.level} ${e.text}`)
);
Console.messageAdded(({message: e}) =>
console.log(`${new Date().toISOString()} ${e.source}:${e.level} ${e.text}`)
)
Page.navigate({url: URL_HERE});
// Inject code,disable setInterval and setTimeout
Runtime.executionContextCreated(async ({context}) => {
console.log('executionContextCreated')
let result = await Runtime.evaluate({
expression: `
window._si=window.setInterval
window.setInterval=(...args)=>{
let id = 1//window._si.apply(window,args)
console.warn(\`setInterval:\${args}\`,)
return id
}
window._st=window.setTimeout
window.setTimeout=(...args)=>{
let id = 1//window._st.apply(window,args)
console.warn(\`setTimeout:\${args}\`,)
return id
}
;location.href
`,
contextId: context.id,
});
console.log('executionContextCreated', result)
});
// Wait for window.onload before doing stuff.
Page.loadEventFired(async () => {
// Debugger.setSkipAllPauses(true)
const js = `
console.log('Page load');
document.querySelector('title').textContent
`;
// Evaluate the JS expression in the page.
const result = await Runtime.evaluate({expression: js});
console.log('Title of page: ' + result.result.value);
protocol.close();
});
}
test()
After script, open 'localhost:9222' in chrome, inspect the page, the debugger not run now.