Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support reuse tab with same host #13

Merged
merged 2 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ const opn = require('better-opn');
opn('http://localhost:3000');
```

### Reuse tab by match host

In case your app can navigate to another pathnames and still want to reuse opened tab, set environment variable `OPEN_MATCH_HOST_ONLY=true` can tell this program to find reusable tab by only match the host part of your URL.

```js
process.env.OPEN_MATCH_HOST_ONLY = 'true';

opn('http://localhost:3000/foo/bar'); // This will reuse any tab with URL starting with http://localhost:3000/
```

## Author

- [Michael Lin](https://michaellin.me)
2 changes: 1 addition & 1 deletion openChrome.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ on lookupTabWithUrl(lookupUrl)
end tell
end using terms from
return found
end lookupTabWithUrl
end lookupTabWithUrl
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ const getBrowserEnv = () => {
return {action, value, args};
};

const normalizeURLToMatch = target => {
// We may encounter URL parse error but want to fallback to default behavior
try {
// Url module is deprecated on newer version of NodeJS, only use it when URL class is not supported (like Node 8)
const URL =
// eslint-disable-next-line node/prefer-global/url
typeof global.URL === 'undefined' ? require('url').URL : global.URL;
const url = new URL(target);
return url.origin;
} catch {
return target;
}
};

// Copy from
// https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/openBrowser.js#L64
// eslint-disable-next-line unicorn/prevent-abbreviations
Expand Down Expand Up @@ -57,7 +71,9 @@ const startBrowserProcess = (browser, url, opts = {}, args = []) => {
execSync('ps cax | grep "' + chromiumBrowser + '"');
execSync(
`osascript ../openChrome.applescript "${encodeURI(
url
process.env.OPEN_MATCH_HOST_ONLY === 'true'
? normalizeURLToMatch(url)
: url
)}" "${chromiumBrowser}"`,
{
cwd: __dirname,
Expand Down
5 changes: 5 additions & 0 deletions test/host-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const opn = require('../dist/index');

// Run test/open.js first, then run this to ensure tab is reused
process.env.OPEN_MATCH_HOST_ONLY = 'true';
opn('http://localhost:8000/foo/bar');