Thank you in advance for your help.
I want to create a script for web scraping using different technologies. This is my main script, and the goal is to ensure that it cannot be changed.
//main//
const main = async (page) => {
const url = 'https://app.proxiesapi.com/login/index.php';
await page.goto(url);
await page.waitForFunction(() => document.readyState === 'complete')
const email = '[email protected]';
await page.evaluate(({ email }) => {
document.querySelector('#myusername').value = email;
}, { email });
const inputFieldValue = await page.evaluate(() => {
return document.querySelector('#myusername').value;
});
console.log({ inputFieldValue });
return page;
};
export { main };
that is my script electron :
import { app, BrowserWindow, session } from 'electron';
import { main } from './script.js';
// create browser /page //
export async function createBrowser() {
await app.whenReady();
const browserWindow = new BrowserWindow({ width: 800, height: 600, show: true });
return browserWindow;
}
async function createTab(browser) {
return browser;
}
// go to url//
async function goto(page, url) {
await page.loadURL(url);
return page;
}
// evaluate electron style//
const evaluate = async (page, func, args) => {
const funcStr = `(${func})(${JSON.stringify(args)})`;
return await page.webContents.executeJavaScript(funcStr);
}
// wait Load //
const waitForFunction = async (page, func) => {
return new Promise((resolve) => {
const intervalId = setInterval(() => {
page.webContents.executeJavaScript(`(${func})()`).then((result) => {
if (result) {
clearInterval(intervalId);
resolve(result);
}
}).catch(console.error);
}, 100);
});
}
async function clear(tab, browser) {
await session.defaultSession.clearStorageData();
tab.close();
await browser.close();
}
// Call function //
(async () => {
const browser = await createBrowser();
let page = await createTab(browser);
page.goto = goto.bind(null, page);
page.waitForFunction = waitForFunction.bind(null, page);
page.evaluate = evaluate.bind(null, page);
page = await main(page);
await clear(page, browser);
})();
It is functional, but I do not want to use things like eval() or executeJavaScript. Instead, I would like to directly inject my script into the page. Do you know how to do that with Electron?
Yahnis allegret is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.