UNPKG

2.66 kBPlain TextView Raw
1(*
2Copyright (c) 2015-present, Facebook, Inc.
3
4This source code is licensed under the MIT license found in the
5LICENSE file in the root directory of this source tree.
6*)
7
8property targetTab: null
9property targetTabIndex: -1
10property targetWindow: null
11property theProgram: "Google Chrome"
12
13on run argv
14 set theURL to item 1 of argv
15
16 -- Allow requested program to be optional,
17 -- default to Google Chrome
18 if (count of argv) > 1 then
19 set theProgram to item 2 of argv
20 end if
21
22 using terms from application "Google Chrome"
23 tell application theProgram
24
25 if (count every window) = 0 then
26 make new window
27 end if
28
29 -- 1: Looking for tab running debugger
30 -- then, Reload debugging tab if found
31 -- then return
32 set found to my lookupTabWithUrl(theURL)
33 if found then
34 set targetWindow's active tab index to targetTabIndex
35 tell targetTab to reload
36 tell targetWindow to activate
37 set index of targetWindow to 1
38 return
39 end if
40
41 -- 2: Looking for Empty tab
42 -- In case debugging tab was not found
43 -- We try to find an empty tab instead
44 set found to my lookupTabWithUrl("chrome://newtab/")
45 if found then
46 set targetWindow's active tab index to targetTabIndex
47 set URL of targetTab to theURL
48 tell targetWindow to activate
49 return
50 end if
51
52 -- 3: Create new tab
53 -- both debugging and empty tab were not found
54 -- make a new tab with url
55 tell window 1
56 activate
57 make new tab with properties {URL:theURL}
58 end tell
59 end tell
60 end using terms from
61end run
62
63-- Function:
64-- Lookup tab with given url
65-- if found, store tab, index, and window in properties
66-- (properties were declared on top of file)
67on lookupTabWithUrl(lookupUrl)
68 using terms from application "Google Chrome"
69 tell application theProgram
70 -- Find a tab with the given url
71 set found to false
72 set theTabIndex to -1
73 repeat with theWindow in every window
74 set theTabIndex to 0
75 repeat with theTab in every tab of theWindow
76 set theTabIndex to theTabIndex + 1
77 if (theTab's URL as string) contains lookupUrl then
78 -- assign tab, tab index, and window to properties
79 set targetTab to theTab
80 set targetTabIndex to theTabIndex
81 set targetWindow to theWindow
82 set found to true
83 exit repeat
84 end if
85 end repeat
86
87 if found then
88 exit repeat
89 end if
90 end repeat
91 end tell
92 end using terms from
93 return found
94end lookupTabWithUrl