#Best way to load data from API on launch
36 messages · Page 1 of 1 (latest)
its the https://rocketlaunch.live api and i know a bit about nodejs/js just not sure how to get a seperate js file running with electron right before the page loads as i dont want to show the page until the data is loaded
Live coverage and the most up-to-date schedule of all upcoming orbital rocket launches, including SpaceX, ULA, Arianespace and others. Check back for live coverage on launch day!
i already understand the API
just need help getting it pulled into electron and able to show it in the html
you can send message from page when to show it
for electron it is same way you do in nodejs/js
api request
i get that but how do i get the data from the main process to the render process
with ipc
Use the ipcMain and ipcRenderer modules to communicate between Electron processes
i think Pattern 2 is a good way
thank you sorry just new to the electron stuff not 100% understanding yet
im trying to use the main to renderer one but something aint working and idk what im missing
preload.js
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('API', {
LoadData: (callback) => ipcRenderer.on('loadData', callback)
})
index.js
const loading = document.getElementById("loading")
const launchName = document.getElementById("nextLaunchName")
window.API.LoadData((event, value) => {
launchName.innerHTML = value;
})
main.js
// Import the required modules
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const axios = require('axios')
// Create a new window and load the index.html file
async function createWindow() {
const mainWindow = new BrowserWindow({
width: 1250,
height: 703,
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
console.log("test")
const res = await axios.get("https://fdo.rocketlaunch.live/json/launches/next")
const result = res.data
mainWindow.webContents.send("loadData", result)
mainWindow.loadFile('home.html');
}
// Initialize the app and create the window when the app is ready
app.whenReady().then(async () => {
createWindow();
// Quit the app when all windows are closed
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
});
// Activate the app when no windows are open
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
home.html this is the script i put in and is in correct location
<script src="./index.js"></script>
probably because you send message before listener is added
how do i make sure to send it after then?
pattern 2 requires the user to interact tho from what it looks like to me and i want it to just load the data with no interaction
it doesnt
then i dont understand it enough
how does it know when to ask is what im confused on
i can try but i feel i will do it wrong considering the example is based on a user click so its hard to understand for my situation
its just an example
still aint working idk tbh im doing something wrong
i followed the example as close as possible considering it does not match my situation
nvm i got it finally sorry for being so confused just its all new to me