upgrade to ES6 modules and top-level await, add jsconfig.json with es2022
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://code.visualstudio.com/docs/languages/jsconfig
This commit is contained in:
parent
9bb548dc3e
commit
3b3289dddf
5 changed files with 186 additions and 182 deletions
|
|
@ -1,16 +1,12 @@
|
||||||
//@ts-check
|
import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra
|
||||||
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
|
import path from 'path';
|
||||||
const path = require('path');
|
import { __dirname, stealth } from './util.js';
|
||||||
const { stealth } = require('./util.js');
|
|
||||||
|
|
||||||
const debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector
|
const debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector
|
||||||
|
|
||||||
const URL_LOGIN = 'https://www.epicgames.com/login';
|
const URL_LOGIN = 'https://www.epicgames.com/login';
|
||||||
const URL_CLAIM = 'https://www.epicgames.com/store/en-US/free-games';
|
const URL_CLAIM = 'https://www.epicgames.com/store/en-US/free-games';
|
||||||
const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
|
|
||||||
// could change to .mjs to get top-level-await, but would then also need to change require to import and dynamic import for stealth below would just add more async/await
|
|
||||||
(async () => {
|
|
||||||
// https://playwright.dev/docs/auth#multi-factor-authentication
|
// https://playwright.dev/docs/auth#multi-factor-authentication
|
||||||
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), {
|
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), {
|
||||||
channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge
|
channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge
|
||||||
|
|
@ -74,7 +70,6 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
console.log('Not in library yet! Click GET.')
|
console.log('Not in library yet! Click GET.')
|
||||||
await page.click('[data-testid="purchase-cta-button"]');
|
await page.click('[data-testid="purchase-cta-button"]');
|
||||||
// click Continue if 'Device not supported. This product is not compatible with your current device.'
|
// click Continue if 'Device not supported. This product is not compatible with your current device.'
|
||||||
// @ts-ignore https://caniuse.com/?search=promise.any
|
|
||||||
await Promise.any([':has-text("Continue")', '#webPurchaseContainer iframe'].map(s => page.waitForSelector(s))); // wait for Continue xor iframe
|
await Promise.any([':has-text("Continue")', '#webPurchaseContainer iframe'].map(s => page.waitForSelector(s))); // wait for Continue xor iframe
|
||||||
if (await page.locator(':has-text("Continue")').count() > 0) {
|
if (await page.locator(':has-text("Continue")').count() > 0) {
|
||||||
// console.log('Device not supported. This product is not compatible with your current device.');
|
// console.log('Device not supported. This product is not compatible with your current device.');
|
||||||
|
|
@ -88,7 +83,6 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
// I Agree button is only shown for EU accounts! https://github.com/vogler/free-games-claimer/pull/7#issuecomment-1038964872
|
// I Agree button is only shown for EU accounts! https://github.com/vogler/free-games-claimer/pull/7#issuecomment-1038964872
|
||||||
const btnAgree = iframe.locator('button:has-text("I Agree")');
|
const btnAgree = iframe.locator('button:has-text("I Agree")');
|
||||||
try {
|
try {
|
||||||
// @ts-ignore https://caniuse.com/?search=promise.any
|
|
||||||
await Promise.any([btnAgree.waitFor(), page.waitForSelector('text=Thank you for buying')]); // EU: wait for agree button, non-EU: potentially done
|
await Promise.any([btnAgree.waitFor(), page.waitForSelector('text=Thank you for buying')]); // EU: wait for agree button, non-EU: potentially done
|
||||||
// await clickIfExists('button:has-text("I Agree")', iframe); // default arg: FrameLocator is incompatible with Page and even Locator...
|
// await clickIfExists('button:has-text("I Agree")', iframe); // default arg: FrameLocator is incompatible with Page and even Locator...
|
||||||
if (await btnAgree.count() > 0)
|
if (await btnAgree.count() > 0)
|
||||||
|
|
@ -118,4 +112,3 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
}
|
}
|
||||||
// await context.waitForEvent("close");
|
// await context.waitForEvent("close");
|
||||||
await context.close();
|
await context.close();
|
||||||
})();
|
|
||||||
|
|
|
||||||
9
jsconfig.json
Normal file
9
jsconfig.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"checkJs": true,
|
||||||
|
"target": "es2021",
|
||||||
|
"module": "es2022",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
},
|
||||||
|
"exclude": ["node_modules", "**/node_modules"]
|
||||||
|
}
|
||||||
|
|
@ -10,5 +10,6 @@
|
||||||
"@playwright/test": "^1.17.1",
|
"@playwright/test": "^1.17.1",
|
||||||
"playwright": "^1.17.1",
|
"playwright": "^1.17.1",
|
||||||
"puppeteer-extra-plugin-stealth": "^2.9.0"
|
"puppeteer-extra-plugin-stealth": "^2.9.0"
|
||||||
}
|
},
|
||||||
|
"type": "module"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
//@ts-check
|
import { chromium } from 'playwright'; // stealth plugin needs no outdated playwright-extra
|
||||||
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
|
import path from 'path';
|
||||||
const path = require('path');
|
import { __dirname, stealth } from './util.js';
|
||||||
const { stealth } = require('./util.js');
|
|
||||||
|
|
||||||
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
|
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
|
||||||
|
|
||||||
|
|
@ -9,8 +8,6 @@ const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://play
|
||||||
const URL_CLAIM = 'https://gaming.amazon.com/home';
|
const URL_CLAIM = 'https://gaming.amazon.com/home';
|
||||||
const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
|
|
||||||
// could change to .mjs to get top-level-await, but would then also need to change require to import and dynamic import for stealth below would just add more async/await
|
|
||||||
(async () => {
|
|
||||||
// https://playwright.dev/docs/auth#multi-factor-authentication
|
// https://playwright.dev/docs/auth#multi-factor-authentication
|
||||||
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), {
|
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), {
|
||||||
channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge
|
channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge
|
||||||
|
|
@ -81,4 +78,3 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
} while (n);
|
} while (n);
|
||||||
}
|
}
|
||||||
await context.close();
|
await context.close();
|
||||||
})();
|
|
||||||
|
|
|
||||||
19
util.js
19
util.js
|
|
@ -1,7 +1,12 @@
|
||||||
|
// https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
|
||||||
|
import path from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
export const __filename = fileURLToPath(import.meta.url);
|
||||||
|
export const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
|
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
|
||||||
const newStealthContext = async (browser, contextOptions = {}) => {
|
const newStealthContext = async (browser, contextOptions = {}, debug = false) => {
|
||||||
if (!debug) { // fix userAgent in headless mode
|
if (!debug) { // only need to fix userAgent in headless mode
|
||||||
const dummyContext = await browser.newContext();
|
const dummyContext = await browser.newContext();
|
||||||
const originalUserAgent = await (await dummyContext.newPage()).evaluate(() => navigator.userAgent);
|
const originalUserAgent = await (await dummyContext.newPage()).evaluate(() => navigator.userAgent);
|
||||||
await dummyContext.close();
|
await dummyContext.close();
|
||||||
|
|
@ -13,7 +18,7 @@ const newStealthContext = async (browser, contextOptions = {}) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const stealth = async (context) => {
|
export const stealth = async (context) => {
|
||||||
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
|
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
|
||||||
// https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth/evasions
|
// https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth/evasions
|
||||||
const enabledEvasions = [
|
const enabledEvasions = [
|
||||||
|
|
@ -35,17 +40,17 @@ const stealth = async (context) => {
|
||||||
'webgl.vendor',
|
'webgl.vendor',
|
||||||
'window.outerdimensions'
|
'window.outerdimensions'
|
||||||
];
|
];
|
||||||
const evasions = enabledEvasions.map(e => require(`puppeteer-extra-plugin-stealth/evasions/${e}`));
|
|
||||||
const stealth = {
|
const stealth = {
|
||||||
callbacks: [],
|
callbacks: [],
|
||||||
async evaluateOnNewDocument(...args) {
|
async evaluateOnNewDocument(...args) {
|
||||||
this.callbacks.push({ cb: args[0], a: args[1] })
|
this.callbacks.push({ cb: args[0], a: args[1] })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
evasions.forEach(e => e().onPageCreated(stealth));
|
for (const e of enabledEvasions) {
|
||||||
|
const evasion = await import(`puppeteer-extra-plugin-stealth/evasions/${e}/index.js`);
|
||||||
|
evasion.default().onPageCreated(stealth);
|
||||||
|
}
|
||||||
for (let evasion of stealth.callbacks) {
|
for (let evasion of stealth.callbacks) {
|
||||||
await context.addInitScript(evasion.cb, evasion.a);
|
await context.addInitScript(evasion.cb, evasion.a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { stealth };
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue