Jump to content

Recommended Posts

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

Welcome @FreeeG,

 

On 7/21/2018 at 1:35 PM, 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
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
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

@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

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

@FreeeG I think I have your solution and hope it is useful to you or others:

 

Chrome > View > Developer > Allow Javascript from Apple Events

 

I am working on a similar problem with an internal site that just stopped using URL-based search strings after an upgrade, and this thread was really useful. I'm NOT a coder but was able to adapt the code to my site and get it working. Prior to making the above change in Chrome, I was getting the same issue as you where the page would open but not run the code. Pasting my code into the Console worked properly. After making the above change it's running properly and I can start building a more complex workflow around it.

Screen Shot 2019-04-04 at 6.01.04 PM.png

Share this post


Link to post
28 minutes ago, Matt LE said:

Chrome > View > Developer > Allow Javascript from Apple Events

 

FWIW, Safari requires the same option to be set under "Develop > Allow Javascript from Apple Events".

Share this post


Link to post

@deanishe Very good to know! I'm sure it's come up elsewhere in these forums, which I'll be digging through as I try to make more powerful workflows with this new site.

 

Any other discussions you'd recommend to followers of this thread? I'm very interested in retrieving/caching search results directly in Alfred, which may be a challenge since it's behind a login. I learn a lot from reading how others have met similar needs.

Share this post


Link to post

Note other Chromium browsers that support AppleScript (Vivaldi, Brave) also require the option to be set.

Share this post


Link to post
26 minutes ago, Matt LE said:

which may be a challenge since it's behind a login.

 

That's just a bit of web automation. Where it becomes tricky is if the site actually requires Javascript to be executed. If not, you can usually get what you need with an HTTP library that supports cookies and an HTML parsing library. If JS support is required, then you'll have to use a headless browser instead (i.e. run a full copy of Chrome/Firefox and automate that).

 

If you can get away without the full browser, JS apps are often easier to pull data out of because the data is typically in easily-parsed JSON, not HTML. Sometimes the data are actually stored in a nice, machine-readable form in data- attributes on HTML elements, too, which is also easy to extract.

 

Share this post


Link to post

@deanish I love your confidence in my skills! Thanks for the few keywords to help me get started on this journey. I really want to make sure whatever I do can be deployed to my team, and fortunately we've standardized on Macs. So we'll be purchasing more Alfred licenses once I make some progress.

 

Any recommended reading?

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...