Skip to content
This repository was archived by the owner on Apr 8, 2019. It is now read-only.

Commit a80c98c

Browse files
elrumordelaluzshellscape
authored andcommitted
docs: add example to reuse open tab in Chrome (#120)
Adding the `.applescript` file on the _User_ project and calling it using `listening` event as `reuse-chrome-tab.config.js` example show. closes #117
1 parent c2d7ee5 commit a80c98c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

docs/addons/openChrome.applescript

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
(*
2+
Copyright (c) 2015-present, Facebook, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file in the root directory of this source tree.
6+
*)
7+
8+
property targetTab: null
9+
property targetTabIndex: -1
10+
property targetWindow: null
11+
12+
on run argv
13+
set theURL to item 1 of argv
14+
15+
tell application "Chrome"
16+
17+
if (count every window) = 0 then
18+
make new window
19+
end if
20+
21+
-- 1: Looking for tab running debugger
22+
-- then, Reload debugging tab if found
23+
-- then return
24+
set found to my lookupTabWithUrl(theURL)
25+
if found then
26+
set targetWindow's active tab index to targetTabIndex
27+
tell targetTab to reload
28+
tell targetWindow to activate
29+
set index of targetWindow to 1
30+
return
31+
end if
32+
33+
-- 2: Looking for Empty tab
34+
-- In case debugging tab was not found
35+
-- We try to find an empty tab instead
36+
set found to my lookupTabWithUrl("chrome://newtab/")
37+
if found then
38+
set targetWindow's active tab index to targetTabIndex
39+
set URL of targetTab to theURL
40+
tell targetWindow to activate
41+
return
42+
end if
43+
44+
-- 3: Create new tab
45+
-- both debugging and empty tab were not found
46+
-- make a new tab with url
47+
tell window 1
48+
activate
49+
make new tab with properties {URL:theURL}
50+
end tell
51+
end tell
52+
end run
53+
54+
-- Function:
55+
-- Lookup tab with given url
56+
-- if found, store tab, index, and window in properties
57+
-- (properties were declared on top of file)
58+
on lookupTabWithUrl(lookupUrl)
59+
tell application "Chrome"
60+
-- Find a tab with the given url
61+
set found to false
62+
set theTabIndex to -1
63+
repeat with theWindow in every window
64+
set theTabIndex to 0
65+
repeat with theTab in every tab of theWindow
66+
set theTabIndex to theTabIndex + 1
67+
if (theTab's URL as string) contains lookupUrl then
68+
-- assign tab, tab index, and window to properties
69+
set targetTab to theTab
70+
set targetTabIndex to theTabIndex
71+
set targetWindow to theWindow
72+
set found to true
73+
exit repeat
74+
end if
75+
end repeat
76+
77+
if found then
78+
exit repeat
79+
end if
80+
end repeat
81+
end tell
82+
return found
83+
end lookupTabWithUrl
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const execSync = require('child_process').execSync;
4+
const port = 8080;
5+
6+
module.exports = {
7+
entry: {
8+
index: [path.resolve(__dirname, 'app.js')],
9+
},
10+
mode: 'development',
11+
output: {
12+
filename: 'output.js',
13+
},
14+
};
15+
16+
module.exports.serve = {
17+
port,
18+
on: {
19+
listening: () => {
20+
execSync('ps cax | grep "Google Chrome"');
21+
execSync(
22+
`osascript openChrome.applescript "${encodeURI(
23+
`http://localhost:${port}`
24+
)}"`,
25+
{
26+
cwd: __dirname,
27+
stdio: 'ignore',
28+
}
29+
);
30+
},
31+
},
32+
};

0 commit comments

Comments
 (0)