AlexTorresDev / custom-electron-titlebar

Custom electon title bar inpire on VS Code title bar
MIT License
868 stars 149 forks source link

Hos to use this library securely? #91

Closed ThomasStubbe closed 4 years ago

ThomasStubbe commented 4 years ago

As commonly known, it's bad practice to expose 'require' to the renderer if you're loading anything from the web, and it seems that this library is impossible to load from main or preload. How to use this library?

AlexTorresDev commented 4 years ago

If you check the example folder, you can see that the library is used in preload.js

// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const path = require('path');
const url = require('url');

//const customTitlebar = require('custom-electron-titlebar');
const customTitlebar = require('..'); // Delete this line and uncomment top line

window.addEventListener('DOMContentLoaded', () => {
  const titlebar = new customTitlebar.Titlebar({
    backgroundColor: customTitlebar.Color.fromHex('#000'),
    itemBackgroundColor: customTitlebar.Color.fromHex('#222'),
    icon: "img/icon48.png",
    menuPosition: "bottom",

    titleHorizontalAlignment: "left",
    minimizable: true,
    maximizable: false,
  });

  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

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