Advertisement
googolaire

Renderer.js

Apr 19th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This file is required by the index.html file and will
  2. // be executed in the renderer process for that window.
  3. // All of the Node.js APIs are available in this process.
  4. const {desktopCapturer, screen} = require('electron');
  5.  
  6. /**
  7.  * Create a screenshot of your electron app. You can modify which process to render in the conditional line #61.
  8.  * In this case, filtered using the title of the document.
  9.  *
  10.  * @param callback {Function} callback receives as first parameter the base64 string of the image
  11.  * @param imageFormat {String} Format of the image to generate ('image/jpeg' or 'image/png')
  12.  **/
  13. function appScreenshot(callback,imageFormat) {
  14.      var _this = this;
  15.      this.callback = callback;
  16.      imageFormat = imageFormat || 'image/jpeg';
  17.      
  18.      this.handleStream = (stream) => {
  19.          // console.log('stream',stream);
  20.          // Create hidden video tag
  21.          var video = document.createElement('video');
  22.          video.style.cssText = 'position:absolute;top:-10000px;left:-10000px;';
  23.          // Event connected to stream
  24.          video.onloadedmetadata = function () {
  25.              // Set video ORIGINAL height (screenshot)
  26.              video.style.height = this.videoHeight + 'px'; // videoHeight
  27.              video.style.width = this.videoWidth + 'px'; // videoWidth
  28.  
  29.              // Create canvas
  30.              var canvas = document.createElement('canvas');
  31.              canvas.width = this.videoWidth;
  32.              canvas.height = this.videoHeight;
  33.              var ctx = canvas.getContext('2d');
  34.              // Draw video on canvas
  35.              ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
  36.  
  37.              if (_this.callback) {
  38.                  // Save screenshot to jpg - base64
  39.                  _this.callback(canvas.toDataURL(imageFormat));
  40.              } else {
  41.                  console.log('Need callback!');
  42.              }
  43.  
  44.              // Remove hidden video tag
  45.              video.remove();
  46.              try {
  47.                  // Destroy connect to stream
  48.                  stream.getTracks()[0].stop();
  49.              } catch (e) {}
  50.          }
  51.          video.src = URL.createObjectURL(stream);
  52.          document.body.appendChild(video);
  53.      };
  54.  
  55.      this.handleError = function(e) {
  56.          console.log(e);
  57.      };
  58.  
  59.      desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => {
  60.          if (error) throw error;
  61.          // console.log(sources);
  62.          for (let i = 0; i < sources.length; ++i) {
  63.              console.log(sources);
  64.              // Filter: main screen
  65.              if (sources[i].name === document.title) {
  66.                  navigator.webkitGetUserMedia({
  67.                      audio: false,
  68.                      video: {
  69.                          mandatory: {
  70.                              chromeMediaSource: 'desktop',
  71.                              chromeMediaSourceId: sources[i].id,
  72.                              minWidth: 1280,
  73.                              maxWidth: 4000,
  74.                              minHeight: 720,
  75.                              maxHeight: 4000
  76.                          }
  77.                      }
  78.                  }, this.handleStream, this.handleError);
  79.                  return
  80.              }
  81.          }
  82.      });
  83.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement