Skip to content

Instantly share code, notes, and snippets.

@naokton
Created December 21, 2020 02:09
Show Gist options
  • Save naokton/6a944761b3bc9abe3e53915f9aacaf80 to your computer and use it in GitHub Desktop.
Save naokton/6a944761b3bc9abe3e53915f9aacaf80 to your computer and use it in GitHub Desktop.
Alfred Workflow Sample - Search Wikipedia by DOM Operation
function run(){
let url = 'https://en.wikipedia.org/wiki/Main_Page'
let word = "{query}"
let app = Application("Safari")
app.includeStandardAdditions = true
app.activate()
let tab = loadUrlNewTab(app, url)
waitPageLoad(app, tab)
searchWord(app, tab, word)
}
function loadUrlNewTab(app, url){
let curWin = app.windows[0]
curWin.tabs.push(app.Tab())
curWin.currentTab = curWin.tabs[-1]
curWin.currentTab.url = url
return curWin.currentTab
}
function waitPageLoad(app, tab){
ret = false
do {
ret = app.doJavaScript(
"(function(){return document.readyState == 'complete'})()",
{ in: tab }
)
delay(0.2)
}while(ret==false)
}
function searchWord(app, tab, word){
app.doJavaScript(
"(function(){\
let form = document.getElementById('searchform');\
let input = document.getElementById('searchInput');\
input.value = '" + word + "';\
form.submit();\
})()",
{in: tab}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment