2

I want to take the current page's URL (using page:env("caller")) and extract a section of it.

For instance, I want to take

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats

and assign

cats

to a variable.

How would I do this with KRL?

I have tried

url = page:env("caller");
query = url.replace("http://www\.google\.com/search\?sourceid=chrome&ie=UTF-8&q=", "");

but it simply assigns the entire page:env("caller") to the variable query (e.g. http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats).

Edit: a jQuery solution would most likely work, as well.

Edit2: @JAM --

The select statement you posted doesn't seem to work. I tested it on http://www.google.com/search?q=cats and it didn't fire. Not sure if the URL doesn't match pageview or what (it looks like it should match to me).

The app I put it in:

ruleset a835x36 {
  meta {
    name "regex testing2"
    description <<
 >>
author ""
logging on
}

rule get_query {
    select when pageview "http://www.google.com/search.*(?:&|?)q=(\w+)(?:&|$)"    setting(query) 
      notify("Query",query) with sticky = true;
   }
}

Also, I'm looking for a more robust way to get at the query, since Google has many ways to land on a search results page with URLs that won't look like http://www.google.com/search?q=cats. For example, going to google and searching for cats just gave http://www.google.com/webhp?hl=en#sclient=psy&hl=en&site=webhp&source=hp&q=cats&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=8ac6b4cea9b27ecb for the URL of the results. I guess I could parse anything with a regex, though...

user898763452
  • 169
  • 1
  • 13
  • Since you were asking about KRL we are assuming that you want to do the processing on the KNS server side. If you would like a jquery solution I would guess there is already an answer for that with a jquery tag. – Mike Grace Nov 30 '10 at 04:29

2 Answers2

5

2 Ways to accomplish what you want.

1) In the pre block

pre {
  queryInURL = page:url("query");
  q = queryInURL.replace(re/.*?q=(.*?)(?:$|&.*)/,"$1");
}
  • page:url("query") grabs the entire string of parameters in a url
  • do string replace to capture specific query parameter that you want

Full Example App Tested

Tested on url -> http://example.com/?q=cats&wow=cool

alt text

ruleset a60x439 {
  meta {
    name "url query test"
    description <<
      Getting the query from the current page URL
    >>
    author "Mike Grace"
    logging on
  }

  rule get_query {
    select when pageview ".*"
    pre {
      queryInURL = page:url("query");
      q = queryInURL.replace(re/.*?q=(.*?)(?:$|&.*)/,"$1");
    }
    {
      notify("Query",queryInURL) with sticky = true;
      notify("q",q) with sticky = true;
    }
  }

}

2) In the rules selection expression the way JAM has shown

Mike Grace
  • 16,636
  • 8
  • 59
  • 79
  • Where's the love? I just answer them how I would want it answered. ; ) – Mike Grace Nov 29 '10 at 21:44
  • 1
    Thanks, Mike! I wasn't aware of page:url("query"), that helps a lot. – user898763452 Dec 01 '10 at 15:15
  • Glad it helps! Best of luck to you! – Mike Grace Dec 01 '10 at 15:21
  • I tried to run the app on a google search URL, but it didn't work. The URL I tested it on is http://www.google.com/#sclient=psy&hl=en&q=testing&aq=f&aqi=g5&aql=&oq=&gs_rfai=&pbx=1&fp=8ac6b4cea9b27ecb . Not sure if the more complicated URL breaks it or what. I'll keep reading about regex's and see what I can figure out. – user898763452 Dec 01 '10 at 16:41
  • @autotrivis, I just tried it on the url you gave me and it is still working for me. Can you give me more details on what exactly is not working? Working screenshot => https://mikegrace.s3.amazonaws.com/forums/stack-overflow/url-query-params-working-on-google.png – Mike Grace Dec 01 '10 at 18:03
  • I can't find what I'm doing wrong... here's a screenshot of the app fired and the source I'm using in appbuilder: http://ompldr.org/vNmR2dw – user898763452 Dec 01 '10 at 18:19
  • I mistakenly ran it on a different URL, but I tried the one you used and I get the same result. – user898763452 Dec 01 '10 at 18:21
  • @autotravis, your replace regex doesn't match the one in my answer. – Mike Grace Dec 01 '10 at 21:13
  • 1
    @autotravis, in your defense, I had modified my regex to fix the issue you are seeing where the q param doesn't come first but forgot to change it also in the full app code example. It's changed now and you should be able to update your code and see it work. – Mike Grace Dec 01 '10 at 21:14
  • @Mike, I copied and pasted your code into my app and I'm still not getting anything, screenshot: http://ompldr.org/vNmV1aQ . You don't have to mess with this anymore, just an FYI for anyone coming along to this question in the future. I just need to study up on regular expressions so I can figure them out on my own :) – user898763452 Dec 03 '10 at 15:07
  • 1
    @Mike, cancel that last comment. I just created a new bookmarklet with your code and it *does* work. My mistake! Thanks for your help!! – user898763452 Dec 03 '10 at 17:09
4

This can be done in the select statement using a regular expression and a capturing group (()'s).

select when pageview "http://www.google.com/search.*(?:&|?)q=(\w+)(?:&|$)" setting(query)

Regular expressions make the select statement powerful. Be sure to learn them! Here is an excellent Regular Expression (or Regex) site.

Jessie A. Morris
  • 2,267
  • 21
  • 23