Warm tip: This article is reproduced from serverfault.com, please click

Importing customized module into renderer.js in Electron app

发布于 2020-12-01 04:46:44

I have a customized module that carries some functions that I would like to use in renderer.js. I tried the following ways of importing but it does not work (in fact, it causes some of the other functions in renderer.js to not execute as well.

const supp = require('./supp.js')

const supp = require('./supp')

const supp = require('supp.js')

const supp = require(path.join(__dirname, '/supp.js')

import 'supp'

supp.js sits in the same folder level as renderer.js and main.js. If someone could advise? Thanks.

Update: below is all the code in file supp.js

const pinOneInGrp = (itemID, grpName, itemColor, grpColor) => {
  let item = document.getElementById(itemID);
  let grpItems = document.getElementsByClassName(grpName);
  for(var i = 0; i < grpItems.length;i++) {
    grpItems[i].style.background-color = grpColor
  }
  item.style.background-color = itemColor;
}

module.exports = {
  pinOneInGrp
}

If one of the import or require lines above is included at the top of renderer.js, none of the subsequent actions in renderer.js is executed. For example, there is a ipc.send() and ipc.on() action right after the import / require line. These two do not send (and hence, receive back) from the main.js.

Questioner
Tristan Tran
Viewed
0
Alexander Leithner 2020-12-01 19:53:56

The code you've posted contains a typo. The error it's throwing (which you most probably can't see) is a SyntaxError, because you cannot subtract color (which is undefined) from grpItems[i].style.background and then assign to it. Thus, you simply have to correct your for-loo from

for (var i = 0; i < grpItems.length; i++) {
    grpItems[i].style.background-color = grpColor;
}

to

for (var i = 0; i < grpItems.length; i++) {
    grpItems[i].style.backgroundColor = grpColor;
}

(And the same goes for the style assignment right below the for-loop!)

Note that all CSS properties which are spelt with a hyphen when in a stylesheet must use camelCase as they otherwise would denote a subtraction, which is causing your problems. Also, this behaviour is explained in Mozilla's Developer Network Web API reference, specifically under "Setting styles".