Skip to content

Instantly share code, notes, and snippets.

@sdondley
Created August 23, 2021 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdondley/032fea49818dc50d65f1c36f92b48f5d to your computer and use it in GitHub Desktop.
Save sdondley/032fea49818dc50d65f1c36f92b48f5d to your computer and use it in GitHub Desktop.
Tabbing through google search results in Safari
For whatever very bad reason (likely related squeezing to every drop of ad revenue from their product), Google does not allow you to easily tab throug search results. When logged in to Google, you have to hit tab 17 times just to get to the first result, which is ridiculous. It can take anywhere from a few to dozens of more taps to get to the next result. This is painful.
I took a quick look for an extension on Safari but came up empty. So here is a quick AppleScript I quickly knocked out (well, OK, it took several hours because I'm not proficient with AppleScript and it's agonizing to debug) to make tabbing a lot more useful on Google:
#!/usr/bin/osascript
on run argv
set op to argv
global isTag
tell application "Safari" to activate
tell application "System Events"
# detect if page is newly opened, tab once if so
tell application "Safari"
set curEl to (do JavaScript "el=document.activeElement;tag=el.tagName;" in current tab of first window)
end tell
if ((curEl as string) is equal to "BODY") then keystroke tab
repeat 50 times # set a limit to prevent runaway
# do the up/down tab
if ((op as string) is equal to "up") then
keystroke tab using shift down
else
keystroke tab
end if
delay .003 # wait for tab to happen
# check the innerHTML to see if we are a main result link
tell application "Safari"
set isTag to (do JavaScript "el=document.activeElement;tag=el.innerHTML;" in current tab of first window)
# uncomment next line to debug
#display dialog isTag
end tell
# Found link. Stop.
if isTag begins with "<br><h3 class" then exit repeat
end repeat
# scroll the screen to the element
tell application "Safari"
do JavaScript "el=document.activeElement;el.scrollIntoView({block: 'center'});" in current tab of first window
end tell
end tell
end run
If you create this as a service in Automator, you can bind it to the keys of your choosing in System Preferences. Note that you will need to supply and input argument to the script, the string "up", to make the script jump to the previous link.
Automator is a little slower to respond, so I use Karabiner Elements which makes tabbing super quick. Here's the json for that which binds the next/prev operations to CTRL-J and CTRL-K:
{
"rules" : [
{
"description" : "ctrl-j to tab to next google result",
"manipulators" : [
{
"type" : "basic",
"from" : {
"key_code" : "j",
"modifiers" : {
"mandatory" : [
"control"
]
}
},
"to" : [
{
"shell_command" : "zsh -c '~/bin/tab_to_result.osa down'"
}
],
"conditions": [
{
"type" : "frontmost_application_if",
"bundle_identifiers" : [
"^com\\.apple\\.Safari"
]
}
]
}
]
},
{
"description" : "ctrl-k to tab to previous google result",
"manipulators" : [
{
"type" : "basic",
"from" : {
"key_code" : "k",
"modifiers" : {
"mandatory" : [
"control"
]
}
},
"to" : [
{
"shell_command" : "zsh -c '~/bin/tab_to_result.osa up'"
}
],
"conditions": [
{
"type" : "frontmost_application_if",
"bundle_identifiers" : [
"^com\\.apple\\.Safari"
]
}
]
}
]
}
],
"title" : "Tab to next Google result"
}
Gitrepo coming soon.
@parhuzamos
Copy link

Take a look at https://chrome.google.com/webstore/detail/vimium/dbepggeogbaibhgnhhndojpepiihcmeb?hl=en
You won't use the mouse ever again (almost) in your browser. IMHO it's better than using the tab key.

@sdondley
Copy link
Author

sdondley commented Sep 7, 2021

Thanks. But I don't use Chrome. I've turned this gist into a project, btw: https://github.com/sdondley/Tab-Through-Google-Results-with-Safari

I get around pretty well without the mouse. It's a very hard habit to break, though, when you have relied on it for so many years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment