mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 02:57:34 +00:00
fix: memory leak due to adblocker (#139)
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
import {Page} from 'puppeteer';
|
||||||
|
import {PuppeteerExtraPluginAdblocker} from 'puppeteer-extra-plugin-adblocker';
|
||||||
|
|
||||||
|
export const adBlocker = new PuppeteerExtraPluginAdblocker({
|
||||||
|
blockTrackers: true
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function disableBlockerInPage(page: Page) {
|
||||||
|
const blockerObject = await adBlocker.getBlocker();
|
||||||
|
await blockerObject.disableBlockingInPage(page);
|
||||||
|
}
|
||||||
+2
-2
@@ -1,14 +1,14 @@
|
|||||||
import puppeteer from 'puppeteer-extra';
|
import puppeteer from 'puppeteer-extra';
|
||||||
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
|
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
|
||||||
import adblockerPlugin from 'puppeteer-extra-plugin-adblocker';
|
|
||||||
import {Config} from './config';
|
import {Config} from './config';
|
||||||
import {Stores} from './store/model';
|
import {Stores} from './store/model';
|
||||||
import {Logger} from './logger';
|
import {Logger} from './logger';
|
||||||
import {tryLookupAndLoop} from './store';
|
import {tryLookupAndLoop} from './store';
|
||||||
import {getSleepTime} from './util';
|
import {getSleepTime} from './util';
|
||||||
|
import {adBlocker} from './adblocker';
|
||||||
|
|
||||||
puppeteer.use(stealthPlugin());
|
puppeteer.use(stealthPlugin());
|
||||||
puppeteer.use(adblockerPlugin({blockTrackers: true}));
|
puppeteer.use(adBlocker);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the bot.
|
* Starts the bot.
|
||||||
|
|||||||
+3
-3
@@ -5,7 +5,7 @@ import open from 'open';
|
|||||||
import {Store} from './model';
|
import {Store} from './model';
|
||||||
import {sendNotification} from '../notification';
|
import {sendNotification} from '../notification';
|
||||||
import {includesLabels} from './includes-labels';
|
import {includesLabels} from './includes-labels';
|
||||||
import {delay, getSleepTime} from '../util';
|
import {closePage, delay, getSleepTime} from '../util';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the brand should be checked for stock
|
* Returns true if the brand should be checked for stock
|
||||||
@@ -62,7 +62,7 @@ async function lookup(browser: Browser, store: Store) {
|
|||||||
response = await page.goto(link.url, {waitUntil: 'networkidle0'});
|
response = await page.goto(link.url, {waitUntil: 'networkidle0'});
|
||||||
} catch {
|
} catch {
|
||||||
Logger.error(`✖ [${store.name}] ${graphicsCard} skipping; timed out`);
|
Logger.error(`✖ [${store.name}] ${graphicsCard} skipping; timed out`);
|
||||||
await page.close();
|
await closePage(page);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ async function lookup(browser: Browser, store: Store) {
|
|||||||
sendNotification(givenUrl, link);
|
sendNotification(givenUrl, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
await page.close();
|
await closePage(page);
|
||||||
}
|
}
|
||||||
/* eslint-enable no-await-in-loop */
|
/* eslint-enable no-await-in-loop */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import {Page} from 'puppeteer';
|
||||||
import {Config} from './config';
|
import {Config} from './config';
|
||||||
|
import {disableBlockerInPage} from './adblocker';
|
||||||
|
|
||||||
export function getSleepTime() {
|
export function getSleepTime() {
|
||||||
return Config.browser.minSleep + (Math.random() * (Config.browser.maxSleep - Config.browser.minSleep));
|
return Config.browser.minSleep + (Math.random() * (Config.browser.maxSleep - Config.browser.minSleep));
|
||||||
@@ -9,3 +11,8 @@ export async function delay(ms: number) {
|
|||||||
setTimeout(resolve, ms);
|
setTimeout(resolve, ms);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function closePage(page: Page) {
|
||||||
|
await disableBlockerInPage(page);
|
||||||
|
await page.close();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user