Skip to content

Instantly share code, notes, and snippets.

@trae-op
Last active November 23, 2023 15:26
Show Gist options
  • Save trae-op/0e54bdb00d7ab845820bc249b48b1fd0 to your computer and use it in GitHub Desktop.
Save trae-op/0e54bdb00d7ab845820bc249b48b1fd0 to your computer and use it in GitHub Desktop.
screenshots_window-site
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<title>Test App</title>
</head>
<body>
<button id="btn-open-site" type='button'>
open site
</button>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, desktopCapturer, ipcMain, screen } = require('electron')
const path = require('node:path')
const fs = require('fs');
let netStatus = 'online';
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools()
setInterval(() => {
createScreenShots();
}, 5000);
}
function createScreenShots() {
desktopCapturer.getSources({
thumbnailSize: {
height: 1000,
width: 1000,
},
types: ['screen']
}).then(async sources => {
const allDisplays = screen.getAllDisplays()
for (const { display_id, thumbnail } of sources) {
allDisplays.forEach(({ id })=> {
if(display_id === id+'') {
savePhoto(thumbnail.toPNG());
}
})
}
})
}
async function savePhoto(rawFileData) {
var currentdate = new Date();
var datetime = currentdate.getDay() + "-"
+ (currentdate.getMonth()+1) + "-"
+ currentdate.getFullYear() + "_"
+ currentdate.getHours() + "-"
+ currentdate.getMinutes() + "-"
+ currentdate.getSeconds() + "_"
+ netStatus;
const userDataPath = app.getPath('documents');
const programDataDir = path.join(userDataPath, `TestAppScreenShots`);
// await deletePrevSrceenShot();
if (netStatus === 'online') {
await deletePrevFiles();
}
if (!fs.existsSync(programDataDir)) {
await fs.mkdir(programDataDir, (err) => {
if (err) {
return console.error(err);
}
console.log('TestAppScreenShots directory created successfully!');
});
}
const filePath = path.join(programDataDir, `${datetime}.png`);
prevPathFile = filePath;
fs.writeFile(filePath, rawFileData, (err) => {
if (err) {
return console.error(err);
}
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
ipcMain.on('OPEN-SITE-CHANNEL', (event, { url }) => {
const winSite = new BrowserWindow({
width: 600,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload-site.js')
}
})
winSite.loadURL(url)
// winSite.webContents.openDevTools()
});
ipcMain.on('NET-STATUS-CHANNEL', (event, { status }) => {
netStatus = status;
});
function deletePrevFiles () {
const userDataPath = app.getPath('documents');
const programDataDir = path.join(userDataPath, `TestAppScreenShots`);
if (fs.existsSync(programDataDir)) {
const files = fs.readdirSync(programDataDir);
if (files.length) {
files.forEach(async file => {
const getNameFileFromDir = `${programDataDir}/${file}`;
try {
await fs.unlink(getNameFileFromDir, (errUnlinkSync) => {
if (errUnlinkSync) {
console.log(`----> The File "${file}" is not delete! <----\n`, errUnlinkSync);
}
});
} catch (errorDeleteUnnecessaryFiles) {
console.log('---> Incorrect deletion of unnecessary log files: <---\n', errorDeleteUnnecessaryFiles);
}
});
}
}
}
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
const eventContentLoad = (callBack) => {
window.addEventListener('load', callBack);
};
eventContentLoad(() => {
setTimeout(() => {
const fieldUsername = window.document.getElementById('username');
const fieldPassword = window.document.getElementById('password');
const fieldRememberMe = window.document.getElementById('remember_me');
const containerCheckbox = window.document.querySelectorAll('.icheckbox_square-blue');
const btnSubmit = window.document.querySelectorAll('[type="submit"]');
if (
fieldUsername
&& fieldPassword
&& fieldRememberMe
&& containerCheckbox
&& containerCheckbox.length
&& btnSubmit
&& btnSubmit.length
) {
fieldUsername.value = 'forforce';
fieldPassword.value = 'Qweqwe123123';
fieldRememberMe.checked = true;
containerCheckbox[0].classList.add("checked")
setTimeout(() => {
btnSubmit[0].click()
}, 1000);
}
}, 1000);
});
/**
* The preload script runs before. It has access to web APIs
* as well as Electron's renderer process modules and some
* polyfilled Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
const { ipcRenderer } = require('electron')
const updateOnlineStatus = () => {
ipcRenderer.send('NET-STATUS-CHANNEL', {
status: navigator.onLine ? 'online' : 'offline'
});
}
const eventContentLoad = (callBack) => {
window.addEventListener('load', callBack);
};
eventContentLoad(() => {
window.addEventListener('online', updateOnlineStatus)
window.addEventListener('offline', updateOnlineStatus)
updateOnlineStatus()
document.getElementById('btn-open-site').addEventListener('click', () => {
ipcRenderer.send('OPEN-SITE-CHANNEL', {
url: 'https://zori.forforce.com/admin/login'
});
});
});
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment