Ask Your Question
3

How to utilize Electron for loading an external site and store messages in JSON format?

asked 2022-12-16 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-10-14 10:00:00 +0000

lalupa gravatar image

To utilize Electron for loading an external site and store messages in JSON format, you can follow these steps:

  1. Create a new Electron project using the Electron Quick Start Template.
  2. Install the request module using npm to make API requests.
  3. Use the BrowserWindow module to load the external website.
  4. Implement a select box to select different pages of the external website.
  5. Use the request module to make API requests to the selected page and parse the JSON response.
  6. Store the JSON data in a local file using the fs module in Node.js.
  7. Use dialog boxes to display success or error messages to the user.

Here's a sample code to get started:

const electron = require('electron');
const url = require('url');
const path = require('path');
const request = require('request');
const { app, BrowserWindow, Menu, dialog } = electron;
const fs = require('fs');

// Create a new window
let mainWindow;
app.on('ready', () => {
  mainWindow = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true
    }
  });

  // Load the external website
  mainWindow.loadURL('https://www.example.com');

  // Create a menu template
  const menuTemplate = [{
      label: 'File',
      submenu: [{
        label: 'Save JSON',
        accelerator: process.platform == 'darwin' ? 'Command+S' : 'Ctrl+S',
        click() {
          saveJSON();
        }
      }]
    }
  ];

  // Add the menu to the window
  const menu = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(menu);

  // Select page select box
  const selectBox = document.getElementById('selectbox');
  selectBox.onchange = function() {
    const selectedPage = selectBox.value;
    request(`https://www.example.com/api/pages/${selectedPage}`, {json: true}, (error, response, body) => {
      if (error) {
        dialog.showErrorBox('Error', 'Unable to retrieve JSON data.');
      } else {
        const data = JSON.stringify(body);
        fs.writeFile('data.json', data, (err) => {
          if (err) {
            dialog.showErrorBox('Error', 'Unable to store JSON data.');
          } else {
            dialog.showMessageBox(mainWindow, {message: 'JSON data has been saved sucessfully.'});
          }
        });
      }
    });
  }
});

// Function to save JSON data to a local file
function saveJSON() {
  dialog.showSaveDialog(mainWindow, {defaultPath: 'data.json'}, (filename) => {
    if (filename === undefined) {
      dialog.showErrorBox('Error', 'Unable to save JSON data.');
      return;
    }
    fs.readFile('data.json', 'utf-8', (err, data) => {
      if (err) {
        dialog.showErrorBox('Error', 'Unable to read JSON data.');
      } else {
        fs.writeFile(filename, data, (err) => {
          if (err) {
            dialog.showErrorBox('Error', 'Unable to save JSON data.');
          } else {
            dialog.showMessageBox(mainWindow, {message: 'JSON data has been saved sucessfully.'});
          }
        });
      }
    });
  });
}

Note that this is just a sample code and you will have to modify it according to your needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-16 11:00:00 +0000

Seen: 11 times

Last updated: Oct 14 '21