Question

I'm trying to build a Chrome extension and clearly I'm a n00b.

I want to display some links on the right side of Google's result page, based on the query the user has searched.

But I'm just not able to get hold of the user query string!. I cannot depend on parsing URL since, the URL remains the same even though the user has made a second search. Let me clarify with a use case:

  1. User enters a search query "testing time" via omnibox and clicks on enter. URL has now become "https://www.google.co.in/search?q=testing+time"
  2. Now from within the results page, user changes the query to "testing again" and clicks on enter. The URL will remain what it was earlier, "https://www.google.co.in/search?q=testing+time".

How then shall I get hold of the query string?

Was it helpful?

Solution

For the case 2 mentioned by you, you should see #q=testing+again at the end of the URL. You can get it via

location.hash.split("=").pop(); //you might have to unescape it 

OR alternatively you can read the new query from the text box itself. (I would prefer this method)

document.getElementsByName("q")[0].value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top