📜 ⬆️ ⬇️

Something about how javascript include javascript

Hello, friends.

Everything new is well forgotten old!
This is NOT about HTML / JavaScript, but only about JavaScript!
I was looking for this topic on Habré, but I did not find ...

I work as a system administrator in my company (windows domain) and therefore I often have to write various scripts. During the work, a library of functions (from .prototype, to WMI) was made, which always had to be dragged from file to file, which is very annoying and weighing down the code. It is very unpleasant when modifications of library functions begin (also written in JavaScript). Then you have to find where this function has already been used? The search process is tedious, and even debugging several files smacks. It was then that I remembered that several years ago I read somewhere on the forum (already after so many years and was forgotten on which), that it is possible to make such “include” .
')
This feature is called .WSF. The technology is ancient, like a chalk period. When you write one .js, then this technology seems a lot and you don’t want to exacerbate your vague xml code, but as soon as you remember that you would add a function from “that” script to “this”, you can’t do without this technology.
Here is a good description: http://www.osp.ru/win2000/2007/08/4684940/

From myself I want to add that the volume of the code immediately diminished when the entry appeared at the beginning of the .wsf files
 <? xml version = "1.0" encoding = "UTF-8"?>
 <package>
   <job id = "id01">
     <script language = "JScript" src = "\\ server03 \ install \ AdminScripts \ frameWork.js" />
     <script language = "JScript">
       <! [CDATA [// Write here our functions ...
 ]]>
     </ script>
   </ job>
 </ package>


In addition, you can make “include” not only JavaScript, but also VBScript.

But there was another problem. I would like to highlight the syntax js / vbs, and after all, no editor can disassemble the format .wsf, because there are mixed xml, javascript / vbscript. NDA, sometimes you wonder how to work with good technology through a notebook. Already having come to terms with this way of editing wsf-files, I suddenly discovered that it was not me alone who was concerned about such a problem !!! And it turned out that for this there were smart guys and wrote a great editor:
Script Editor (trial, but ...)
It has good features: the similarity of intellisense (highlights the path to the file from which the function “take”, for example, via ctrl-Space), the ability to run the program directly from the editor’s window (who uses scriptomatic to evaluate: Pasted it into this editor and forward - for revision). Built-in help for WScript objects, WshNetwork, and so on. The right objects.
With a minus in this editor, I found only the lack of intellectual indents and the almost complete absence of text editor settings. But the undoubted advantage was that instead of tabulation, four spaces are written (this is for my taste, I really like the four spaces!).

Here is an example:
The functions that I write to the frameWork.js file, and place the file in the "\\ server03 \ install \ AdminScripts \ frameWork.js" directory.
 // Display a message on the screen (in order not to write often WScript.Echo ()):
 // To suppress echo output, simply set this variable to false.
 // Very handy at the end of the debugging process, so that nothing is output.
 var boolShowEchoFunction = true;
 function echo (str)
 {
    if (boolShowEchoFunction == false)
    {
       return;
    }
     if (typeof (window)! = 'undefined')
     {
       window.alert (str);
     }
     if (typeof (WScript)! = 'undefined')
     {
       WScript.Echo (str);
     }
 }

 // Open the file and save the line in it (I write a lot of logs to text files):
 function appendStrToFile (fileName, strData)
 {
    try
    {
      var fso = WScript.CreateObject ("Scripting.FileSystemObject");

      // Expand the path into components and check whether there is a directory in which to write the file:
      var arrPath = new Array ();
          fileName = fileName.replace (/ [\ /] + / gi, "\\"). replace (/ ^ \\\\ /, "//").replace(/\\ / gi ,"\n") .replace (/ ^ \ / \ // gi, "\\\\");
          arrPath = fileName.split ("\ n");
      // Walk along the path and check the presence of each directory:
      var fileName = arrPath [0];
      var n = 0;
          for (n = 1; n <= arrPath.length-2; n ++)
          {
             fileName + = "\\" + arrPath [n];
             if (! fso.FolderExists (fileName))
             {
                  fso.CreateFolder (fileName);
             }
          }
          fileName + = "\\" + arrPath.last ();
          if (! fso. FileExists (fileName))
          {
              var file = fso.CreateTextFile (fileName);
                  file.Close ();
          }
      var f = fso. GetFile (fileName);
      var file = f.OpenAsTextStream (ForAppending, TristateTrue);
          file.Write (strData);
          file.Close ();
    }
    catch (e)
    {
      echo ("Error writing: \ nappendStrToFile (" + fileName + "," + strData + "): \ n" + e.description + ", \ n");
    }
    return;
 }

 // Get the string with today's date (aligned with zeros):

 function getStrToday ()
 {
    var now = new Date ();
    var strNowDate = now.getYear () + "-" + ((now.getMonth () + 1) <= 9? "0": "") + (now.getMonth () + 1) + "-" + ( now.getDate (<= 9? "0": "") + now.getDate ();
    return strNowDate;
 }

 function getStrTime ()
 {
    var now = new Date ();
    var strNowTime = (now.getHours () <= 9? "0": "") + now.getHours () + ":" + (now.getMinutes () <= 9? "0": "") + now .getMinutes () + ":" + (now.getSeconds () <= 9? "0": "") + now.getSeconds ();
    return strNowTime;
 }

 // Return the time with millisecond accuracy.
 function getStrTimeM ()
 {
    var now = new Date ();
    var strNowTime = (now.getHours () <= 9? "0": "") + now.getHours () + ":" + (now.getMinutes () <= 9? "0": "") + now .getMinutes () + ":" + (now.getSeconds () <= 9? "0": "") + now.getSeconds () + ":" + (now.getMilliseconds () <= 99? now.getMilliseconds () <= 9? "00": "0": "") + now.getMilliseconds ();
    return strNowTime;
 }




It's time to use the library.
The script writes to the file the name of the computer on which the script was run (write the file to the path "\\ server03 \ install \ AdminScripts \ powerOnToFile.wsf"):
 <? xml version = "1.0" encoding = "UTF-8"?>
 <package>
   <job id = "id01">
     <script language = "JScript" src = "\\ server03 \ install \ AdminScripts \ frameWork.js" />
     <script language = "JScript">
       <! [CDATA [// Register event entry
 function logonToFile ()
 {
        WshShell = WScript.CreateObject ("WScript.Shell");
        WshNetwork = WScript.CreateObject ("WScript.Network");

    var maxLen = 0;
    var now = new Date ();
    var fileName = "\\\\ server03 \\ Files \\ log \\ powerOn \\" + getStrToday () + "\\ powerOn - [" + WshNetwork.ComputerName + "] [" + getStrToday () + "]";  // Suffix of the file into which infa will merge.
    var strNowDate = getStrToday ();
    var strNowTime = getStrTime ();
        appendStrToFile (fileName + ". txt", "\ r \ n [" + strNowDate + "] [" + strNowTime + "] [" + WshNetwork.ComputerName + "]");
 }
 logonToFile ();
 ]]>
     </ script>
   </ job>
 </ package>

Through Active Directory in GPO-> Computer Configuration-> Windows Configuration-> Startup / Completion Scripts-> Startup-> "cscript.exe", "\\ server03 \ Install \ AdminScripts \ powerOnToFile.wsf"

Now I know that the computer was turned on today.

The second script determines the user's login on the computer on which this script is running:
(write the file to the path "\\ server03 \ install \ AdminScripts \ logonToFile.wsf"):

 <? xml version = "1.0" encoding = "UTF-8"?>
 <package>
   <job id = "id01">
     <script language = "JScript" src = "\\ server03 \ install \ AdminScripts \ frameWork.js" />
     <script language = "JScript">
       <! [CDATA [// Register event entry
 function logonToFile ()
 {
        WshShell = WScript.CreateObject ("WScript.Shell");
        WshNetwork = WScript.CreateObject ("WScript.Network");

    var maxLen = 0;
    var now = new Date ();
    var fileName = "\\\\ server03 \\ Files \\ log \\ powerOn \\" + getStrToday () + "\\ powerOn - [" + WshNetwork.ComputerName + "] [" + getStrToday () + "]";  // Suffix of the file into which infa will merge.
    var strNowDate = getStrToday ();
    var strNowTime = getStrTime ();
        appendStrToFile (fileName + ". txt", "\ r \ n [" + strNowDate + "] [" + strNowTime + "] [" + WshNetwork.ComputerName + "]");
 }
 logonToFile ();
 ]]>
     </ script>
   </ job>
 </ package>


Through Active Directory in GPO-> User Configuration-> Windows Configuration-> Startup / Completion Scripts-> Startup-> "cscript.exe", "\\ server03 \ Install \ AdminScripts \ logonToFile.wsf"
Now, when users turn on computers in the morning and log in to them, they’re in the directory
\\ server03 \\ Files \\ log
I write files with the information I need, that the computer is turned on and that the user has logged on to a specific computer with a specific login. As you can see, both files used the echo () and appendStrToFile (), getStrToday () functions, which were taken from the frameWork.js library file.

Now, as an administrator, I have information on which computers were turned on today and which users logged on.

I hope that such a simple technology with this editor can bring huge benefits to someone.

Source: https://habr.com/ru/post/126802/


All Articles