Page navigation
Purpose of this page
Cooker is an application for manipulating web pages, so you need to load the page that is the target of the maneuver.
On this page, we will learn about page navigation and the components of the Rice language by creating a script that navigates to the target page.
Table of contents:
Pane and browser
Cooker is a two-pane application. The Rice code on this page is implemented assuming it runs in the left pane, Main-Browser.
You can also do it in the right pane, but the result of the run will appear in the Main-browser.
Declaration statement
A simple page navigation script is shown below: This is a script to navigate Cooker to https://www.example.com/.
1: | class main |
2: | // Navigates the browser to example.com. |
3: | open method void start() |
4: | browser br; // Declaration statement: It creates a variable of "browser" class. |
5: | br.LoadUrl("https://www.example.com/"); // Calls "LoadUrl(string)" method of "browser" class. |
6: | endmethod |
7: | |
8: | open method void end() |
9: | endmethod |
10: | endclass |
Two lines are added to the template script on the previous page. The following explanation ignores the existence of comments.
Here, the point is the fourth line. The "browser" is followed by the "br" and ends with a semicolon.
A word sequence that meets the following rules is called a declaration statement. The fourth line meets these rules.
The first word must be a class name. It has been specified the "browser" that indicates the browser class in this case.
A class is a named group of states and operations. In Cooker, the state and functions of the browser are defined by the "browser".
The second word is the variable name. Here we use the "br" as appropriate name.
The variables and their naming rule are described later. it is enough to understand that the "br" is valid as a variable name for now.
The last is a semicolon. This is a symbol that indicates the end of a statement.
A declaration statement is the introduction of a name into a script. The above declaration tells Cooker about a name and class of the "br".
You can use variables of the "browser" class without definition. In that case, they represent the Main-browser.
LoadUrl(string) - call statement
Next is the fifth line. This is a word sequence that the "br" is followed by ".", and ends with "LoadUrl (" https://www.example.com/ ");".
A word sequence like this is called "call statement". The important thing in the call statement is the ".". This is an operator called the dot operator. It tells Cooker to call "members".
Some new terms have emerged, which we'll discuss later.
Cooker has already known that "br" is a browser class. And with the above statement, Cooker will understand the command "call the LoadUrl of the browser class".
There are a few types of "LoadUrl". Therefore, creates the exact name "LoadUrl(string)" from "LoadUrl" and ("https://www.example.com/") to determine what to call.
The function assigned to "LoadUrl(string)" is browser navigation. the URL needed for the navigation is passed with the "https://www.example.com/". The final semicolon marks the end of the statement.
When this call is executed, Cooker's browser will start navigating to "https://www.example.com/".
Script execution
Export the above sample to a suitable file and run it, the example.com is shown.
In the following explanation, we will assume that the name of the exported file is "example.cook".
When writing the above example to a file, delete the line number. This is for illustration purposes only and is not required to run the script.
The "example.com" is a domain provided for use as an example and is permitted to use in the literature.
The "example.cook" is a script that just navigates to a URL, but you can freely move to the desired URL by rewriting the "https://www.example.com/" part.
The "browser" class has many other features like "LoadUrl". In addition, Many other classes like "browser" are built in.
With these combinations, you can create new functions that you want.
Class
All the sample scripts that came out so far were definitions of the main class. And we already explained that can freely define new classes other than main.
Classes that users define independently are called "user-defined classes". And there are a few other types of classes.
Cooker has predefined classes that are not user-defined classes. These predefined classes are called the built-in classes.
There are three types of built-in classes. they are Rice-basic classes, Cooker-built-in classes, and Cooker-embed classes.
Rice-basic: Classes that can be used commonly by all Rice scripts. They are basic elements such as number and string.
Cooker-built-in: Classes that can be used only with Rice script on Cooker. They are components of Cooker such as window and browser.
Cooker-embed: Classes that are dynamically loaded when Cooker starts. They are classes to extend features.
For example, there is no program that does not handle numbers.
For example, Cooker has a browser as a component. We can easily predict that the browser is frequently operated from user-defined classes.
The built-in classes have predefined such features that are used by many users. The browser class mentioned above is one of such built-in classes.
A list of built-in classes can be check here.
Member
The term member in Rice means the component of class.
The member types are shown below.
field
fitter
setter
getter
method
See reference for more information about individual members.
The LoadUrl(string) mentioned above is a method that is a member of the browser class.
Variable and name
The definition of variables in Rice language is as follows.
A variable is a memory area that holds the address of an instance.
It is a reference type variable because it holds an address. It is identified by name and bound to the class.
An instance is a group of data that is the entity of a class. It is usually held in a contiguous area of memory identified by an address.
See reference for more information about variables in Rice language.
Above definition is not appropriate for tutorial, so we restate it easily.
Variable is a container with name. In addition, A variable is bound to a class as shown above, so the class to assign is fixed.
A value can be replaced because a variable is a container. However, since a class to assign is fixed, other classes can not be assigned.
A naming rules for variables are as follows.
Usable characters are single-byte characters. Multi-byte characters can not be used.
The first letter is underscore or alphabet, and followed by zero or more repetitions of the alphabet, underscore, or number.
Uppercase and lowercase letters are distinguished.
There is no limit on the number of characters.
Reserved words can not be used for names. Reserved words are keyword and built-in class name.
See reference for more details of the naming rules.
Variables naming rules are also naming rules for user-defined "name". User-defined elements, such as classes and members, must be named according to this rules.
Dot operator and member call
The dot operator is an operator for calling a member of the class to the left of the operator. In the above example, the left side of the dot operator is a variable of browser class. Therefore, a member of the browser class will be called. but the left side of the operator is not limited to a variable.
The left side of the operator is not limited to a variable. If class of the left side can be determined, a member of its class is called.
The above example has called the LoadUrl(string) method which is a member of the browser class. Methods are member that can pass zero or more values at calling time. These values are called the "argument".
The number of arguments and their class are strictly defined for each method. The LoadUrl(string) method is defined to pass a URL string, as an argument, so "https://www.example.com/" has been passed.
See reference for notations when no argument or more than one argument.
Statement and semicolon
Meaningful word sequences, such as a declaration and a method call, are called "statements".
The end of the statement is indicated by a semicolon. In fact, both declaration and call statements require a semicolon at the end.
However, There are several statements do not end with a semicolon. We will describe about these statements each time they come out.
Event handling
The current "example.cook" will not wait for finishing to move after calling the LoadUrl(string) method. That's because the method is implemented that way.
It may take some time to move the page, and the move may fail. It is necessary to judge "whether loading has been finished" in order to operate on the page after moving.
There are several ways to determine if loading is complete, but let's use event handling here.
The "event" is an event that occurs within an application such as a mouse click or typing characters.
Event handling is a sequence of giving a name to an event, detecting the occurrence, and calling the corresponding program - event handler.
To determine if the load is complete, we will use the "LoadCompleted" event of the browser class.
An event handler for this event is assigned by the "LoadCompleted(class, string)" method of the browser class.
Now, let's add the code for that to "example.cook".
1: | class main |
2: | // Moves the browser to example.com and captures loading completion. |
3: | open method void start() |
4: | browser br; |
5: | switch sw = br.LoadCompleted(this, "LCHandler(dictionary)"); // Assigns a handler to the LoadCompleted event. |
6: | sw.Start(); // Starts event handling. |
7: | br.LoadUrl("https://www.example.com/"); // Navigating. |
8: | endmethod |
9: | |
10: | open method void end() |
11: | endmethod |
12: | |
13: | open method void LCHandler(dictionary dic) |
14: | message m; // A class for displaying messages. |
15: | m.Show("LoadCompleted event has occurred!"); // Displays message. |
16: | endmethod |
17: | endclass |
At the fifth line, the "LCHandler(dictionary)" method of the "main" class has been assigned to the event. This method becomes the event handler.
Event handling is enable at the sixth line.
The fifth line is a definition statement. The definition statement declares and initializes a variable at the same time.
Here, it declares the variable "sw" of the "switch" class and sets the result of the right side of the symbol "=" as its content.
The symbol "=" is the assignment operator. It assigns the result of the right side of the operator to the left side. The left and right classes of the operator must match. Since br.LoadCompleted(this, "LCHandler(dictionary)") is implemented to return the "switch" class, it can be assigned to "sw".
See browser class: LoadCompleted(class, string) method for more information.
The keyword "this" has been used in LoadCompleted (this, "LCHandler (dictionary)"). The "this" has a special meaning like the "class" and "endclass".
The keyword "this" represents the this operator. The this operator is an expression that returns an instance of a user-defined class. Which user-defined class is returned depends on where it appears. In this case, it returns an instance of the "main" class because it appears in the definition of the "main" class.
The LoadCompleted(class, string) method returns an instance of "switch" class.
In this case, It returns an instance is set to call the LCHandler(dictionary) method of the "main" class in response to the "LoadCompleted" event.
At the sixth line, the Start() method of "sw" is called to start event handling.
See switch class: Start() method for more information.
Field
Two problems remain in the above "example.cook".
First | Event handling has not been stopped. |
Second | The success of page loading has not been judged. |
The following code fixes the problem.
1: | class main |
2: | browser br; // Declarations of field. |
3: | switch sw; |
4: | message m; |
5: | open method void start() |
6: | sw = br.LoadCompleted(this, "LCHandler(dictionary)"); // Assigns to the field. |
7: | sw.Start(); // Starts event handling. |
8: | br.LoadUrl("https://www.example.com/"); |
9: | endmethod |
10: | |
11: | open method void end() |
12: | sw.Stop(); // Stops event handling. |
13: | endmethod |
14: | |
15: | open method void LCHandler(dictionary dic) |
16: | if(dic.IsSuccess) // if statement |
17: | m.Show("Page loading is successful."); |
18: | else |
19: | m.Show("Page loading is failure."); |
20: | endif |
21: | endmethod |
22: | endclass |
Lines 2 to 4:
The variables "br", "sw", and "m" that were declared in the method definition have been moved out of the method definition.
Variables declared outside all member definitions are especially called fields.
The difference between a field and a variable is its reference range. A variable can only be referenced within the definition which it is located. Fields can be referenced from within all class definitions.
Such a reference range is called a scope. Variables that you want to share in various definitions in the class should be declared as fields.
Lines 11 to 13:
As explained on the previous page, the end() method is called at the end of the script. Calling sw.Stop() in the end() method guarantees that event handling will be stopped.
See following links for more information.
if statement
Lines 16 to 20:
A sequence of words that starts with the keyword "if" and ends with "endif" is called an if statement.
The if statement is a statement for branching execution by the expression in parentheses following "if".
The handler is called when the "LoadCompleted" event occurs. At this time, the information related to the "LoadCompleted" event is automatically set as the argument of the dictionary class.
"dic.IsSuccess" is one of the information which automatically set, and it is a judgment whether the page loading is successful. If "dic.IsSuccess" is true, the page loading is successful. Otherwise, it is failure.
Branches the execution depending on success or failure. In this case, the appropriate message is displayed.
See if statement for more infomation.
The if statement is a statement, but the "endif" is not followed by a semicolon. This is an example of a sentence that does not end with a semicolon.
The if statement can contain other statements inside. The above example contains the following statement:
m.Show("Page loading is successful.");
m.Show("Page loading is failure.");
There are several other statements that can contain other statements inside. They use a keyword to indicate the end of a statement.
See Statements in the Rice for more infomation.
It is possible to handle the success and failure of the page loading by replacing the code executed by the branch of the if statement with the appropriate one.