Commit 6ebec23a authored by Jirat Ki's avatar Jirat Ki Committed by Dan Abramov
Browse files

Chrome 'open tab' reuse an empty tab when possible (#1165)

* Reuse empty tab on open chrome apple script

* Break find tab into function

* Use property to store found

* Fix minor issues that caused window to not get active
parent 15feb02e
Showing with 52 additions and 16 deletions
+52 -16
...@@ -7,23 +7,70 @@ This source code is licensed under the BSD-style license found in the ...@@ -7,23 +7,70 @@ This source code is licensed under the BSD-style license found in the
of patent rights can be found in the PATENTS file in the same directory. of patent rights can be found in the PATENTS file in the same directory.
*) *)
property targetTab: null
property targetTabIndex: -1
property targetWindow: null
on run argv on run argv
set theURL to item 1 of argv set theURL to item 1 of argv
tell application "Chrome" tell application "Google Chrome"
if (count every window) = 0 then if (count every window) = 0 then
make new window make new window
end if end if
-- Find a tab currently running the debugger -- 1: Looking for tab running debugger
-- then, Reload debugging tab if found
-- then return
set found to my lookupTabWithUrl(theURL)
if found then
set targetWindow's active tab index to targetTabIndex
tell targetTab to reload
tell targetWindow to activate
set index of targetWindow to 1
return
end if
-- 2: Looking for Empty tab
-- In case debugging tab was not found
-- We try to find an empty tab instead
set found to my lookupTabWithUrl("chrome://newtab/")
if found then
set targetWindow's active tab index to targetTabIndex
set URL of targetTab to theURL
tell targetWindow to activate
return
end if
-- 3: Create new tab
-- both debugging and empty tab were not found
-- make a new tab with url
tell window 1
activate
make new tab with properties {URL:theURL}
end tell
end tell
end run
-- Function:
-- Lookup tab with given url
-- if found, store tab, index, and window in properties
-- (properties were declared on top of file)
on lookupTabWithUrl(lookupUrl)
tell application "Google Chrome"
-- Find a tab with the given url
set found to false set found to false
set theTabIndex to -1 set theTabIndex to -1
repeat with theWindow in every window repeat with theWindow in every window
set theTabIndex to 0 set theTabIndex to 0
repeat with theTab in every tab of theWindow repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1 set theTabIndex to theTabIndex + 1
if theTab's URL as string contains theURL then if (theTab's URL as string) contains lookupUrl then
-- assign tab, tab index, and window to properties
set targetTab to theTab
set targetTabIndex to theTabIndex
set targetWindow to theWindow
set found to true set found to true
exit repeat exit repeat
end if end if
...@@ -33,17 +80,6 @@ on run argv ...@@ -33,17 +80,6 @@ on run argv
exit repeat exit repeat
end if end if
end repeat end repeat
if found then
tell theTab to reload
set index of theWindow to 1
set theWindow's active tab index to theTabIndex
tell theWindow to activate
else
tell window 1
activate
make new tab with properties {URL:theURL}
end tell
end if
end tell end tell
end run return found
end lookupTabWithUrl
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment