Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chrome-driver.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const ChildProcess = require('child_process');
  2. const path = require('path');
  3. const { default: got } = require('got');
  4. const split = require('split');
  5. function ChromeDriver(
  6. host,
  7. port,
  8. nodePath,
  9. startTimeout,
  10. workingDirectory,
  11. chromeDriverLogPath
  12. ) {
  13. this.host = host;
  14. this.port = port;
  15. this.nodePath = nodePath;
  16. this.startTimeout = startTimeout;
  17. this.workingDirectory = workingDirectory;
  18. this.chromeDriverLogPath = chromeDriverLogPath;
  19. this.path = require.resolve('electron-chromedriver/chromedriver');
  20. this.urlBase = '/';
  21. this.statusUrl =
  22. 'http://' + this.host + ':' + this.port + this.urlBase + 'status';
  23. this.logLines = [];
  24. }
  25. ChromeDriver.prototype.start = function () {
  26. if (this.process) throw new Error('ChromeDriver already started');
  27. const args = [this.path, '--port=' + this.port, '--url-base=' + this.urlBase];
  28. if (this.chromeDriverLogPath) {
  29. args.push('--verbose');
  30. args.push('--log-path=' + this.chromeDriverLogPath);
  31. }
  32. const options = {
  33. cwd: this.workingDirectory,
  34. env: this.getEnvironment()
  35. };
  36. this.process = ChildProcess.spawn(this.nodePath, args, options);
  37. const self = this;
  38. this.exitHandler = function () {
  39. self.stop();
  40. };
  41. global.process.on('exit', this.exitHandler);
  42. this.setupLogs();
  43. return this.waitUntilRunning();
  44. };
  45. ChromeDriver.prototype.waitUntilRunning = function () {
  46. const self = this;
  47. return new Promise(function (resolve, reject) {
  48. const startTime = Date.now();
  49. const checkIfRunning = function () {
  50. self.isRunning(function (running) {
  51. if (!self.process) {
  52. return reject(Error('ChromeDriver has been stopped'));
  53. }
  54. if (running) {
  55. return resolve();
  56. }
  57. const elapsedTime = Date.now() - startTime;
  58. if (elapsedTime > self.startTimeout) {
  59. return reject(
  60. Error(
  61. 'ChromeDriver did not start within ' + self.startTimeout + 'ms'
  62. )
  63. );
  64. }
  65. global.setTimeout(checkIfRunning, 100);
  66. });
  67. };
  68. checkIfRunning();
  69. });
  70. };
  71. ChromeDriver.prototype.setupLogs = function () {
  72. const linesToIgnore = 2; // First two lines are ChromeDriver specific
  73. let lineCount = 0;
  74. this.logLines = [];
  75. const self = this;
  76. this.process.stdout.pipe(split()).on('data', function (line) {
  77. if (lineCount < linesToIgnore) {
  78. lineCount++;
  79. return;
  80. }
  81. self.logLines.push(line);
  82. });
  83. };
  84. ChromeDriver.prototype.getEnvironment = function () {
  85. const env = {};
  86. Object.keys(process.env).forEach(function (key) {
  87. env[key] = process.env[key];
  88. });
  89. if (process.platform === 'win32') {
  90. env.SPECTRON_NODE_PATH = process.execPath;
  91. env.SPECTRON_LAUNCHER_PATH = path.join(__dirname, 'launcher.js');
  92. }
  93. return env;
  94. };
  95. ChromeDriver.prototype.stop = function () {
  96. if (this.exitHandler) global.process.removeListener('exit', this.exitHandler);
  97. this.exitHandler = null;
  98. if (this.process) this.process.kill();
  99. this.process = null;
  100. this.clearLogs();
  101. };
  102. ChromeDriver.prototype.isRunning = function (callback) {
  103. const cb = false;
  104. got(this.statusUrl)
  105. .json()
  106. .then(({ value }) => callback(value && value.ready))
  107. .catch(() => callback(cb));
  108. };
  109. ChromeDriver.prototype.getLogs = function () {
  110. return this.logLines.slice();
  111. };
  112. ChromeDriver.prototype.clearLogs = function () {
  113. this.logLines = [];
  114. };
  115. module.exports = ChromeDriver;