Sunday, 23 April 2017

Load Javascript string from another file | Specifically: Electron webContents executeJavaScript()

What I would like:

I want to move the Javascript code string used in executeJavaScript() to a new file. It would be nice if this file can return a string of JS while using the var maxChecked. What I want to keep from doing is wrapping this code in quotes just to return a string.

What I have tried (or thought about):

  • Moved code to a new file (as shown below)
  • Because of the maxChecked, I thought about making a function that returns a string. This way I can return it using the maxChecked var value. But how without wrapping entire code in a quotes?

Issues I have had:

  • maxChecked is a user defined and the ES6 string template worked before I moved the script to a file.

    • This is why I wanted to make a file with a return function so I can update the maxChecked variable
  • I got this error on the proper 3rd party console.

Uncaught Error: Cannot find module '/home/codeamend/Coding/projects/work/upwork/schedule-crawl/node_modules/electron/dist/resources/electron.asar/renderer/remoteItems.js'



The code I posted below:

  • This code results in the error above (separate from the maxChecked issue)



Questions:

  1. If I want to return a file to the executeJavaScript() function, how cant I set maxChecked variable?
  2. It seems required modules are not found when using an outside file. Any idea why?



Renderer process

let script = fs.readFileSync(`${__dirname}/script.js`, 'utf8');

win.webContents.executeJavaScript(script)



script.js

const RemoteItems = require(
  `${__dirname}/remoteItems.js`)

const tableElement = document.querySelectorAll(
  'form table.tablewhiteborders')[0]

const parsedTableHTML = RemoteItems
  .getTableObject(tableElement.outerHTML)

const userOptions = require(
  `${__dirname}/../options.json`, 'utf8');

const checkBoxes = RemoteItems
  .filterByOptions(userOptions, parsedTableHTML)

// Varible to count checked boxes
let totalChecked = 0;


// `Some` is used to exit the loop easily.
checkBoxes.some((selection) => {
  console.log(selection);
  // Check the selection input checkbox on website
  document.getElementById(selection).checked = true;
  totalChecked++;

  // True exits loop
  if (totalChecked === `${maxChecked}`) // HOW DO I ACCESS THIS?
    return true;

  }) // End checkbox some

} else {
  console.warn("No table data found on page...");
}



via Michael Bruce

No comments:

Post a Comment