The google search API customized on a form.

Published on by Yousfi Benameur

 
The google search API announced as deprecated is still working nowadays with  javascript code.
see this link for web developpers:https://developers.google.com/web-search/docs/
its very usefull and cuts all adds,....this can be presented properly on a top level form as a web searcher( with the native vfp olecontrol browser).
textbox with some customized capabilities as:
1-local search (in center point choosed in code-here New York).
2-web
3-video
4-blogs
5-news
6-images
7-Books
-....
can expand or collapse each div on the page result found.
can make all these options or set only the wanted searcher (see the javascript line of code as:
"searchControl.addSearcher(new google.search.WebSearch());"
Any clicked link browses a new internet explorer window.
the skin can be modified in the style tag of the code(background color,font,..).

Of course the code is mixed with javascript (google API),because there is no automation for this purpose.

this code works as well  until google erases this API...
This API is officially deprecated as of November 1, 2010 but still works.


Click on code to select [then copy] -click outside to deselect

*1*
 _Screen.WindowState=1
Declare Integer AnimateWindow In WIN32API ;
    Integer HWnd, ;
	Integer dwTime, ;
	Integer dwFlags


Publi yform
yform=Newobject("ygsearcher")
yform.Show
Read Events
Return
*
Define Class ygsearcher As Form
	BorderStyle = 3
	Height = 572
	Width = 933
	ShowWindow = 2
	AutoCenter = .T.
	Caption = "yGoogle_search API"
	Name = "Form1"

	Add Object olecontrol1 As OleControl With ;
		oleclass="shell.explorer.2",;
		Top = 0, ;
		Left = 0, ;
		Height = 541, ;
		Width = 936, ;
		Name = "Olecontrol1"

	Procedure ybuild
	Local m.myvar
	TEXT to m.myvar textmerge noshow
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		  <head>
		    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
		    <title>Google  Web Search API </title>
		    <script src="https://www.google.com/jsapi"
		        type="text/javascript"></script>
		    <script language="Javascript" type="text/javascript">
		    //<!
		    google.load('search', '1');

		    function OnLoad() {
		      // Create a search control
		      var searchControl = new google.search.SearchControl();

		      // Add in a full set of searchers
		      var localSearch = new google.search.LocalSearch();
		      searchControl.addSearcher(localSearch);
		      searchControl.addSearcher(new google.search.WebSearch());
		      searchControl.addSearcher(new google.search.VideoSearch());
		      searchControl.addSearcher(new google.search.BlogSearch());
		      searchControl.addSearcher(new google.search.NewsSearch());
		      searchControl.addSearcher(new google.search.ImageSearch());
		      searchControl.addSearcher(new google.search.BookSearch());
		     // searchControl.addSearcher(new google.search.PatentSearch());

		      // Set the Local Search center point
		      localSearch.setCenterPoint("New York, NY");

		      // tell the searcher to draw itself and tell it where to attach
		      searchControl.draw(document.getElementById("searchcontrol"));

		      // *Set the Search Control to get the most number of results
		      searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);

		      // execute an inital search
		      searchControl.execute("VISUAL FOXPRO");
		    }
		    google.setOnLoadCallback(OnLoad);

		    //]]>
		    </script>
		    <style type="text/css">
			body
			{
				text-align: left;
				 font-family: "trebuchet ms", verdana, sans-serif;
		         font-size: 14px;
			     font-weight:bold;
			     style="background-color:rgb(255,251,168);"
			}

			#searchcontrol
			{
				width:100%;
				margin-left:auto;
				margin-right:auto;
				text-align:left;
				background-color:rgb(255,251,168);
			}
			.gsc-control
			{
				width: 100%;
				background-color:rgb(255,251,168);
			}
				</style>
		  </head>
		  <body>
		    <div id="searchcontrol"  >Loading</div>
		  </body>
		</html>
	ENDTEXT
	Local m.lcdest
	m.lcdest=Addbs(Sys(2023))+"ytest.html"
	Strtofile(m.myvar, m.lcdest)

	Thisform.olecontrol1.Navigate(m.lcdest)
	Endproc

	Procedure Resize
	Try
		Thisform.olecontrol1.ybuild()
	Catch
	Endtry
	Endproc


	Procedure Init
	*https://developers.google.com/web-search/docs/
	Set Safe Off
	#Define AW_BLEND        0x00080000
	AnimateWindow (Thisform.HWnd,5000, AW_BLEND)
	Thisform.ybuild()
	Endproc

	Procedure Destroy
	Clea Events
	Endproc

	Procedure olecontrol1.Init
	With This
		.silent=.T.
		.Anchor=15
		.Left=0
		.Top=0
		.Width=.Parent.Width
		.Height=.Parent.Height
	Endwith
	Endproc

Enddefine
*
*-- EndDefine: ygsearcher
***************************


 

The google search API customized on a form.
The google search API customized on a form.

Click on code to select [then copy] -click outside to deselect


*2* created on sunday 15 of october 2017
*this code uses the original google textbox search to fire suggestions and retrieves result to a vfp textbox class.
*i guess its an autocomplete textbox but with a large google autocomplete table to fill these suggestions.
*the google search  page is embed on a vfp olecontrol ("shell.explorer.2" as oleclass) in a container.
*the ingenious beforenavigate2 method of the olecontrol redirect the url as vfp wants even in the vfp namespace.
*this is an exercice how to intercept the google textbox value after choice in autocomplete.
*the code is to perfect yet...

Publi yform
yform=Newobject("ytxtSearch")
yform.Show
Read Events
*
Define Class ytxtSearch As Form
	BorderStyle = 0
	Top = 106
	Left = 336
	Height = 144
	Width = 455
	ShowWindow = 2
	Caption = "Google search adaptation"
	MaxButton = .F.
	backcolor=rgb(112,108,110)
	Name = "Form1"

	Add Object text1 As TextBox With ;
		FontBold = .T., ;
		FontSize = 12, ;
		Anchor = 0, ;
		Height = 41, ;
		Left = 23, ;
		Top = 99, ;
		Width = 337, ;
		Name = "Text1"

	Add Object timer1 As Timer With ;
		Top = 106, ;
		Left = 360, ;
		Height = 23, ;
		Width = 23, ;
		Interval = 2000, ;
		Name = "Timer1"

	Add Object container1 As ycnt With ;
		Top = 0, ;
		Left = 0, ;
		Width = 456, ;
		Height = 90, ;
		Name = "Container1"

	Procedure Destroy
		Clea Events
	Endproc

	Procedure timer1.Timer
		Try
			Thisform.ycnt.olecontrol1.Document.body.Style.overflowx="hidden"  &&x scrollbar hidden, y scrollbar shown (hidden by container but can scroll with mouse)
			*hidding google buttons is to work...
			*thisform.ycnt.olecontrol1.document.getElementById("_eEe").style.visibility="hidden"
			*thisform.ycnt.olecontrol1.document.getElementById("_fZl").style.visibility="hidden"
			This.Enabled=.F.
		Catch
		Endtry
	Endproc

Enddefine
*-- EndDefine: ytxtSearch
*************************
Define Class ycnt As Container
	Top = 0
	Left = 0
	Width = 456
	Height = 90
	borderwidth=0
	backstyle=0
	Name = "ycnt"

	Add Object olecontrol1 As OleControl With ;
		oleClass="shell.explorer.2", ;
		Top = -300, ;
		Left = 1, ;
		Height = 450, ;
		Width = 500, ;
		Name = "Olecontrol1"

	Procedure olecontrol1.BeforeNavigate2
		*** Événement de contrôle ActiveX  ***
		Lparameters pdisp, url, Flags, targetframename, postdata, headers, Cancel
		Local m.x
		m.x=Strextract(Allt(url),"&q=","&oq")
		m.x=Strtran(m.x,"+"," ")
		Thisform.text1.Value=m.x
		Cancel=.T.
		This.Document.parentWindow.scrollBy(0,-This.Document.parentWindow.innerHeight)  &&can see also: parentwindow.scrollTo(coord-x,coord-y)
		Return .F.
	Endproc

	Procedure olecontrol1.Init
		This.silent=.T.
		This.Navigate("https://www.google.dz/?gws_rd=cr&dcr=0&ei=_iTjWcnhJonlUZuFrLAH")   &&google search url
	Endproc
Enddefine
*-- EndDefine: ycnt
************************


 
*Important:*the code above is tested on visual foxpro 9 sp2-under windows 10 pro

To be informed of the latest articles, subscribe:
Comment on this post