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 01 Answer
Reset to default 7I 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
版权声明:本文标题:javascript - How does create-react-app re-use an existing Browser tab after running `npm run start`? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745261056a2650359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论