-1

I want to make a javascript that being put into any browser's url (assuming google.com is opened already) automatically puts the search query into google search field and clicks on search button.

clicking on search button can be through onclick event of javascript but i want the full process through javascript. As in an automated search for google without typing. Any help would definitely be appreciated.

Sanchit Gupta
  • 77
  • 1
  • 3
  • 10

3 Answers3

2

I don't think what you're trying to accomplish is a good design. What you might want is instead learning the url pattern for a search website. For example you can search with google by navigating to the url with the query inside of it. Here's one example https://www.google.com/#q=search+query, so you can create your javascript to navigate your page to the url that you can modify using javascript.

Edit Based on what I've understood from comments, this might be usefull. javascript:(function() {var value = prompt("Name?"), value2 = prompt("Desciption"); window.location.href = "http://google.com/#q=" + value + "-" + value2;})();

Armin
  • 220
  • 2
  • 10
  • No i want that user search keyword should come to search input field on google through javascript and not by just manually typing it. – Sanchit Gupta Mar 05 '17 at 08:21
  • You don't have to manually type it, change `search+query` (+ is used instead of space) in `https://www.google.com/#q=search+query` using javascript and navigate to the page. For example you can try opening this link https://www.google.com/#q=sanchit+g – Armin Mar 05 '17 at 08:24
  • No @armin not exactly this. This is just a normal url in which you are manipulating the query. Let me explain it to you suppose you have two input type say name and description and what i want is that through javascript (which i will be entering in browser url bar) i can enter my desired value into both input types. for eg: in this way right after javascrpit and colon javascript: – Sanchit Gupta Mar 05 '17 at 08:35
  • Sorry I can't make sense of your comment. Is this what you're looking for? https://jsfiddle.net/mLh3xjkp/ – Armin Mar 05 '17 at 08:40
  • @Armin http://stackoverflow.com/questions/4163879/call-javascript-function-from-url-address-bar – Jonathan Portorreal Mar 05 '17 at 08:46
  • No @Armin Have you heard about sending Facebook page invites to all friends in one go through javascript. I want to enter values in fields through java script. You can visit this link once https://safetricks.org/invite-all-friends-to-like-facebook-page/ – Sanchit Gupta Mar 05 '17 at 08:48
  • @sanchitg sorry, I have not. I've updated my answer see if that helps, otherwise. I don't know how to help you, sorry. – Armin Mar 05 '17 at 08:56
  • Somewhat similar @Armin Thanks. Just wanted to know if there can be a generalized script for the same? – Sanchit Gupta Mar 05 '17 at 09:04
0

It would be much better to manipulate the search url instead of automating clicks. You could do that with the endpoint #q= + "your_search_term". Here's an example that will search for fluffy pillows:

function searchGoogle(query) {
  let base = "https://google.com/#q=";
  // change current web page to search query
  document.location.href = base + query; 
}
searchGoogle("fluffy pillows");

or you could use google's api to get search data

$.ajax({
  url: 'https://www.googleapis.com/customsearch/v1',
  type: 'GET',
  data: {
    key: 'YOUR_API_KEY',
    alt: 'json',
    prettyPrint: 'true',
    q: 'SEARCH_FIELD'
  },
  success: data => console.log(data),
  error: err => console.log(err)
});
Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38
0

Searched and found a way with just HTML.

  • copied from another answer in my search for this from Shyam * This snippet creates a simple google search box

This snippet creates a simple google search box:

<form action="http://www.google.com/search" method="get">
    <input type="text" name="q"/>
    <input type="submit" value="search" />
</form>

Works. The input word is pasted into google search bar.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 16 '23 at 22:27