Search code examples
javascriptphphtmlgoogle-chrome-extensiongoogle-search

How to save text from google search page?


I want to build a chrome extension which can save the google search text (single English word) with its meaning. E.g. Suppose I have searched for the word "amount" i get the meaning.

"- a quantity of something, especially the total of a thing or things in number, size, value, or extent."

Now i want these 2 to store as key value pair in my chrome's local storage. Hope I m cleared.

For this I m using curl and hosted my php page to a server.

$curl = curl_init('http://testing-ground.scraping.pro/textlist');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$page = curl_exec($curl);

if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}

curl_close($curl);

$regex = '/<div id="case_textlist">(.*?)<\/div>/s';
if ( preg_match($regex, $page, $list) )
  echo $list[0];
else 
  print "Not found";

Code is working fine for static sites or 'dynamic sites with static div', until i got stuck using google search url. The question is how do go to that url for saving the meaning of particular word. Also sometimes I m not able to get the "div" using inspect, in which the actual meaning is written. If I have to go to that "div", then it is must to reach to the url first. Is there any possibility for achieving this. If not just help me out in saving the input value i type on search bar. Thanks.


Solution

  • The Google Web Search API is offcially deprecated, Google excourage developer to use the Custom Search API which may provide solution.

    Custom Search Element is a control that provides the user interface for a Google Custom Search Engine. Using the Custom Search Element JavaScript API, you can configure the Google Custom Search UI, search parameters, and result rendering.

    It implements the Custom Search Element, which comprises the Custom Search engine and UI. Calling the constructor initializes the Custom Search service and UI; other methods allow you to control search queries, results, and UI.

    Syntax:

    google.search.CustomSearchControl.CustomSearch
    Control(cseId, opt_options);
    

    You can use setOnKeepCallback method. Specifies the optional text label displayed under each search result. Clicking this label triggers a callback into the specified object/method.

    google.search.SearchControl.KEEP_LABEL_SAVE sets a label value of save.