Home Notices Documents Classes Download Others Rice
Documents  >  tutorial  >  Basic usage  >  Integration
Integration of scripts, and Load event

Purpose of this page

So far, displaying a page and calling JavaScript code from Rice have been implemented as a separate script. Here we group these separate scripts into one.

And, we will explain the Load event required to integrate the two scripts.

Asynchronous method

sample_005.cgx is shown below. This is a script that simply line the scripts that were implemented so far up.

1:

class main

2:

// getter

3:

closed getter directory _baseDir

4:

// Returns the directory where this script - sample_005.cgx - is located.

5:

rice riceEnv;

6:

file source = new file(riceEnv.SourceFileName);

7:

return source.Directory;

8:

endgetter

9:
10:

// method

11:

open method void start()

12:

// Navigation

13:

browser brw; // Declaration statement. Creates variable - brw - of browser class.

14:

brw.Navigate("https://www.google.com/"); // Calling the Navigate method.

15:
16:

// Loads JavaScript code from sample_001.js.

17:

reader red = new reader(this._baseDir.FullName + "\sample_001.js");

18:

string jsCode = red.ReadToEnd();

19:

red.Release();

20:
21:

// Executes the JavaScript code.

22:

jsexecutor jse; // Declaration statement. Creates variable - jse - of jsexeutor class.

23:

jse.Execute(jsCode); // Calling the Execute method.

24:

endmethod

25:
26:

open method void end()

27:

endmethod

28:

endclass

Navigation, loading, and execution. At first glance it looks like this script will work well, but it will not work as we want. The cause is the call of the Navigate() method on the fourteenth line.


The Navigate() method does not wait for navigation to complete. In the example above, it won't wait to finish displaying the "https://www.google.com/" page. it exits immediately after making a navigation request. The browser is working to display the requested page, but the script will proceed to the next.

Methods that do such behavior are called asynchronous methods.

It usually takes time to load the page, but fourteenth line ends immediately after making a request for navigation. Therefore, the following script is executed before the Google search page is displayed. The sample_001.js executed by the following script need a newly loaded Google search page, but there is no new Google search page yet. Therefore, the script stops because JavaScript execution is not successful.

Load event

In order to put together a script, we have to wait JavaScript execution until to finish displaying Google search page. And CookerGX has a good solution to this problem.

It is a Load event of the browser class. The Load event is an event that occurs only once after the navigated page has been completely displayed. You can use this event to determine the end of the navigated page display.


The sample_006.cgx that has been rewritten considering the Load event is shown below.

1:

class main

2:

// getter

3:

closed getter directory _baseDir

4:

// Returns the directory where this script - sample_006.cgx - is located.

5:

rice riceEnv;

6:

file source = new file(riceEnv.SourceFileName);

7:

return source.Directory;

8:

endgetter

9:
10:

// method

11:

open method void start()

12:

// Navigation

13:

browser brw; // Declaration statement. Creates variable - brw - of browser class.

14:

ehswitch ehs = brw.Load(this, "LoadHandler(eventargs)"); // Handler assignment. The Load method returns the ehswitch class.

15:

ehs.OneShot(); // Starts handler execution. The handler started by OneShot () is called only once and stopped.

16:

brw.Navigate("https://www.google.com/"); // Calling the Navigate method.

17:

endmethod

18:
19:

open method void end()

20:

endmethod

21:
22:

// Handler method called when Load event occurs.

23:

open method void LoadHandler(eventargs ea)

24:

// Loads JavaScript code from sample_001.js.

25:

reader red = new reader(this._baseDir.FullName + "\sample_001.js");

26:

string jsCode = red.ReadToEnd();

27:

red.Release();

28:
29:

// Executes the JavaScript code.

30:

jsexecutor jse; // Declaration statement. Creates variable - jse - of jsexeutor class.

31:

jse.Execute(jsCode); // Calling the Execute method.

32:

endmethod

33:

endclass

The Load event is used in the start() method.

In fourteenth line, the event handler - event handling method - to be called in the Load event has been specified with the Load() method of the browser class.

The first argument of the Load method have to specify an instance that has a handler as a member. In this case, we use the this keyword to specify the current main class instance.

The second argument have to specify the signature of the handler method. The signature means a name used for calling.

The Load method returns the ehswitch class as a return value. it uses this to control the execution of the handler. We call the OneShot method in this example. The handler started by the OneShot method is called only the first time the event occurs.

For details about the Load method is here. For details about the ehswitch class is here.

This is the assignment of the event handler for the Load event. After the end of the assignment, The Navigate() method is called in the sixteenth line.


The Navigate() method ends immediately and at the same time the start() method also ends.

The Load event occurs after the Google search page is completely displayed. CookerGX executes the method assigned to this event by the Load() method. In this case, the LoadHandler method of the main class is executed.

At the end

Let's execute sample_006.cgx. You should see a dialog on Google search page.





As shown in tutorials so far, you can use CookerGX to manipulate web pages and extract information from within.

Previous
Copyright © CookerGX All rights reserved.