How do I get Google search query string inside my content script? - javascript

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:
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"
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?

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

Related

How to get Previous URL query String parameters in Javascript

I want to get the previous page URL query string parameters
document.referrer
gives the previous URL but not query string parameters. Is there any way to get that in javascript?
the purpose of reading that is I don't have any control on the previous page and I wanted those parameters in the next call.
I am doing an integration where the login page pop-ups of some third party were based on the client id it asks for the permissions to Allow. once user allows that it redirect to the "redirect URL" passed in the parameter with 2 parameters in query string.
Now I don't have any other way to find out which user has given the access. If I can get the client id on my current page I can get to know that this user has granted the login.
If is there any other way to do it please help me. Or let me know if anything else is also required

How to find path for on submit text box

I'm trying to reference a URL in a local application and am having difficultly finding the correct path to reference.
It uses a value that the website transforms into another value for the page requested.
The main URL is http://exac.broadinstitute.org/
My inputs contain variations of rs113488022 , which the website will turn to a variant
http://exac.broadinstitute.org/variant/7-140453136-A-T
Unfortunately I do not have this variant value to pass directly,and am looking for the path of the on submit action from the front page.
Yikes, that is some horrible HTML code.
But if you inspect the input element you find that the form has a simple action /awesome, and the input’s name is query. As a consequence, you can simply perform a GET on the following URI:
http://exac.broadinstitute.org/awesome?query=rs113488022
Be prepared to process redirects, because the above will give you an HTTP response of 302 FOUND with a redirect location (which is the URI you found).

Fill field in SSRS with javascript

I have an SSRS report with a function that detect url of report (the report shows tvo different value, dependending on url)
I use:
="javascript: var name=document.URL; var n=name.indexOf('hsbportalen.se'); if(n > 0) { void window.open('http://www.google.com',800,800,'_blank')} else { void window.open('http://www.msn.com',800,800,'_blank')}"
But what I want now, is how can I set a value in a textfield (Textbox42) with a JavaScript dependign on url.
What you'll find is that SSRS does not support JavaScript, period. The only functions that you can pass through SSRS are those that your web-browser can interpret outside of a script file. From what I've found, this is a very limited set of commands.
For instance, you can use Window.Open('') and void() and a few others. I've never been able to get an If statement and certainly not a parse() to work through SSRS.
I've had to do this a couple of times and, if you're in the same boat I was, you'll find the easiest way is to do one of the following:
Option 1: Going from one SSRS Report to Another, Pull data from URL
Go to the source of the current URL and reconstruct the URL you are receiving. For instance, you can use the "Go to URL" action to create a custom URL to a Report that will drop the breadcrumbs and use the Report Server rs & rc parameters. Basically, to make this work, whatever variable you want to parse out of the URL would first be created as a parameter of the appropriate type in your target report and then you would pass that value through the URL to that report.
Scenario 2: Coming from an external website, Pull data from URL
I've had to do this too, but it's a bit more involved. What you need is an intermediate webpage. One that Opens and Closes immediately, and all it basically does is redirect you to where you want the user to go. To accomplish this, follow these steps:
First, create a parameter in the report you want to read the URL. The parameter should be of the appropriate type to store the data to be read from the URL (i.e., URL_Data)
Second, create an intermediate webpage that will lie where the URL is pointing. You can construct the page to open with a message like "Loading..." or "Please wait..." (just in case the user sees it).
Third, in the script of the intermediate page, have it parse the data from the URL that you are trying to capture. Then, incorporate that data into a custom URL that points to the desired SSRS report and includes the URL data as the parameter.
See here and here for more information on how to generate custom URLs that can incorporate parameter values.
http://SSRS_Server/reportserver/Pages/ReportViewer.aspx?/directory/Sample_Report&URL_Data=URL_Snippet
Finally, finish off your JavaScript on the intermediate webpage to open the SSRS URL in a new window and close the current page.
Now, you can use the values of that parameter wherever you want.

Twitter-like tweet box?

so what I'm hoping to do is having a box that's similar to the tweet box. When the user enters '#' (I know, use keyup to detect when), and then he types a letter (i.e. 'J'), I want to get 4 of the people he's following and show it as a suggestion (i.e. James, Jay, Jack, etc...). The way I would approach this is make an AJAX call which will query the sql table to find all of the users whom the user follows and whose names begin with that substring, but what I've noticed that FB does with friends is that it loads all of the user's friends upon visit, meaning that if you were to type '#' then it would scour a client-side array rather than constantly query the database. Although I'm not so sure if that's better... Is it better? Also, when would I load all of the user's following users? As soon as he/she visits any page? And is it conventional?
tl;dr - Should users that a logged-in user be pre-loaded and stored as an array or constantly obtained by querying the database?
Thanks.
When an ajax call has been sent to server a thread is generated in Server's CPU. So if you have 10 user on line search for a friend list it will consume more CPU.
So better option is that fetch list in group. e.g. you want to search Arjun,
If you type A, it will fetch list of all friend with name A. so when you type R(second letter) it will not send ajax request again.

Shall I use javascript for page submission?

I am working on a big site, and in the site there is a search module. Searching is done by using a a lot of user submitted values, so in pagination I must pass all these data to the next page, appending the values to url make the url very big.
Sso how can I solve this issue? I am planning to use a javascript based page submission (POST) with all the values in hidden fields to the next page the read all the values from the next page.
Will it cause any problems? Or should I use database to keep the search criterias?
I would create a server side object, possibly with a database backend which is updated by the different pages.
It is at my opinion the most clear and easy solution. Giving parameters from page to page, either by post or javascript or cookie will work too but it's more of a quirk in my experience.
Also if a search query is so complex that it needs multiple pages to create it, it might be helpfull for the user to have all the data stored on the server so he can change it more easily by switching back and forth between the different pages.
I would store all the search criterias in some kind of session-store on the server when the initial search is being triggered.
For pagination I would retrieve the criterias from the session-store and then just show the appropriate results. Also I would append some kind of key to the pagination links (so this would be the only hidden post-field) under which the search criterieas can be found.
Even though the session is per user, you might have several search windows open within the same session, and you don't want to mess them up with the pagination.
In order to make a reliable search with pagination, we need to do a bit more than normal.
We need to handle the following cases.
Once search is done, user may choose to do browser back and forward. Here, if you are doing form submission on every page, it would be an overload. Also, if user presses browser refresh button, it will unnecessarily warn him that data is being submitted.
Searching on a large database with lots of criteria is costly. Hence, optimization is important.
So you should NOT do the following:
Submit data on every page change
Not store data in cookie. (This is not secure and not even reliable.)
For large database with complex query, cache the result in session.
In case, you need very up-to-date and real-time result, ignore point (3) and try doing partial search for every page.
Thus, for your case, you can do the following:
When user searches first time, make the form POST data to a search page.
This search page will store the search query in session and generate a unique id for it.
Now render the result page. The result page will be passed the search id (generated in point 2) and the page number. Example result.aspx?searchId=5372947645&page=2
The result page will puck up the query from session using the searchId and then provide result based on the page number sent.
Using hidden fields and POST method should be fine too unless you are able to get them on the next page right.
To supplement Sarfraz's answer...
It's not necessary to use Javascript to make a POST.
<form action="destination_url" method="POST">
...
</form>

Categories