'Fill the search field of google and search it

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.



Solution 1:[1]

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;})();

Solution 2:[2]

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)
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jonathan Portorreal