-
Notifications
You must be signed in to change notification settings - Fork 0
/
workarounds.js
120 lines (109 loc) · 3.36 KB
/
workarounds.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//@ts-check
const puppeteer = require('puppeteer-core');
/** @type {[RegExp, (page: puppeteer.Page) => Promise<void>][]} */
const workArounds = [
[/google\..*/, googlePolicy],
[/youtube\.com/, youtubePolicy],
[/roblox.com/, robloxRedirect],
[/twitter\.com/, twitterRedirect],
[/github\.com/, githubRedirect],
];
/**
* @param {puppeteer.Page} page
* @returns {Promise<void>}
*/
async function githubRedirect(page) {
console.log(`Applying github redirect workaround for ${page.url()}`);
if (page.url() === 'https://github.com/') {
await page.goto('https://github.com/userstyles-world/userstyles.world', {waitUntil: 'domcontentloaded'});
}
}
/**
* @param {puppeteer.Page} page
* @returns {Promise<void>}
*/
async function twitterRedirect(page) {
console.log(`Applying twitter redirect for ${page.url()}`);
if (page.url() === 'https://twitter.com/') {
// Fox is always good!
await page.goto('https://twitter.com/joinmastodon/', {waitUntil: 'domcontentloaded'});
}
}
/**
* @param {puppeteer.Page} page
* @returns {Promise<void>}
*/
async function robloxRedirect(page) {
console.log(`Applying roblox redirect for ${page.url()}`);
if (page.url() === 'https://www.roblox.com/') {
await page.goto('https://www.roblox.com/discover#/', {waitUntil: "networkidle0"});
}
await page.waitForSelector('.cookie-button-container .btn-cta-lg', {
timeout: 5000,});
await page.click('.cookie-button-container .btn-cta-lg');
}
/**
* @param {puppeteer.Page} page
* @returns {Promise<void>}
*/
async function googlePolicy(page) {
console.log(`Applying google policy workaround on: "${page.url()}"`);
// Click button img + div + div > :last-child
await page.click('img + div + div > :last-child');
}
/**
* @param {puppeteer.Page} page
* @returns {Promise<void>}
*/
async function youtubePolicy(page) {
console.log(`Applying youtube workaround on: "${page.url()}"`);
// Click button .buttons.ytd-consent-bump-v2-lightbox > :last-child [role="button"]
await page.click('.buttons.ytd-consent-bump-v2-lightbox > :last-child [role="button"]');
// This is some custom button so it might take a bit of time to dissapear.
await page.waitForTimeout(500);
}
/**
* @param {string} url
* @returns {(page: puppeteer.Page) => Promise<void>}
*/
function matchPage(url) {
console.log(`Checking "${url}"`);
for (let i = 0; i < workArounds.length; i++) {
if (workArounds[i][0].test(url)) {
return workArounds[i][1];
}
}
return null;
}
const cloudflareTitles = [
'Please Wait... | Cloudflare',
'Just a moment...',
];
/**
* @param {puppeteer.Page} page
*/
async function checkForWorkarounds(page) {
let domain;
try {
const pageURL = new URL(page.url());
domain = pageURL.hostname;
} catch (e) {
console.log(`Error parsing page URL: ${e}`);
return;
}
const workAround = matchPage(domain);
if (workAround) {
await workAround(page);
}
// Check the title against the common cloudflare title.
const pageTitle = await page.title();
console.log(`Checking Title "${pageTitle}"`);
if (cloudflareTitles.includes(pageTitle)) {
console.log(`Cloudflare detected on "${domain}"`);
// Exit the program.
process.exit(31);
}
}
module.exports = {
checkForWorkarounds,
};