admin管理员组

文章数量:1417434

Running open http://localhost:8080 in Terminal will open a new tab each time.

How does create-react-app re-use an existing Browser tab, when available?

Update

It looks like here is the place where the magic happens:

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):
  // 
  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;
  }
}

However, I still don't know how this is working. I am on OS X, and I do have the osascript binary, surprisingly. But I'm not sure how to use it within Terminal.

I've tried osascript openChrome.applescript "localhost:8080", but I'm getting the following error:

osascript: openChrome.applescript: No such file or directory

What is the proper use of the osascript mand to open http://localhost:8080 in the current tab, if it exists?

Update

It looks like the openChrome.applescript file is somewhere included within create-react-app, but I'm not sure where.

Running open http://localhost:8080 in Terminal will open a new tab each time.

How does create-react-app re-use an existing Browser tab, when available?

Update

It looks like here is the place where the magic happens:

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./facebook/create-react-app/pull/1690#issuement-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;
  }
}

However, I still don't know how this is working. I am on OS X, and I do have the osascript binary, surprisingly. But I'm not sure how to use it within Terminal.

I've tried osascript openChrome.applescript "localhost:8080", but I'm getting the following error:

osascript: openChrome.applescript: No such file or directory

What is the proper use of the osascript mand to open http://localhost:8080 in the current tab, if it exists?

Update

It looks like the openChrome.applescript file is somewhere included within create-react-app, but I'm not sure where.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 21, 2018 at 19:04 Raphael RafatpanahRaphael Rafatpanah 20.1k28 gold badges105 silver badges174 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

I just tested osascript /path/to/openChrome.applescript "http://localhost:8080" and it works as expected.

If you don't have the openChrome.applescript script, then here its source code URL: openChrome.applescript

本文标签: javascriptHow does createreactapp reuse an existing Browser tab after running npm run startStack Overflow