在运行``npm运行start''之后如何重复使用现有的浏览器选项卡



在终端中运行open http://localhost:8080每次都会打开一个新选项卡。

创建反应式应用如何在可用时重新使用现有的浏览器选项卡?

更新

看起来这是魔术发生的地方:

function startBrowserProcess(browser, url) {
  // If we're on OS X, the user hasn't specifically
  // requested a different browser, we can try opening
  // Chrome with AppleScript. This lets us reuse an
  // existing tab when possible instead of creating a new one.
  const shouldTryOpenChromeWithAppleScript =
    process.platform === 'darwin' &&
    (typeof browser !== 'string' || browser === OSX_CHROME);
  if (shouldTryOpenChromeWithAppleScript) {
    try {
      // Try our best to reuse existing tab
      // on OS X Google Chrome with AppleScript
      execSync('ps cax | grep "Google Chrome"');
      execSync('osascript openChrome.applescript "' + encodeURI(url) + '"', {
        cwd: __dirname,
        stdio: 'ignore',
      });
      return true;
    } catch (err) {
      // Ignore errors.
    }
  }
  // Another special case: on OS X, check if BROWSER has been set to "open".
  // In this case, instead of passing `open` to `opn` (which won't work),
  // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
  // https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
  if (process.platform === 'darwin' && browser === 'open') {
    browser = undefined;
  }
  // Fallback to opn
  // (It will always open new tab)
  try {
    var options = { app: browser };
    opn(url, options).catch(() => {}); // Prevent `unhandledRejection` error.
    return true;
  } catch (err) {
    return false;
  }
}

但是,我仍然不知道这是如何工作的。我在OS X上,令人惊讶的是,我确实有osascript二进制。但是我不确定如何在终端中使用它。

我已经尝试了osascript openChrome.applescript "localhost:8080",但是我会收到以下错误:

osascript: openChrome.applescript: No such file or directory

如果存在osascript命令在当前选项卡中打开http://localhost:8080的正确使用是什么?

更新

看起来像OpenChrome.AppleScript文件在Create-React-App中包含在某个地方,但我不确定在哪里。

i刚刚测试了osascript /path/to/openChrome.applescript "http://localhost:8080",并且可以按预期工作。

如果您没有 openthrome.applescript 脚本,则此处是源代码url :openthrome.applescript

最新更新