Skip to content

Instantly share code, notes, and snippets.

@Dry7
Last active October 11, 2018 00:47
Show Gist options
  • Save Dry7/6ef5d8e363eb8ee05d833ca5d72cc5dd to your computer and use it in GitHub Desktop.
Save Dry7/6ef5d8e363eb8ee05d833ca5d72cc5dd to your computer and use it in GitHub Desktop.
const CDP = require('chrome-remote-interface');
CDP(async (client) => {
const {Network, Page, Target} = client;
Network.requestWillBeSent(({request, redirectResponse}) => {
console.log((redirectResponse || {}).url + ' -> ' + request.url);
});
Target.targetCreated((params) => {
if(params.targetInfo.type != "page") {
return;
}
const {targetId} = params.targetInfo;
const findTarget = (targets) => {
return targets.find(target => target.id === targetId);
};
CDP({target: findTarget}, async (popup) => {
popup.Network.requestWillBeSent(({request, redirectResponse}) => {
console.log((redirectResponse || {}).url + ' -> ' + request.url);
});
await popup.Network.enable();
await popup.Runtime.runIfWaitingForDebugger();
});
});
try {
await Target.setDiscoverTargets({discover: true});
Target.setAutoAttach({
autoAttach: true,
waitForDebuggerOnStart: true
});
await Network.enable();
await Page.enable();
await Page.navigate({url: 'http://getresto.gifts48.ru/1/'});
await Page.loadEventFired();
clickPage(client);
} catch (err) {
console.error(err);
} finally {
// client.close();
}
}).on('error', (err) => {
console.error(err);
});
function clickPage(client) {
const options = {
x: 100,
y: 100,
button: 'left',
clickCount: 1
};
Promise.resolve().then(() => {
options.type = 'mousePressed';
return client.Input.dispatchMouseEvent(options);
}).then(() => {
options.type = 'mouseReleased';
return client.Input.dispatchMouseEvent(options);
}).catch((err) => {
console.error(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment