Meet it, it's C#. Simple, fast, convenient! + a selection of snippets inside.

  • Автор темы Автор темы LightWood
  • Дата начала Дата начала

LightWood

Moderator
Регистрация
04.11.2010
Сообщения
2 382
Реакции
917
Баллы
113
ZennoPoster is very flexible and easy to learn, but for ease of studying some of the functionality is missing by default, some not popular things can not be implemented by standard functionality or they are implemented in several steps although the action is "typical" or you have to use alot of cubes.
But for quickly and conveniently solution untipical parts we can use C#.
Spend an hour of time and you will already start to understand basics in snippets and how to write your first C# snippet.

C# (pronounced "c sharp") is a general-purpose, object-oriented programming language.
Read more - https://en.wikipedia.org/wiki/C_Sharp_(programming_language)

We are not going to learn C# now, but we will only analyze how to use and slightly edit ready-made code sections that will replace/supplement our code with cubes.

Snippet (in programming) - a small piece of source code or text, suitable for reuse. Mean a separate piece of code that can be used separately.
In our case, this is a piece of code that will replace 1 or more ordinary Zenno-cubes

Step snippet in the project:

nKr9Q9.png
LYLPVK.png


An example of a snippet that came to my attention first:
C#:
Развернуть Свернуть Копировать
String acc_full = project.Variables ["account_source"]. Value;
Var account = acc_full.Split ('|'). ToList ();
Project.Variables ["login"]. Value = account [0];
Project.Variables ["pass"]. Value = account [1];
Project.Variables ["hz"]. Value = account [2];

Often for snippets, you need the latest version of ZennoPoster. Many snippets use methods added in the latest versions. On older versions of ZennoPoster, the snippet may fail.

The method (in C#)
is a function or procedure belonging to a class or object. Like procedure in procedural programming, the method consists of a number of operators for performing some action and has a set of input arguments
In simple words, this is a certain command for an object, often with specifying parameters. An example will be considered below.

Each snippet often consists of 3 things:
1.
Set value to variables
2. Perform methods on these variables or other objects
3. Show the obtained result after processing by methods

This is not an obligatory sequence, and especially not necessarily the constituent parts.


In C# code, everything after // and before a line break is ignored, i.e. after the characters // as a rule write explanations to a line of code.
If you make a mistake in the snippet, ZennoPoster will tell you the number of the line in the logs and where this error occurred.

About variables:

-Variables inside a snippet, separate from the variables inside our template. They can be connected in some way, but this is not the same thing.
-Variables in C# are of different types. (it is the basics of C#, just google for understanding: "Types of variables (data) in C#"). Some methods work only with variable numbers, others with text, etc.
It's not like in the standard blocks of the template, where we set a variable to anything.

Here is an example of set variables in a snippet with comments to them:
C#:
Развернуть Свернуть Копировать
Var list = project.Lists ["BlackList"]; //project.Lists["BlackList "]; - In this format, we write our usual list in the project named "BlackList", i.e. In this line C# variable of type "var" with the name "list" we set the value of our list with the name "BlackList".
String id = project.Variables ["it"]. Value; // here we use the C# variable of type string and the name "id" to set a value to our variable from the project named "it".
Project.Variables ["text"]. Value = "LightWood writes templates to order and advises on ZennoPoster"; // Here, we set the text specified in quotes from the inside of the C# code to the project variable. You can also set the value of the C# variable or the result of the method execution, but in these cases, you no longer need to use quotes. The abundance of examples of ready-made snippets on the forum will help to understand)

About methods:
We have understood the methods above.
The method can interact with a variable, or it can interact with any other object.
For example here such line clears cookies of instance:
C#:
Развернуть Свернуть Копировать
Instance.ClearCookie ();
And such, will break a line on a separator |
C#:
Развернуть Свернуть Копировать
String acc_full = project.Variables ["account_source"]. Value;
Var account = acc_full.Split ('|'). ToList (); // Split ('|'). ToList (); - this is the method of separation
Only certain methods can be used for each kind of object. Clean the cookies for a variable or try to break an instance can not.)

How to correctly write a method and what it can be applied is always written to the description of the method, that in the method in which sequence should it stand. simply put, you take a sample from the instruction and set your values. Nothing complicated.

ZennoPoster methods are posted here - http://zennolab.com/wiki/en:zennoposter:macros-documentation. Choose your version of Zenno.

Output of result C# of the step:
In the value of the veriable made by the C# step, the default text is "OK", but if there is a line in the snippet code:
C#:
Развернуть Свернуть Копировать
Return [...];
Where [...] can be a variable, a text, etc., example (return name; or return "finish";-)
Then the value of the variable of C# step will be equal to the value of what is specified in the return string.

On the introduction to the basics that's all. Next, you need to look a little and get a grasp of the finished snippets and comments to them.


* Section of snippets on the forum, here you can to find ready-made snippets and ask questions - http://zennolab.com/discussion/forums/snippets.143/

In my experience I would say that in 95% of cases already on the forum (russian part of forum) there are ready-made solutions for the necessary tasks in the snippets section, you just need to find them and maybe just tweak or combine with something.

Officially nobody must write a snippet at the request, even if you have a pro version of ZennoPoster. This is not part of the support area, but if the question is correctly formulated, then the question is unlikely to be ignored by the Zenno community.

P.S. I'm not a C# coder, if I wrote it somewhere incorrectly, then please inform me in the subject.)
 
Последнее редактирование:
The selection of snippets that I most often use:
(Snippets of mine, familiar coders and our members of the forum)

Print the number of elements the page
C#:
Развернуть Свернуть Копировать
return instance.ActiveTab.FindElementsByAttribute("tag","name of attribute","value","type of search").Count;


Set the value of the variable, based on the value of another variable
If the month variable of our project is equal to "jan", then the result of the snippet will be = 1, if "feb", then the result of the snippet will be = 2, etc.
At its best, replaces the switch step + setting the value with a change.
C#:
Развернуть Свернуть Копировать
return "jan|1,feb|2,mar|3,apr|4,may|5,jun|6,jul|7,aug|8,sep|9,oct|10,nov|11,dec|12".Split(',').First(s=>s.Contains(project.Variables["month"].Value)).Split('|')[1];


Change the size of the instance

C#:
Развернуть Свернуть Копировать
instance.SetWindowSize(800,600);


Replacement with "donor", by regular expression

C#:
Развернуть Свернуть Копировать
return Regex.Replace(project.Variables["text"].Value,"([a-zA-Z; ])","$1*",RegexOptions.None); // After each letter in the variable text will put *.
// if it was ab1123g, then the result of the accomplishment a*b*1123g*


Split the line on the separator | and set the resulting parts to variables.
C#:
Развернуть Свернуть Копировать
string acc_full = project.Variables["account_source"].Value;
var account = acc_full.Split('|').ToList();
project.Variables["login"].Value = account[0];
project.Variables["pass"].Value = account[1];
project.Variables["hz"].Value = account[2];


Check for the presence of a line in the list. With regard to the register, etc., ie. The text in the variable and the list line must be identical. If a line is finded, it returns true, and otherwise false.
C#:
Развернуть Свернуть Копировать
var list = project.Lists["BlackList"];
string id = project.Variables["id"].Value;
if(list.Contains(id))
{
  return "true";
}
else
{
  return "false";
}


Looking for and showing from the list a string that contains text from a variable. Case insensitive.
C#:
Развернуть Свернуть Копировать
List<string> list = new List<string>();
list.AddRange(project.Lists["all"]);
string myString = project.Variables["recipient"].Value;
var matchingvalues = list
.FindAll(x => x.IndexOf(myString, StringComparison.OrdinalIgnoreCase) != -1);
return matchingvalues.First();


Code for search of the partial match in the list. If any of the string in the list includes the value of the variable, then output yes, otherwise no.
C#:
Развернуть Свернуть Копировать
// We take from the variable the text that should be searched for
var textContains = project.Variables["tmp_city"].Value;
// Get the list in which we will search
var sourceList = project.Lists["city"];
// Looking for each line in the list
lock(SyncObjects.ListSyncer)
{
  for(int i=0; i < sourceList.Count; i++)
  {
  // Read the string from the list
  var str = sourceList[i];
  // Check the contents of the text in the line, if there is a match, return "yes"
  if (str.Contains(textContains))
  return "yes";
  }
}
// If you find nothing, then returned "no"
return "no";


Searches in the table in the specified column the cell that contains the variable, if it finds that it outputs true, and otherwise false.
C#:
Развернуть Свернуть Копировать
var table = project.Tables["base"];
for(int i = 0; i < table.RowCount; i++)
{
    int col_num = 0; //Column sequence number
    string cur_string = table.GetCell(col_num,i); //The current line that is being processed
    //project.SendInfoToLog(cur_string);
    if(cur_string.Contains(project.Variables["partSite"].Value))
    {
    
        return "true";
    }
}
return "false";


Change the type of the variable to work inside C # methods
C#:
Развернуть Свернуть Копировать
// Change the project variable mailDays of type string, to type int
int mailDaysC = int.Parse(project.Variables["mailDays"].Value);

// Change the project variable useSSL of type string, to type bool
bool mailDaysC = bool.Parse(project.Variables["useSSL"].Value);


Set the value to 0 for all checkboxes on the page
C#:
Развернуть Свернуть Копировать
HtmlElementCollection hecol = instance.ActiveTab.FindElementsByAttribute("input:checkbox", "fulltagname", "input:checkbox", "regexp");
for(int i = 0; i< hecol.Count; i++)
{
    hecol.Elements[i].SetValue("0", instance.EmulationLevel, false);
}


Click on all checkboxes on the page
C#:
Развернуть Свернуть Копировать
HtmlElementCollection hecol = instance.ActiveTab.FindElementsByAttribute("input:checkbox", "fulltagname", "input:checkbox", "regexp");
    for(int i = 0; i< hecol.Count; i++)
    {
        hecol.Elements[i].Click();
    }


Get the file size in bytes
C#:
Развернуть Свернуть Копировать
var length = new System.IO.FileInfo(@"C:\img.png").Length;
return length;


Get the number of characters in the value of the variable
C#:
Развернуть Свернуть Копировать
return project.Variables["var1"].Value.Length;


Split the file into small ones by separator inside the text. 11111 - delimiter in the text. The files are saved in the template folder.
The text initially looks like this:
a
b
c
11111
d
e
f
11111
g
h
i
C#:
Развернуть Свернуть Копировать
string data = File.ReadAllText(project.Directory+"\\"+ "data.txt");
string separator = "11111";
string[] text = data.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < text.Length; i++)
   {
      File.WriteAllText(project.Directory+"\\"+ i + ".txt", text[i].Trim());
   }


Set the value of var1 in the clipboard and does an insertion by pressing ctrl + v
C#:
Развернуть Свернуть Копировать
var descr = project.Variables["var1"].Value;
System.Windows.Forms.Clipboard.SetText(descr);
instance.ActiveTab.KeyEvent("v","press","ctrl");
 
Последнее редактирование:
Thank you. that was a thorough explain of the topic.
 
that was the best and i don't have any experience in programming but i've always been eager to learn C sharp for Zenno.
anyway one snippet got my attention and maybe you can solve my problem
please take a look at these pics.
i got these server:
and i loaded just 4 templates in zennoposter and 40 simultaneous threads but my resources are fulled up (i marked them by red)
if i use this snippet:
Change the size of the instance
this issue gets better?
can u explain what can i do for solving this problem?
i have 16 gig RAM and a decent CPU but as u can see it's getting pressure from zennoposter.
thanks u as always.
 

Вложения

  • 1.jpg
    1.jpg
    307 KB · Просмотры: 1 144
  • Untitled.jpg
    Untitled.jpg
    306,9 KB · Просмотры: 847
Последнее редактирование:
thank for your great tutorials!
 
hi i need to select a dropdown with c# can someone help me?
 
thank for your great tutorials!
 

Кто просматривает тему: (Всего: 0, Пользователи: 0, Гости: 0)