Save html page in 1s 8.3. Creating a link to a database object

27
When integrating 1C with websites, the question always arises of transferring some data to a web server. Whether it is the transfer of request parameters to receive data from the web server to 1C, or the transfer of data from 1C, which ... 14
Developers on the 1C platform sometimes have the task of interacting the 1C: Enterprise system with various mail programs and protocols. 1C Enterprise 8.0 has quite convenient means for sending ... 9
When exchanging data with websites, the JSON format is often used. Unfortunately, in 1C there is no standard procedures to work with this format. In the process of implementing one of the projects, I developed a number of procedures and ... 8
The JSON format in 1C before version 8.3.6 is not implemented, but below I will give examples of functions that can be used for the full-fledged work of JSON in 1C previous versions... JSON ( JavaScript Object Notation) is text format data exchange, ... 5
In this article I will try to describe the process of parsing sites using 1C tools with an example. This article is not intended as an instruction for use, but only demonstrates the capabilities of 1C. What we have? 1. A website on the Internet where ...

During the implementation of one project, it became necessary to execute javascript (hereinafter JS) code under the control of the FieldHTMLDocument object, with obtaining the result in 1C. The examples given in the articles found did not seem very convenient to use (purely personal opinion). It was decided to try to find another, simpler, solution, the search was given no more than 1 day.

DIV.setAttribute ("onclick", "alert (" Coordinates ");");

Let's execute the script

DIV.click ();

TA-DAH. everything works

Procedure RunScript (TextScript) DIV = Elements.doc.Document.getElementById ("TEST"); DIV.setAttribute ("onclick", TextScript); DIV.click (); End of Procedure

Update as of 08/02/2016

Another way of calling JS was suggested in the comments (thanks) if you have the ability to change the code HTML document, then you can write a function in it and then call it. passing JS code to it.

and then in 1C:

Items.HTMLFieldDocument1.Document.parentWindow.exec ("alert (" OK ")");

Returning the JS result to 1C

To transfer the result of the script to 1C from the Document FieldHTMLObject, we will use the event. Let's bind the OnClick event to the HTMLDocument Field, which receives 3 parameters as input:

  1. The element in which the event occurred (the HTMLDocument Field itself)
  2. Event object
  3. Sign of performing standard behavior

To trigger an event on click, you need to execute the following js code

Var evt = document.createEventObject (); // create an empty object event evt.propertyName = "funkts1"; // I propose to put the name of the result in the propertyName property (something like a type, or the name of the function from which the data was received) evt.data = "156"; // we will pass the result data to the data props document.body.fireEvent ("onclick", evt); // execute the event when the HTMLDocument Field is clicked

After executing this js code, the event object created by us will be sent to the procedure when pressed as the second parameter. Which is pretty handy to handle.

An example of parsing the answer.

& OnClient // get the name and result of the event. Procedure DockOnClick (Element, EventData, StandardProcessing) OperationName = EventData.Event.propertyName; OperationData = EventData.Event.data; If OpName = "" Then // not our event Return; EndIf; If OperationName = "func1" Then // processing result OtherwiseIf OperationName = "func2" Then // processing result // ...... EndIf; End of Procedure

This method eliminates the need for wait handlers and provides a convenient event identifier. By the identifier, we know exactly what data came to us, and how to parse it.

Supplements from 03/26/2016

And in this place, misunderstandings also occurred (the display is reproduced only on the platform 8.3.5.1570, but in order to prevent these situations in the future, I will give a solution) The very reason - when re-launching the form with the HTML field of the document at the time of the procedure call when clicking on the HTML field, the platform call stuck and felled the whole COM object(Exception occured (htmlfile): Unspecified error). The solution was to turn off the standard execution of the click event, and turn off the event bubbling mode. How to do this, see below.

// disable bubbling of the event

Evt.cancelBubble = true;

// disable the default behavior

Evt.returnValue = false;

As a result, we will get a function to transfer data to 1c from JS.

Function return1c (name, data) (var evt = document.createEventObject (); evt.propertyName = name; evt.data = data; evt.cancelBubble = true; evt.returnValue = false; document.fireEvent ("onclick", evt ););

Output.

By combining these methods, you can quite easily run the js code you need and receive data in 1C for further processing.

An example of implementation in

Sometimes it becomes necessary to display a web page directly on the form. I will consider this possibility of the 1C platform using the example of displaying a page with the yandex search engine on the form, and with search results for a word entered in the form field.

Those. here of this page:

Picture 1

To begin with, let's create an external processing, add the “Address” attribute with the “String” type, of unlimited length.

Picture 2

Let's create a processing form. Move the "Address" attribute to the form twice. We will rename one of the elements of the requisite form to “Page”, change the View of the element to “HTML document field”.

The form will take the form:

Figure 3

Let's start processing in the "Enterprise" mode and enter the address of an arbitrary Internet resource. And we get the result:

Figure 4

How can you make sure that the Yandex page is displayed in the Page field, but already with search results for a word?

Add the form attribute: "Keyword", transfer it to the form.

If you look closely at the address that appears in the Yandex address bar after searching for the keyword "1C" (and it will be like this: "http://yandex.ru/yandsearch?text=1%D1%81"), you can see that the entered word is encrypted and passed in the post variable text. From this we conclude that it remains only to figure out how to get the encrypted value of the request.

If you dig a little, you can find out that you can use the java-script function "encodeURIComponent" to encode variables in URLs. You can, of course, brute force to select the code for each character of the alphabet and write the encoder yourself, or you can use the capabilities of the wscript component, which can be connected from 1C.

So what we need:

A) Connect wscript

B) Based on keyword build a script that will receive the value returned by the "encodeURIComponent" function when passed to it as a parameter of our word, and then the resulting result will be written to text file with a predefined directory

C) Get a text file created using wscript from 1C and read it.

If we speak in the built-in language, then it sounds like this:

& OnClient Procedure KeyWordOnChange (Item) Object. Address = " http: //yandex.ru/yandsearch?text= "+ ConvertKeyWord (); End of Procedure & OnClient Function ConvertKeyWord () FileNameTime = GetTempFileName ("js" ) ; SaveDir = StrReplace (TempFileDir ()," \" , " /" ) ; Saving Directory = StrReplace (Saving Directory,"C: /" , "C: //"); TextDocument = New TextDocument; Text Document. SetText ("value = encodeURIComponent ("" "+ Keyword +" "" ) ; | FSO = WScript. CreateObject ("" Scripting ... FileSystemObject" " ) ; | f = FSO. CreateTextFile ("" "+ Saving Directory +" test ... txt" + " " " , true);| f ... WriteLine (value);| f ... Close ();"); TextDocument. Write (FileNameTime, TextCode. ANSI); WshShell = New COMObject (" WScript ... Shell"); WshShell. Run (" wscript ... exe"" "+ FileNameTem +" "" ", 0, - 1); DeleteFiles (FileNameTime); TextDocument = New TextDocument; TextDocument. Read (TemporaryFiles Directory () +" test ... txt"); String = DocumentText. GetText (); Return Abbreviation (String); EndFunction

The HTML editor is used to create custom descriptions in the form of HTML documents. The editor window contains three tabs that allow you to view and edit the document.

On a bookmark Editing the document is displayed in text and graphic form:

On a bookmark Text the developer has the ability to edit the HTML text of the document:

Bookmark View is designed to display the document in the form in which it will be shown to the user:

Working with an HTML document is similar to working in a regular text editor, but the HTML editor provides a number of additional opportunities which are due to the use of the HTML format:

Text formatting

The text of an HTML document can be formatted: set its style, paragraph indents, select font size, options, typeface, etc .:

When copying text from others text editors(eg Word) HTML editor preserves the original text formatting.

Tables

You can insert tables into an HTML document and edit both the parameters of individual table cells, and change appearance and parameters of the whole table:

Pictures

Pictures can be placed in the HTML document. The developer has the ability to set the size of the picture, its position and the thickness of the frame around the picture:

Lettering

The HTML editor allows you to use labels in documents - special objects of an HTML document. You can place text, table, picture or other inscription in the label. As you view the document, the nested label objects will scroll continuously from right to left.

The 1C platform is a fairly powerful tool designed to solve a wide range of tasks. In this article, we want to talk about one type of object with which you can:

  • Work with ActiveX components;
  • Call Javascript;
  • Create and use links to various database objects.

This object is called in 1C the field of the HTML document.

The HTMLDocumentField control has one significant limitation in its use - it cannot be used for fields located in a table.

Creating your own browser in 1C

Let's try using this field to create a processing that opens a link to a resource on the Internet:

Let's check how it works .. Get a picture (Fig. 4)
Fig. 4.

Let's analyze what we see:

  1. A search window has opened, with a link to the site, while the search is carried out using search engine assigned by default;
  2. Processing works, web pages open, links are clicked;
  3. A script error occurred while calling the resource.

What is the error? Due to the fact that the DocumentHTMLField is actually a typical Internet Explorer, and not the most latest version which is not supported by most modern resources. So sites that do not support Compatibility Mode will not open with scripted errors.

For the convenience of the user, you can create navigation commands "Forward" and "Back" on the form, which will describe the corresponding movements.

Creating a link to a database object

The challenge is as follows:

  • Create a list of any objects, let's say the elements of the "Employees" catalog;
  • Get a link to each of them;
  • Open them directly by clicking on the corresponding link.

Let's create our processing and shape on it.


Let's take a closer look at what we have done:

  1. Created Text Document which contains the "body" of our site;
  2. The beginning and end of the document, as well as the body of our HTML document, we described using the appropriate tags;
  3. Each employee was enrolled in it;
  4. In the records of employees, we contain information that we will need later, when we open elements by clicking on the full names of employees;
  5. We set our field to a text that describes the structure.

Having tried now to activate any of the existing hyperlinks, we will face the fact that "This page was not found" (Fig. 8)
Fig. 8

Let's continue to describe the handler.

Let's create a procedure that processes a mouse click on a hyperlink to a directory item (Fig. 9).

Fig. 9

Let's consider it in more detail:

  1. First of all, turn off the standard handling of clicking on a hyperlink;
  2. We will receive a string characterizing our element, for this we will get the value of the Element.id parameter in EventData;
  3. Let's divide the resulting string in accordance with the rules by which we formed it (the name of the directory and the unique identifier, separated by a dash);
  4. Get a link to an item using the ReturnLink function;
  5. We open the element by the received link.

Let's test the execution of the generated code.

Since the use of modal windows is prohibited in the settings of our program, the execution of the above code ended with an error (Fig. 10). Otherwise, everything would have worked fine.

Fig. 10

Let's go to the configurator in the menu Text-> Refactoring-> Deprecated synchronous calls-> Convert module calls. After some time, the text of our module will look like Fig. 11

Fig. 11

Having tried the processing again, you can make sure that everything works fine and when you click on the link, the corresponding element of the reference book opens.

Some explanations for the code. Statistics say that it is unlikely that elements with the same identifier exist within the same database, but the platform can only guarantee uniqueness within the same table. That is why we formed the link from two parts.

A few words about Javascript

JS function calls in earlier versions programs were implemented through the document.parentWindow.MyFunk () field parameter, but this feature has been lost in modern browsers. Calls via eval () do not work either.

The way out is to use the special function fireEvent, which is a universal mechanism for exchanging data between 1C and Javascript.