如何使用nodejs禁用Chrome的会话恢复警告?

3 windows google-chrome chromium node.js

如何通过 NodeJS 在 Windows 中重新启动 Chromium/Google Chrome(信息亭模式),以便它在重新启动时正常启动浏览器,就像普通人使用它一样?(当我每次重新启动 Chromium/Google chrome 时使用 NodeJS 时,都会在右上角显示丑陋/烦人/致命的弹出窗口)

NodeJS:告诉 chrome 关闭

在此输入图像描述

NodeJS:告诉 chrome 现在启动:每次启动时,它都会不断打开右上角那个丑陋的弹出窗口,并且在没有人为参与的情况下无法将其删除

在此输入图像描述

var wait_seconds = null;

function reboot_chrome() {
  // taskkill /f /im chrome.exe
  run_cmd( "taskkill", ["/f", "/im", "chrome.exe"], function(text) { 
    console.log (text);
  });

  //$ cat C:/Python27/run.bat:
  //@echo off
  //@start /b cmd /c "C:\Users\tpt\AppData\Local\Chromium\Application\chrome.exe" --kiosk

  wait_seconds = setTimeout(function() {
    run_cmd("C:\\Python27\\run.bat", [], function(text){
      console.log(text);
    });
  }, 20000);

}
Run Code Online (Sandbox Code Playgroud)

cvi*_*ejo 5

您可以使用--incognito--disable-session-crashed-bubble --disable-infobars开关,但浏览器的行为不会完全按预期进行。

最干净的方法是更改exit_type​​用户配置文件的首选项。这是一个小例子,就是这样做的:

var fs   = require("fs");
var path = require("path");
var exec = require("child_process").exec;

//----------------------------------------------------
function restartChrome(){
    stopChrome();
    setTimeout(startChrome, 20000);
}

//----------------------------------------------------
function startChrome(){
    // change this path to your application path
    exec('"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome" --kiosk')
}

//----------------------------------------------------
function stopChrome(){
    exec("taskkill /IM chrome.exe /f");
    setExitType();
}

//----------------------------------------------------
function setExitType(callback){
    // change this path to your session preferences path
    var preferencesPath = path.join(process.env["USERPROFILE"], "AppData/Local/Google/Chrome/User Data/Default/Preferences");

    fs.readFile(preferencesPath, "utf8", function(err, data){
        if (err) { return callback && callback(err); }

        var txt = data.replace(/exit_type":"Crashed/g,   'exit_type":"None')
                      .replace(/exited_cleanly":false/g, 'exited_cleanly":true');
        fs.writeFile(preferencesPath, txt, "utf8", callback);
    });
}

restartChrome();
Run Code Online (Sandbox Code Playgroud)

请记住调整注释中标记的应用程序和首选项文件的路径。