Skip to content

Instantly share code, notes, and snippets.

@imaman
Created December 25, 2018 13:49
Show Gist options
  • Select an option

  • Save imaman/8ef2ba68ccb3dc440627ea6767c273bf to your computer and use it in GitHub Desktop.

Select an option

Save imaman/8ef2ba68ccb3dc440627ea6767c273bf to your computer and use it in GitHub Desktop.
Drill info iframes which are not SAME_ORIGIN
const CDP = require('chrome-remote-interface');
const chromeLauncher = require('chrome-launcher');
// cdp.js: a playground for using chrome-devtools-protocol.
// Plug-in your own code at the run() function, below.
//
// Usage:
// $ node scripts/cdp.js
//
// This program requires node version >= 8.
async function launch(action, flags) {
let chrome;
try {
console.log('\n\nStarting with flags: ' + JSON.stringify(flags));
chrome = await chromeLauncher.launch({chromeFlags: flags});
console.log(`Chrome debugging port running on ${chrome.port}`);
return await action(chrome.port);
} finally {
if (chrome) {
await chrome.kill();
}
}
}
async function onLaunched(port) {
let client;
try {
// const prot = await CDP.Protocol({port});
// console.log('prot=' + JSON.stringify(prot, null, 2));
const options = {port};
let count = 0;
while (++count < 20) {
console.log(`Iteration ${count}: Connecting to ${JSON.stringify(options)}`);
client = await CDP(options);
const targetId = await run(client, count === 1);
if (!targetId) {
break;
}
options.target = targetId;
}
} finally {
if (client) {
await client.close();
}
}
}
async function main() {
await launch(onLaunched, []);
// await launch(onLaunched, ['--headless']);
// await launch(onLaunched, ['--disable-features=top-document-isolation,site-per-process']);
// await launch(onLaunched, ['--disable-features=top-document-isolation']);
// await launch(onLaunched, ['--disable-features=site-per-process']);
}
/**
* Your custom business logic goes in here.
* @returns {*} whatever is returned from this method will be console.log()-ed by the caller.
*/
async function run(client, shouldNavigate) {
const { Page, DOM, Browser, DOMSnapshot, Target } = client;
await Promise.all([DOM.enable(),
Page.enable(),
DOMSnapshot.enable()]);
const SAME_ORIGIN = "https://imaman.github.io/outer.html"
const NOT_SAME_ORIGIN = "http://jsbin.testim.io/zuw"
if (shouldNavigate) {
await Page.navigate({ url: NOT_SAME_ORIGIN });
await Page.loadEventFired();
}
const v = await Browser.getVersion();
console.log('version=\n' + JSON.stringify(v, null, 2));
const nodeId = (await check(client, 'DOM', 'getDocument', {depth: 1, pierce: true})).root.nodeId;
const specs = [
["DOM", "getOuterHTML", {nodeId}],
["DOMSnapshot", "captureSnapshot", {computedStyles: []}],
["DOM", "describeNode", {nodeId, depth: -1, pierce: true}],
["DOM", "requestChildNodes", {nodeId}],
["DOM", "getFlattenedDocument", {depth: -1, pierce: true}],
];
for (let i = 0; i < specs.length; ++i) {
const [domain, command, params] = specs[i];
await check(client, domain, command, params);
}
const fd = await DOM.getFlattenedDocument({depth: -1, pierce: true});
const unexpandedIframes = fd.nodes.filter(n => n.nodeName === 'IFRAME' && !n.contentDocument).map(n => n.frameId);
if (!unexpandedIframes.length) {
return null;
}
return unexpandedIframes[0];
}
async function check(client, domain, command, params) {
const func = client[domain][command];
const obj = await func(params);
const str = JSON.stringify(obj, null, 2);
const index = str.indexOf('3.1415926');
const what = `${domain}.${command}`;
if (index < 0) {
console.log(`${what}: No`);
return obj;
}
console.log(`${what}: YES-YES-YES`);
return obj;
}
if (require.main === module) {
main()
.then(x => console.log('-the end-'))
.catch(e => console.error('Error: ', e));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment