Rap*_*nah 4 javascript applescript
open http://localhost:8080
每次在终端中运行都会打开一个新选项卡。
create-react-app 如何重用现有的浏览器选项卡(如果可用)?
看起来这里是魔法发生的地方:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然不知道这是如何工作的。我在 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 中的某个地方,但我不确定在哪里。
我刚刚测试过osascript /path/to/openChrome.applescript "http://localhost:8080"
,它按预期工作。
如果您没有openChrome.applescript 脚本,那么这里是它的源代码 URL:openChrome.applescript