10

I have an old html page that creates a script file and executes it using:

fsoObject = new ActiveXObject("Scripting.FileSystemObject")
wshObject = new ActiveXObject("WScript.Shell")

I am trying to modify it and make it usable also from other browsers. If you know the answer stop reading and please answer. If there is no quick answer, here is the description of my attempts. I was successful in doing the job, but only when the script is shorter than 2000 characters. I need help for scripts longer than 2000 characters.

The webpage is for internal use only, so it is easy for me to create a custom URL protocol on each computer that runs a VBScript file from a network drive.

I created my custom URL Protocol that starts a VBScript file like this:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\MyUrlProtocol]
"URL Protocol"=""
@="Url:MyUrlProtocol"
"UseOriginalUrlEncoding"=dword:00000001

[HKEY_CLASSES_ROOT\MyUrlProtocol\DefaultIcon]
@="C:\\Windows\\System32\\WScript.exe"

[HKEY_CLASSES_ROOT\MyUrlProtocol\shell]

[HKEY_CLASSES_ROOT\MyUrlProtocol\shell\open]

[HKEY_CLASSES_ROOT\MyUrlProtocol\shell\open\command]
@="C:\\Windows\\System32\\WScript.exe \"X:\\MyUrlProtocol.vbs\" \"%1\""

In MyUrlProtocol.vbs I have this:

MsgBox "The length of the link is " & Len(WScript.Arguments(0)) & " characters"
MsgBox "The content of the link is: " & WScript.Arguments(0)

When I click on <a href="MyUrlProtocol:test" id="test">click me</a> I see two messages, so everything works well (tested with Chrome and IE in Windows 7.)

It works also when I execute document.getElementById("test").click()

I thought this could be the solution: I would pass the text of the script to the VBS static script, which would create the dynamic script and run it, but with this system I can't pass more than ~2000 characters.

So I tried to split the text of the script in chunks smaller than 2000 characters and simulate several clicks on the link, but only the first one works.

So I tried with xmlhttp.open("GET","MyUrlProtocol:test",false);, but Chrome says Cross origin requests are only supported for HTTP.

Is it possible to pass more than 2000 characters to a VBScript script via a custom URL protocol?

If not, is it possible to call several custom URL protocols in sequence?

If not, is there another way to create a script file and run it from Javascript?

EDIT 1

I found a solution, but in Chrome only works when it likes, so I'm back to square one.

The code below in IE executes the script 4 times (correct), but in Chrome only the first execution runs.

If I change it to delay += 2000, then Chrome usually runs the script 2 times, but sometimes 1 and sometimes 3 or even 4 times.

If I change it to delay += 10000, then it usually runs the script 4 times, but sometimes misses one.

The function is always executed 4 times, both in Chrome and IE. What is weird is that the sr.click() sometimes does nothing and the function execution continues.

<HTML>
<HEAD>
  <script>
    var delay;

    function runScript(text) {
      setTimeout(function(){runScript2(text)}, delay);
      delay += 100;
    }

    function runScript2(text) {
      var sr = document.getElementById('scriptRunner');
      sr.href='intelliclad:'+text;
      sr.click();
    }

    function test(){
      delay = 0;
      runScript("uno");
      runScript("due");
      runScript("tre");
      runScript("quattro");
    }
  </script>
</HEAD>
<BODY>
  <input type="button" value="Run test" onclick="test()">
  <a href="nothing yet" id="scriptRunner">scriptRunner</a>
</BODY>
</HMTL>

EDIT 2

I tried with Luke's suggestion of setting the next timeout from inside the call back but nothing changed (IE works always, Chrome whenever it likes).

Here is the new code:

var scripts;
var delay = 2000;

function runScript() {
  var sr = document.getElementById('scriptRunner');
  sr.href = 'intelliclad:' + scripts.shift();
  sr.click();

  if(scripts.length)
    setTimeout(function() {runScript()}, delay);
}

function test(){
  scripts = ["uno", "due", "tre", "quattro"];
  runScript();
}

Some background: The page asks for the shape of a panel, which can be just a few parameters [nfaces=1, shape1='square', width1=100] or hundreds of parameters for panels with many faces, many slots, many fasteners, etc. After asking for all the parameters a script for our internal 3D CAD (which can be larger than 20KB) is generated and the CAD is started and asked to execute the script.

I would like to do all on the client side, because the page is served by a Domino web server, which can't even dream of managing such a complex script.

stenci
  • 8,290
  • 14
  • 64
  • 104
  • First of all, you should possibly increment `delay` from *within* the setTimeout callback and then set the next Timeouts from there too, to avoid problems with async code execution. I'm not sure what script you're passing to your URL-handler, or how you're actually passing it? A bit more detail or code example would help here. I'm sure there's a way to manage this without having to chunk your VBScript, which sounds prone to many problems. – Luke H Apr 22 '14 at 14:04
  • @LukeH I tried following your suggestion, but it didn't help. I added some background info at the end of the post. – stenci Apr 22 '14 at 19:56
  • Possibly it's quicker to rewrite the script in javascript than it is to find a workaround to run VBScript? Sorry I can't help more, but it sounds complex enough that I'd need to see the code. – Luke H May 01 '14 at 13:50
  • @LukeH I'm open to javascript or any other language. The script needs to start Excel, open an Excel file, start a CAD and run a script inside the CAD. I do that now with ActiveX in IE, and I'm trying to do it with other browsers – stenci May 02 '14 at 16:17
  • @dandavis Some scripts are fast and should run in real time. Right now in IE+ActiveX some javascript events get the CAD to show the correct drawing as soon as the parameters are changed in the html form. – stenci May 02 '14 at 16:20
  • i think there is no chance to get this work because google chrome blocks click method used for many times, , some kind of popup blocker. The big advanced of IE is supporting ACTIVE X for scripting in the operating system, which other browsers dont support. the question is, why do you want to do such a workaround, instead of simply downloading the google chrome active x plugin or the active x plugin for firefox –  May 05 '14 at 21:01
  • @ThorstenArtner'Austria' IE must be configured to trust my server and to run activex. Microsoft for some reason feels free to reset all the configurations of all the computers every time there is a security update. On top of that add the installation (and configuration?) of different extensions for different browsers. I am trying to keep the configuration to the minimum, just the double click on a .reg file, regardless of your browser and machine configuration. Of course if the custom URL worked would be a very good solution. Unfortunately there are problems, and now I need to re-evaluate it. – stenci May 06 '14 at 19:15
  • @ThorstenArtner'Austria' I thought of some kind of popup blocker, that's why I added a pause. But even a 10 second pause doesn't solve the problem. – stenci May 06 '14 at 19:21
  • a break doesnt solve the problem because then the click has no more connection to the click initiated by the user and so its also ignored –  May 06 '14 at 19:44
  • @ThorstenArtner'Austria' The only explanation I can think of is that this is a bug of Chrome. The behavior is inconsistent, sometime fails and sometimes works. Why would the popup blocker randomly block some popups only? – stenci May 06 '14 at 21:45
  • 1
    ok i didnt recognized that because i tried your script and Chrome always gets stuck at number uno regardless of changing the timer. as you said IE must be configured to trust my server, there is an option of HTA with IE... HTA is HTML Application, i dont know if you know that its a simple html file with a header with the hta tag. it has to be renamed to .hta then its treated like an exe application. I'm programming HTA since IE5 . it gives you full access without trusting ACTIVE X components. its like you double click an exe file. –  May 07 '14 at 07:21
  • @ThorstenArtner'Austria' I didn't know the HTA, interesting. In this case as I said I have a Domino server, which is as flexible as a dried stick. But the HTA could be useful in other cases. – stenci May 07 '14 at 13:49

3 Answers3

4

I didn't read your whole post...have an answer:

I too wish that custom url protocols can handle long urls. They simply do not. IE is even worse as some OSs only accept 800 chars.

So, here's the solution:

For long urls, only pass a single use token.   The vbscript uses the token 
and does a url get to your web server to get all of the data.

This is the only way I've been able to successfully pass lots of data around. If you ever find a clearer solution, please remember to post it here.

Update:

Note that this is the best way I have found to deal with the url protocol limitations. I too wish this was not necessary. This does work and works well.

You mentioned Dominos, so possibly you need something in a POS environment... I create a web based POS system, so we could face a lot of the same issues.

Suppose you want a custom url to print a pdf to the default printer without the annoying popup window. We need to do this thousands of times a day...

  1. When building the web page, add the print button which when pressed calls the custom url: myproto://printpdf?id=12345&tocken=onetimetoken

  2. this will execute your vbscript on the local desktop

  3. in your vbscript, parse the arguments and react. In this case, your command is printpdf and the id is 123456 and you have a onetime tocken key.

  4. have the vb script to an https get to: https://mydomain.com/APIs/printpdf.whatever?id=12345&key=onetimetoken

  5. check the credentials based on the ip address and token, if all aligns, then return the contents of the pdf (you may want to convert the pdf to a byte array string)

  6. now the vbscript has the pdf, assemble it and write it to a temp folder then execute a silent pdf print command (I use Sumatra PDF http://blog.kowalczyk.info/software/sumatrapdf/free-pdf-reader.html)

  7. mission accomplished.

Since I do know what you what to do in your custom url and the general workflow, I can only describe how I've solved the sort url issue.

Using this technique, the possibilities are limitless. You have full control over the local computer running the web browser, you have a onetime use token which grants access to a web API with can return any sort of information you program.

You could write a custom url protocol to turn on the pizza oven if you wanted :)

If you are not able to create the server side code which is listening for vbscript's get request then this would not work.

You might be able to pass the data from the browser to the vbscript using the clipboard.

Update 2:

Since in this case the data is on the client (one single form can define hundreds of parameters), the server API doesn't know what to answer to the vb script request. So the workflow described above must be preceded by these two steps:

  1. The onkeypress event executes a submit to send the current parameters to the server

  2. The server replies with the refreshed form, adding to the body onload a call to a function which uses another submit to call the custom url, as described on point 1 listed above.

Update 3: stenci, what you've added (in Update 2) will work. I would do it like this:

  1. user presses a button saying I'm done editing the form
  2. ajax post the form to the server
  3. the server saves the data and attaches unique key to the datastore
  4. the server returns the key to ajax callback function
  5. now the client has a single use key and invokes the url schema passing the key
  6. vbscript does an https get to the server and passes the key
  7. server returns the data to the vbscript

It is a bit long winded. Once coded it will work like a charm.

The only other alternative I can see is to copy the form data to the clipboard using something like: http://zeroclipboard.org/

and then in vbscript see if you can read the clipboard like: Use clipboard from VBScript

Community
  • 1
  • 1
Brian McGinity
  • 5,777
  • 5
  • 36
  • 46
  • Making a round trip is very difficult because there is a Domino web server, which does what it likes, not what I like. Plus a round trip is slow. I need more details. How does it work? The parameters are on the browser, not on the server. Should the browser submit all the parameters and the page returned by the server should start the custom URL in the onload event? – stenci May 06 '14 at 05:08
  • Yes, it is a round trip and it is is 20-60ms depending on the network and it is a bummer--it's fast enough that my users have no issues. I'll update my answer to describe how I handle this. For this to work in your environment, you will need to be able to write server-side code. Eventually you are creating a API for your custom protocol. – Brian McGinity May 06 '14 at 12:32
  • Your idea works assuming that the server has the info required to do the job. In my case the data is on the client. So I need two synchronized round trips. Too difficult to describe on a comment, I added an update. Please let me know what you think and feel free to edit... your own answer! – stenci May 06 '14 at 19:03
  • THE CLIPBOARD, why didn't think about it!!! I will investigate that road before starting working on the server side. Thanks. – stenci May 06 '14 at 21:52
  • If you can share what you find, that would be great. I was hesitant to use the clipboard because it seemed like having flash installed was mandatory or that the user would be prompted each time javascript tired to access the clipboard--which might be incorrect. If you're willing to share what you find, please do. – Brian McGinity May 07 '14 at 11:13
2

How about creating an iFrame for each instance? Something like this:

function runScript(text) {
  var iframe = document.createElement('iframe');
  iframe.src = 'intelliclad:'+text;
  document.body.appendChild(iframe);
}

function test(){
  runScript("uno");
  runScript("due");
  runScript("tre");
  runScript("quattro");
}

You can then use css styling to make these iframes transparent / hidden.

urish
  • 8,943
  • 8
  • 54
  • 75
0

You might not like this answer, but I've used this method in the past and it works.

Instead of relying on ActiveX, consider using a Java Applet, and JNI.

Basically, you have to make sure the native scripts you want to run are available on your client machine, along with a JNI wrapper.

The applet will have to be at least self signed, for the browser to allow it to load and access a native library. Once the JNI libraries are loaded, you can easily call methods from the page / applet.

As a consequence of using Java, you could possibly use the same applet for windows as well as linux clients, provided of course you have native libraries present on the respective clients.

This series of articles talks about precisely your problem : http://www.javaworld.com/article/2076775/java-security/escape-the-sandbox--access-native-methods-from-an-applet.html

P.S the article is really old, but the concept remains unchanged.

Akshay
  • 3,158
  • 24
  • 28
  • active x is also supported in mozilla and google chrome by downloading the active x plugin –  May 05 '14 at 21:03
  • I wasn't implying ActiveX is not cross browser compatible, in fact given that the OP is deploying this in a controlled environment, cross browser / platform compatibility is probably not even an issue. – Akshay May 05 '14 at 21:09
  • i haven't thought about there could be different plattforms on the computers, but that would be a problem. youre right –  May 05 '14 at 21:17
  • does anyone like java applets? – Brian McGinity May 06 '14 at 00:19
  • I didn't understand that last comment @BrianMcGinity.. and btw, when was the last time java security rules changed ? Are you saying suddenly security rules change and code stops working? – Akshay May 06 '14 at 16:17
  • I understand you don't like applets, I'm with you there. But this exchange seems like just oneupsmanship. If you think the answer is wrong, please downvote and provide an explanation. – Akshay May 06 '14 at 16:18
  • 1
    Na... this answer is good...a java applet can do the job, coding them is just a pain for some (like me). I used to use the jzebra print applet to drive barcode printers https://code.google.com/p/jzebra it was slow to load, slow to run and when Java 45 Update 7 was released, it broke the applet as now applets need to be code signed (or somthing). This drove me crazy as all of sudden my users could not longer print barcodes. My solution was to create a custom url protocol--which works great..is very fast and easy to code and always works. – Brian McGinity May 06 '14 at 22:11