Created
April 27, 2020 17:45
-
-
Save DanEdens/51b811940ee0d9cd23b495dc88d5d6c7 to your computer and use it in GitHub Desktop.
Interactive Browser session with Live template function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const puppeteer = require('puppeteer'); | |
const Promise = require("bluebird"); | |
const CREDS = require(__dirname + '/user/creds.js'); | |
const url = 'www.google.com' | |
(async () => { | |
let wargs = puppeteer.defaultArgs() | |
function arrayRemove(arr, value) { | |
return arr.filter(function (ele) { | |
return ele != value; | |
}); | |
} | |
wargs = arrayRemove(wargs, '--password-store=basic'); | |
wargs = arrayRemove(wargs, '--hide-scrollbars'); | |
wargs = arrayRemove(wargs, '--use-mock-keychain'); | |
wargs = arrayRemove(wargs, '--disable-extensions'); | |
wargs = arrayRemove(wargs, '--disable-sync'); | |
wargs = arrayRemove(wargs, '--headless') | |
wargs = arrayRemove(wargs, 'about:blank') | |
const userdata = process.env.userdata | |
const chromeexe = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"; | |
wargs.push('--no-sandbox'); | |
// wargs.push('--start-fullscreen'); | |
//wargs.push('--new-window'); | |
let browser = await puppeteer.launch({ | |
userDataDir: userdata, | |
executablePath: chromeexe, | |
ignoreDefaultArgs: ['--headless', '--password-store=basic', '--disable-extensions', '--hide-scrollbars'], | |
args: wargs | |
}); | |
const page = await browser.newPage(); | |
await page.setViewport({ | |
width: 1920, | |
height: 980, | |
deviceScaleFactor: .6 | |
}); // | |
await page.goto(url, { | |
waitUntil: 'domcontentloaded' | |
}); | |
await page.setDefaultNavigationTimeout(0); | |
const navigationPromise = page.waitForNavigation(); | |
const dimensions = await page.evaluate(() => { | |
return { | |
width: document.documentElement.clientWidth, | |
height: document.documentElement.clientHeight, | |
deviceScaleFactor: window.devicePixelRatio | |
}; | |
}); | |
console.log('Dimensions:', dimensions); | |
console.log('Interactive Browser session initiated:\n'); | |
/* | |
Template functions go here. After using the template, move section 2 to listed section below | |
This template works with jetbrains Live templates | |
*/ | |
async function $NAME$(browser,page) { | |
/* | |
Preform this function during Raw Input Interactive browser session | |
Input Key: $KEY$ | |
*/ | |
$SELECTION$ | |
} | |
// Move Section Start - $NAME$ | |
if (['$key$'].includes(key.name)) { | |
try { | |
await $NAME$(browser, page); | |
} catch (error) { | |
console.log('Caught:', error.message) | |
} | |
} // Move Section End - $NAME$ | |
process.stdin.on('keypress', async (str, key) => { | |
if (key.sequence === '\u0003') { | |
await browser.close(); | |
process.exit(); | |
} | |
//Second part of template goes here | |
}); | |
readline.emitKeypressEvents(process.stdin); | |
process.stdin.setRawMode(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment