Jump to content

Recommended Posts

Posted (edited)

I'm starting out trying to make workflows and have to this point just managed to do web searches on a specific website by using Alfred's buildt inn function with the {query} option. This has worked out perfectly with sites that displays a HTTP address for a search, but how can i tackle pages that does not do this?
ATM i'm trying to make a search for this page http://www.magnusli.no/ifirooms/. I tried to inspect the getRoom() function and stole a link from there 

include/get_room.php?id=

and tried to use the {query} on that (with eg. 'Caml' as a search) and that worked to a point where the function returned the data for that search, but that was not what i intended to do. The request returned this:
1519201981_Skjermbilde2018-07-21kl_14_31_33.thumb.png.eeb93f463056570b7a291b215dbd6790.png
Wanted outcome:
91611419_Skjermbilde2018-07-21kl_14_31_10.thumb.png.cd37cf991ac9d707faa1e21819193ab4.png

Any tips or input as to where to start or how to tackle an issue like this would be much appreciated. 
Thank you!

Edited by FreeeG
Formating

Share this post


Link to post

You can't use a regular Alfred web search for this because the page is a JavaScript "app". The URL you've found is called via JavaScript and injects the result into the current page.

 

Consequently, to achieve the result you want, you need to inject JavaScript into the page and execute it.

 

Here's a demo workflow.

 

It's smart enough to inject the JS into the correct tab, but it's hard-coded to Safari because I know how to do all this in Safari.

 

You appear to be using some kind of Chrome browser, so I'll leave adapting it to Chrome as an exercise for the reader :) 

 

Mostly, you'll just need to change the code than injects/executes the JavaScript and point the workflow at the correct app.

Share this post


Link to post
Posted (edited)

Welcome @FreeeG,

 

2 hours ago, FreeeG said:

This has worked out perfectly with sites that displays a HTTP address for a search, but how can i tackle pages that does not do this?

 

The answer is “it depends on the website”. In this case what you type in the box is being sent to a PHP script that parses your request and sends the information back, at which point the webpage fits it in. When you’re sending your request directly to the PHP script, as in your example, you’re getting the raw information. There seems to be no simple way to do this by giving the browser a URL.


A way to go about it is to tell the browser to open the page and then execute what needs to be done. Using JavaScript to write what you need on the search box isn’t enough because the page takes a while to detect it. What we can do is ignore the site’s search and do it ourselves, essentially loading the page, figuring out every element that doesn’t match what we want and hiding it.


Like so (edited). I’ve made it work just in Chrome, as it seems to be the browser in your screenshots.

Edited by vitor

Share this post


Link to post

Well, there's the code to run JS in Chrome. I'd use that to alter the workflow I posted, as it makes sure the page is open in the browser before trying to run JS in it.

 

11 minutes ago, vitor said:

Using JavaScript to write what you need on the search box isn’t enough because the page takes a while to detect it

 

var js = 'document.getElementById("search_input").value="' + query + '";getRooms();'

:) 

Share this post


Link to post
2 minutes ago, deanishe said:

as it makes sure the page is open in the browser before trying to run JS in it.

 

My version opens the page and then waits for it to finish loading before running the JS. I could have an extra check in place to guarantee the active tab is really that one, but in practice I doubt it’ll make a difference.

 

5 minutes ago, deanishe said:

var js = 'document.getElementById("search_input").value="' + query + '";getRooms();'

 

 

I looked at the script tag and thought “nah, I don’t want to wade through javascript files to get the exact function”. It’s literally the first line! I’ve updated the previous link to use that solution. Thank you.


Small technical note you may be interested in. To use that solution we have to change the way the JS is sent to Chrome. If we use execute({javascript: js_code}) it will execute our code but ignore the page’s, meaning it will fail to run getRooms(). The workaround is to instead url = 'javascript:' + js_code.

Share this post


Link to post
Posted (edited)
23 minutes ago, vitor said:

My version opens the page and then waits for it to finish loading before running the JS

 

Right. See what you mean. Does Chrome simply reload an existing tab if the URL is already open? (That's what Safari does.)

 

23 minutes ago, vitor said:

it will execute our code but ignore the page’s

 

That sucks.

 

23 minutes ago, vitor said:

The workaround is to instead url = 'javascript:' + js_code.

 

Presumably, you also have to URL-encode the JS in that case, too?

 

Edited by deanishe

Share this post


Link to post
Posted (edited)
52 minutes ago, deanishe said:

Does Chrome simply reload an existing tab if the URL is already open?

 

Not with the openLocation method I’m using (which is enabled by includeStandardAdditions, not a Chrome feature). Unsure if it does with other methods (I typically want a new tab). Would have to try it out.

 

52 minutes ago, deanishe said:

Presumably, you also have to URL-encode the JS in that case, too?

 

Nope. Just checked and AlfredBookmarklet’s sanitising feature even reverses the encoding it there’s any (so the same code works with both Chrome and Safari). It also seems to work when encoded, so it appears Chrome is smart enough to figure it out when it sees javascript: at the start.

Edited by vitor

Share this post


Link to post
Posted (edited)

@vitor and @deanishe, thank you guys so much for the helping hand! Appreciated! Now I just have to hope and see if i cant retrace your steps and learn from your solutions 🧐Been a normal Alfred user for quite some time, but now i'm looking forward to get better at thinkering with custom solutions! 🙏

Edited by FreeeG

Share this post


Link to post
Posted (edited)

I have a new issue i was wondering if any of you more experienced than me could answer. The workflows @vitor and @deanishe worked out great on my mid 2015 macbook pro - but i just got the newest 15" macbook pro on the door today and the workflow does not seem to want to function any longer. Ideas? When i use e.g. @vitor's workflow it opens the page - but does not search it. Still using chrome.


EDIT: Found out that Chrome was just updated. Belive that to be the reason behind it, but dont know why.

Edited by FreeeG

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×