how to protect JS file

  • Hi,

    I feel stupid, but can't seem to encrypt a simple JS file. This file only has some js functions, being called from a button on the HTML page with the embed viewer.
    Perhaps that's the reason, should this JS file comply with certain standards (like the template for plugins )?

    It works perfectly without this encryption. Only necessary for HTML5.
    I am using the 1.19-pr3 (tried also 1.18), have the licenses included, just plain encryption (no private) and also created the viewer files with the same version.

    What am I doing wrong or not seeing, please?

    If it's not possible, what other solution would I have to not publicly show these functions?

    Thanks!

  • Thanks for joining in Umalo, but no, unfortunately that doesn't change things either...
    (btw, it's -h5 right?).

    Any other suggestions?

    First of all, it should be possible right? Just a simpel JS file with only a (couple of) functions?

  • BTW, I tried it with another JS file as well, same problem. Similar stuff, just a couple of functions.

    Are there any other ways?
    I am now trying an Obfuscator, but would rather encrypt it of course..

  • Tried cleaning up even further (replacing double quotes with single ones, etc.), but still no luck. Tried all settings for the encrypttool via commandline, as well as krpanotools32 and krpanotools64 (I am on a 64bit machine), but still to no avail.

    I can't get it to work unfortunately...
    Has anybody actually been able to encrypt JS files successfully?

  • yes i did
    did you use the 'red iconed' Krpanotools encryption tool from the 19.pr3 package?
    Maybe post your js file.. so we can see.

    Tuur *thumbsup*

  • Hi Tuur, thanks again for following up, much appreciated!

    Yes, I did use the 'red iconed' Krpanotools, as well as via commandline with krpanotools32 and krpanotools64 directly.

    You asking me for the code got me thinking to go back to basics, as I just tested a very simple Hello World script. Even this didn't work!
    So, I am either missing something in the JS file, or I am calling it the wrong way, or I need to include it in a different way.

    This is what I have, which works as long it is not encrypted:
    1. JS file, only 1 function:

    Code
    function down_img()
    {
       alert ("Hello World!");
    }

    2. include in HTML HEAD:

    Code
    <script src="../js/script.js"></script>

    3. calling via a button in vtourskin:

    Code
    <layer name="skin_btn_img" style="skin_img|skin_tooltips" tooltip="JPG Snapshot" align="left" x="125" y="0" scale="0.5" onclick="js(down_img());" devices="html5" />
  • Try including the js file in the krpano xml instead of putting it in the index.html.
    Then it will be read by krpano, and understands the encryption.
    Not, sure but i think that is the problem.

    Tuur *thumbsup*

  • Sorry.. that is probably not gonna work.

    try to put the javascript in the xml, like:

    Code
    <action name="test" type="Javascript" autorun="">
    	<![CDATA[
    		alert("Cheers from Holland");
    	]]>
    </action>


    Tuur *thumbsup*

  • Mmmm, I am afraid that's gonna be difficult. Just tested it and a simple alert does indeed work, but my original script already fails on the second line:

    var mycanvas = $('div canvas:first-child').get(0);
    try{ ...... etc

    (the try{ is not understood)

    I will probably need to rewrite the whole script to 'krpano-speak' ? Does it accept normal Javascript?
    This script is not too difficult (I hope) to rearrange, but I have another one with all kinds of calculations which will be much more difficult if I need to rewrite that...

  • HANG ON! That's perfectly working!

    I only had to remove a couple of 'return;' statements in the script, apparently those are not allowed. And to change 'function' by '<action etc.>' of course.

    I am now going to try my big script with a lot of functions. Will let you know.

    Still would like to know why I cannot encrypt the JS files though, even if it's only with 1 alert it won't work...

    Thanks so far, you're a star!

  • Like I feared, stuck on the functions...

    I can successfully call the following without a problem:

    Code
    <action name="down_img" type="Javascript" autorun="">
    <![CDATA[
    alert('cheers');
    ]]>
    </action>

    from a button with

    Code
    onclick="down_img();"

    I can also successfully pass an argument like so:

    Code
    <action name="down_img" type="Javascript" autorun="">
    <![CDATA[
    alert(args[1]);
    ]]>
    </action>

    with button:

    Code
    onclick="down_img(cheers);"

    BUT how do I pass on an argument/variable from action to action, which all contain JS scripts?

    So with button:

    Code
    onclick="down_img();"


    And action1:

    Code
    <action name="down_img" type="Javascript" autorun="">
    <![CDATA[
    var poptext="cheers";
    popup(poptext);
    ]]>
    </action>

    and action2:

    Code
    <action name="popup" type="Javascript" autorun="">
    <![CDATA[
    alert(args[1]);
    ]]>
    </action>

    I tried all kinds of variations of calling and passing, like:
    krpano.call popup(args[1]);
    call popup(args[1]);
    krpano.call actions.popup(args[1]);
    js(popup(args[1]));
    etc.

    also, tried
    %1
    args[1]

    But to no avail. Any clue what to do?

    Edited once, last by LMB (March 19, 2016 at 2:40 PM).

  • Mmmm... I feel rather stupid. Or perhaps not, but after dreaming about functions, I think I realise now how to do it. At least, a small test confirmed my idea *smile*

    I don't need to put all original functions into actions of course. My main action just needs to include all those functions, in plain JS code.

    So, in my example, this works:

    button remains:

    Code
    onclick="down_img();"

    1 action in its shortest form, just to prove a point:

    Code
    <action name="down_img" type="Javascript" autorun="">
    <![CDATA[
    popup('cheers');
    
    
    function popup(txt){
    alert(txt);
    }
    ]]>
    </action>

    I am happy again!

    Of course, still would like to know 2 things, I hope Klaus can join in:
    * why can I not encrypt JS files?
    * how to pass on /arguments/variables from (JS-)action to (JS-)action?

    Thanks again Tuur for keeping on the right track!

  • Pfff... Spoke too soon... *sad*

    I still have a problem, and as far as I can tell, it's simply my lack of knowledge of the internal calls.

    What I told before works, but only for a button inside the pano.
    I actually have a button in the HTML page. Before I started fumbling around, I had the JS code inside the HTML working fine. Thát code I want to encrypt.
    So at first I thought to put the code in a JS file and encrypt that. If I could that working, everything would be so easy. But I don't know how.
    So, I put that code the JS-code inside an XML as 'type=javascript' and as long as I call that from a button within the pano, I am OK.

    But how do I call that function from this button in the HTML? I tried simply calling the action, but since it is JS code it won't take it. Other things I tried:
    js(actionfunction);
    jscall(actionfunction);
    krpano.call actionfunction();
    etc.

    What would be the syntax, please?

    To reiterate what I have:
    1.button in HTML:

    Code
    <span id="Button" class="myButton" onclick="actionFunction();">buttontest</span>

    2. XML:

    Code
    <action name="actionFunction" type="Javascript" autorun="">
    <![CDATA[
    alert('hello');	
    ]]>
    </action>
  • Hi,

    only krpano files (e.g. xml files or krpano html5 .js plugins) could be encrypted - these files will be loaded and decrypted by the krpano viewer and run inside krpano.

    Any external .js files on .html webpage itself are outside of krpano and can't be encrypted - the browser alone wouldn't know that to do with such files.

    If you want to call krpano actions from html, the JS-Interface need to be used:
    https://krpano.com/docu/js/#top

    e.g.
    in the xml:

    Code
    <action name="do_something">
    ...
    </action>

    in the html:

    Code
    <span ... onclick="document.getElementById("krpanoSWFObject").call('do_something()');">...</span>


    Or if you just want to project/encrypt your Javascript functions and then use them normally in the .html:

    1. make a Javascript action inside the xml
    2. set autorun="preinit"
    3. add all your functions to the 'window' object

    E.g. in the (encrypted) xml:

    and in the html (but only after krpano is loaded):

    Code
    <span ... onclick="do_something();">...</span>

    Best regards,
    Klaus

  • thanks Klaus!
    It all makes sense.

    I was halfway there, but still a bit guessing and stuck. Thank you so much!
    And good to know I should not look further into encrypting external JS.

    Since I am programming for HTML5 only, is there a simple example (other than that of the plugins template) of the JS plugin? And what makes it that a JS plugin can be encrypted, but an external JS not?

    Edited 2 times, last by LMB (March 21, 2016 at 9:12 PM).

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!