Preload in electron.js

Is someone familiar with electron here? If yes, can you explain to me the preload js script?

// from electron's official docs
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const dependency of ['chrome', 'node', 'electron']) {
    replaceText(`${dependency}-version`, process.versions[dependency])
  }
})

The code you have posted above means:

When HTML document has been completely loaded do the following:

  1. create a function that would get hold of a selector using getElementById method and replace the text with the value from the “text” parameter.

  2. Take the function declared above and assign it to replaceText constant.

  3. Loop the elements from this array [‘chrome’ … etc, then use the function declared above to print out a text with the version of whatever property is dynamically accessed in this part of the program process.versions[dependency]

Then the function would do for each of the elements of the array:

  • for an element with id ‘chome-version’ I am going to put whatever value is inside process.versions.chrome.

  • for an element with id ‘node-version’ I am going to put whatever value is inside process.versions.node.

  • for an element with id ‘electron-version’ I am going to put whatever value is inside process.versions.electron.

Hope this helps.

1 Like

ahh now i see what that array meant , thanks !!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.