preload.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. var _electron = require('electron');
  3. var _path = require('path');
  4. var _path2 = _interopRequireDefault(_path);
  5. var _fs = require('fs');
  6. var _fs2 = _interopRequireDefault(_fs);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var INJECT_JS_PATH = _path2.default.join(__dirname, '../../', 'inject/inject.js'); /**
  9. Preload file that will be executed in the renderer process
  10. */
  11. var log = require('loglevel');
  12. /**
  13. * Patches window.Notification to set a callback on a new Notification
  14. * @param callback
  15. */
  16. function setNotificationCallback(callback) {
  17. var OldNotify = window.Notification;
  18. var newNotify = function newNotify(title, opt) {
  19. callback(title, opt);
  20. return new OldNotify(title, opt);
  21. };
  22. newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
  23. Object.defineProperty(newNotify, 'permission', {
  24. get: function get() {
  25. return OldNotify.permission;
  26. }
  27. });
  28. window.Notification = newNotify;
  29. }
  30. function injectScripts() {
  31. var needToInject = _fs2.default.existsSync(INJECT_JS_PATH);
  32. if (!needToInject) {
  33. return;
  34. }
  35. // Dynamically require scripts
  36. // eslint-disable-next-line global-require, import/no-dynamic-require
  37. require(INJECT_JS_PATH);
  38. }
  39. setNotificationCallback(function (title, opt) {
  40. _electron.ipcRenderer.send('notification', title, opt);
  41. });
  42. document.addEventListener('DOMContentLoaded', function () {
  43. injectScripts();
  44. });
  45. _electron.ipcRenderer.on('params', function (event, message) {
  46. var appArgs = JSON.parse(message);
  47. log.info('nativefier.json', appArgs);
  48. });
  49. _electron.ipcRenderer.on('debug', function (event, message) {
  50. // eslint-disable-next-line no-console
  51. log.info('debug:', message);
  52. });
  53. //# sourceMappingURL=preload.js.map