【问题标题】:How to disable Chrome's session restore warning using nodejs?如何使用 nodejs 禁用 Chrome 的会话恢复警告?
【发布时间】:2016-08-10 10:06:36
【问题描述】:

如何通过 NodeJS 在 Windows 中重新启动 Chromium/Google Chrome(kiosk 模式),以便在重新启动时正常启动浏览器,就像普通人使用它一样? (当我每次重新启动 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);

}

【问题讨论】:

  • 您也可以在通过 cli 启动 chrome 时使用 --app-auto-launched 选项

标签: node.js windows google-chrome chromium


【解决方案1】:

您可以使用--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();

记得根据 cmets 中的标记调整应用程序和首选项文件的路径。

【讨论】:

    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    相关资源
    最近更新 更多