<commit_before>'use strict';

var app = require('app');
var BrowserWindow = require('browser-window');
var ipc = require('ipc');
var sock = require('axon').socket('rep');

var show = process.env.NODESCREENSHOT_SHOW === '1' ? true: false;

sock.connect(parseInt(process.env.PORT, 10));

app.on('ready', function() {

  if ( process.platform === 'darwin' && app.dock.hide !== undefined ) {
    app.dock.hide();
  }

  var emitSuccess = function( reply, cleanup, data ) {
    reply(null, data, cleanup);
  };

  var emitError = function( reply, error ) {
    reply(error, null);
  };

  sock.on('message', function(task, options, reply){
    switch (task) {
      case 'take-screenshot' :
        takeScreenshot(
          options,
          emitSuccess.bind(null, reply),
          emitError.bind(null, reply)
        );
        break;
    }
  });

  sock.on('close', function(){
    app.terminate();
    process.exit();
  });
});


function takeScreenshot(options, sCall, eCall) {

  var popupWindow = new BrowserWindow({
    x:0,
    y:0,
    width: options.width,
    height: options.height,
    show: show,
    frame: false,
    preload: __dirname + '/preload.js',
    transparent: options.transparent || false,
    'enable-larger-than-screen': true,
    'skip-taskbar': true,
    'overlay-scrollbars': true,
    'direct-write': true
  });

  var cleanup = function(){
    popupWindow.webContents.executeJavaScript('window["__atom__close"]()');
    popupWindow = null;
  };

  var loadTimeout;
  var resetTimeout = function(func){
    clearTimeout(loadTimeout);
    loadTimeout = setTimeout(func, options.timeout || 2000);
  };

  var makeScreenshot = function(){
    // Remove any loadTimeout
    clearTimeout(loadTimeout);

    var loadEvent = 'Loaded-' + popupWindow.id;
    var sizeEvent = 'Size-' + popupWindow.id;

    // requestAnimationFrame will call the function before the next repaint.
    // This way it is ensured that at least on paint has happend.
    popupWindow.webContents.executeJavaScript(
      'var __atom__ra = window.requestAnimationFrame;' +
      'var __atom__ipc = require("ipc");' +
      'function __atom__close(){require("remote").getCurrentWindow().close();}' +
      'function __atom__load(){__atom__ipc.send("'+loadEvent+'");};' +
      'function __atom__size(){__atom__ipc.send("'+sizeEvent+'",{width: document.body.clientWidth, height: document.body.clientHeight});};' +
      'window["__atom__loaded__"] = function(){'+
        '__atom__ra(function(){' +
          // Be sure to render the whole page before taking the screenshot
          'document.body.scrollTop=document.body.clientHeight;' +
          '__atom__ra(function(){'+
            '__atom__ra(function(){'+
              // Take screenshot at offset
              'document.body.scrollTop='+(options.pageOffset || 0)+';'+
              '__atom__ra(__atom__load);'+
            '});' +
          '});' +
        '});' +
      '}');

    // Inject custom CSS if necessary
    if (options.css !== undefined) {
      popupWindow.webContents.insertCSS(options.css);
    }

    // Register the IPC load event once
    ipc.once(loadEvent, function(){
      // Delay the screenshot
      setTimeout(function(){
        var cb = sCall.bind(null, cleanup);

        if (typeof options.crop === 'object') {
          popupWindow.capturePage(options.crop, cb);
        }else{
          popupWindow.capturePage(cb);
        }

      }, options.delay * 1000);
    });

    // Register the IPC sizeEvent once
    ipc.once(sizeEvent, function(e, data){
      popupWindow.setSize(data.width, data.height);
      popupWindow.webContents.executeJavaScript('window["__atom__loaded__"]()');
    });


    // Resize to the correct size
    // ensures that all styles are up to date
    // hiding the scrollbars need a browser resize
    popupWindow.setSize(0, 0);
    popupWindow.setSize(options.width, options.height);

    if (options.page) {
      popupWindow.webContents.executeJavaScript('window["__atom__size"]()');
    } else {
      popupWindow.webContents.executeJavaScript('window["__atom__loaded__"]()');
    }
  };

  popupWindow.webContents.on('did-fail-load', function(e, errorCode, errorDescription) {
    eCall(new Error(errorDescription));
    cleanup();
  });

  popupWindow.webContents.on('crashed', function() {
    eCall(new Error('Render process crashed'));
    cleanup();
  });

  var asked = false;<commit_msg>Fix linux<commit_after>'use strict';

var app = require('app');
var BrowserWindow = require('browser-window');
var ipc = require('ipc');
var sock = require('axon').socket('rep');

var show = process.env.NODESCREENSHOT_SHOW === '1' ? true: false;

sock.connect(parseInt(process.env.PORT, 10));

app.on('ready', function() {

  if ( process.platform === 'darwin' && app.dock.hide !== undefined ) {
    app.dock.hide();
  }

  var emitSuccess = function( reply, cleanup, data ) {
    reply(null, data, cleanup);
  };

  var emitError = function( reply, error ) {
    reply(error, null);
  };

  sock.on('message', function(task, options, reply){
    switch (task) {
      case 'take-screenshot' :
        takeScreenshot(
          options,
          emitSuccess.bind(null, reply),
          emitError.bind(null, reply)
        );
        break;
    }
  });

  sock.on('close', function(){
    app.terminate();
    process.exit();
  });
});


function takeScreenshot(options, sCall, eCall) {

  var popupWindow = new BrowserWindow({
    x:0,
    y:0,
    width: options.width,
    height: options.height,
    show: show,
    frame: false,
    preload: __dirname + '/preload.js',
    transparent: options.transparent || false,
    'enable-larger-than-screen': true,
    'skip-taskbar': true,
    'overlay-scrollbars': true,
    'direct-write': true
  });

  var cleanup = function(){
    popupWindow.webContents.executeJavaScript('window["__atom__close"]()');
    popupWindow = null;
  };

  var loadTimeout;
  var resetTimeout = function(func){
    clearTimeout(loadTimeout);
    loadTimeout = setTimeout(func, options.timeout || 2000);
  };

  var makeScreenshot = function(){
    // Remove any loadTimeout
    clearTimeout(loadTimeout);

    var loadEvent = 'Loaded-' + popupWindow.id;
    var sizeEvent = 'Size-' + popupWindow.id;

    // requestAnimationFrame will call the function before the next repaint.
    // This way it is ensured that at least on paint has happend.
    popupWindow.webContents.executeJavaScript(
      'var __atom__ra = window.requestAnimationFrame;' +
      'var __atom__ipc = require("ipc");' +
      'function __atom__close(){require("remote").getCurrentWindow().close();}' +
      'function __atom__load(){__atom__ipc.send("'+loadEvent+'");};' +
      'function __atom__size(){__atom__ipc.send("'+sizeEvent+'",{width: document.body.clientWidth, height: document.body.clientHeight});};' +
      'window["__atom__loaded__"] = function(){'+
        '__atom__ra(function(){' +
          // Be sure to render the whole page before taking the screenshot
          'document.body.scrollTop=document.body.clientHeight;' +
          '__atom__ra(function(){'+
            '__atom__ra(function(){'+
              // Take screenshot at offset
              'document.body.scrollTop='+(options.pageOffset || 0)+';'+
              '__atom__ra(__atom__load);'+
            '});' +
          '});' +
        '});' +
      '}');

    // Inject custom CSS if necessary
    if (options.css !== undefined) {
      popupWindow.webContents.insertCSS(options.css);
    }

    // Register the IPC load event once
    ipc.once(loadEvent, function(){
      // Delay the screenshot
      setTimeout(function(){
        var cb = sCall.bind(null, cleanup);

        if (typeof options.crop === 'object') {
          popupWindow.capturePage(options.crop, cb);
        }else{
          popupWindow.capturePage(cb);
        }

      }, options.delay * 1000);
    });

    // Register the IPC sizeEvent once
    ipc.once(sizeEvent, function(e, data){
      popupWindow.setSize(data.width, data.height);
      popupWindow.webContents.executeJavaScript('window["__atom__loaded__"]()');
    });


    // Resize to the correct size
    // ensures that all styles are up to date
    // hiding the scrollbars need a browser resize
    popupWindow.setSize(10, 10); // Linux can't handle 0, 0
    popupWindow.setSize(options.width, options.height);

    if (options.page) {
      popupWindow.webContents.executeJavaScript('window["__atom__size"]()');
    } else {
      popupWindow.webContents.executeJavaScript('window["__atom__loaded__"]()');
    }
  };

  popupWindow.webContents.on('did-fail-load', function(e, errorCode, errorDescription) {
    eCall(new Error(errorDescription));
    cleanup();
  });

  popupWindow.webContents.on('crashed', function() {
    eCall(new Error('Render process crashed'));
    cleanup();
  });

  var asked = false;
{'fraction_non_alphanumeric': 0.15068338416356036, 'fraction_numerical': 0.004066418163334463, 'mean_word_length': 3.0208900999091735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1324959', 'n_tokens_mistral': 2762, 'n_tokens_neox': 2640, 'n_words': 567}