Share your advanced knowledge/code with the community.
User avatar
Peter
 
Posts: 525
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Tool: HtmlPreprocessor for SpiderBasic

by Peter Thu Aug 23, 2018 9:41 pm

If the prophet does not come to the mountain the mountain must come to the prophet. ;)

Referring to this feature request i wrote a little SpiderBasic-Tool which allow to manipulate the HTML file generated by SpiderBasic before displaying it in the browser.

More informations here: https://github.com/spiderbytes/HtmlPreprocessor

Greetings ... Peter
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Tool: HtmlPreprocessor for SpiderBasic

by SparrowhawkMMU Fri Aug 24, 2018 8:47 am

Thanks Peter, great bit of code. Works great on Mac too.
User avatar
Peter
 
Posts: 525
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: Tool: HtmlPreprocessor for SpiderBasic

by Peter Fri Aug 24, 2018 9:01 am

you're welcome, SparrowhawkMMU. :)

SparrowhawkMMU wrote:Works great on Mac too.

I'm glad to read this. I could only test it under windows and linux so far.

Thanks for your feedback & Greetings ... Peter
HPW
 
Posts: 33
Joined: Thu May 04, 2017 4:25 pm

Re: Tool: HtmlPreprocessor for SpiderBasic

by HPW Fri Aug 24, 2018 5:12 pm

Hello Peter,

Thanks, works fine.
Lets hope Fred comes with a final solution in sb.

Regards
Hans-Peter
User avatar
Peter
 
Posts: 525
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: Tool: HtmlPreprocessor for SpiderBasic

by Peter Sat Aug 25, 2018 11:52 am

Hello Hans-Peter,

thank you for the feedback.

HPW wrote:Lets hope Fred comes with a final solution in sb.

yes, a native solution would be better. But there may still be some time to go until then.

Greetings ... Peter
HPW
 
Posts: 33
Joined: Thu May 04, 2017 4:25 pm

Re: Tool: HtmlPreprocessor for SpiderBasic

by HPW Sat Aug 25, 2018 12:17 pm

Hello Peter,

For a one-liner replacement this works fine.
I now try to insert a larger block of JavaScript. Now I fighting with error popup
json error in htmlpreprocessot: invalid escape character

So how about extending the preprozessor to allow the Parameter "replace": followed by a filename with full path to insert the complete file.

Or where can I have the wrong escape character? Which characters have to be escaped?
This is my JavaScript to insert:
Code: Select all
    <script type='text/javascript'>
<!--
      // ================== careful changing anything below ===================
      // EMSCRIPTEN loading newlisp-js-lib.js showing progress of downloading
      // importing newlispEvaklStr and defining preRun and postRun functions
      var Module = {
        preRun: [],
        postRun: [(function() {
          // import newlispEvalStr from newlisp-js-lib.js library
          newlispEvalStr = Module.cwrap('newlispEvalStr', 'string', ['string']);
          // preload newLISP functions from code textarea id='code'
//          newlispEvalStr(document.getElementById('code').value);
          // preload memoizing table
//          newlispEvalStr('(fibo 150)(fibo 300)(fibo 450)');
        })],
        print: (function() { return function(text) { }; })(),
        printErr: function(text) { },
        setStatus: function(text) {
          if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
          if (text === Module.setStatus.text) return;
          var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
          var now = Date.now();
          if (m && now - Date.now() < 30) return; // if progress update, skip if too soon
          var statusElement = document.getElementById('status');
          var progressElement = document.getElementById('progress');
          if (m) {
            text = m[1];
            progressElement.value = parseInt(m[2])*100;
            progressElement.max = parseInt(m[4])*100;
            progressElement.hidden = false;
          } else {
            progressElement.value = null;
            progressElement.max = null;
            progressElement.hidden = true;
            }
            statusElement.innerHTML = text;
        },
        totalDependencies: 0,
        monitorRunDependencies: function(left) {
          this.totalDependencies = Math.max(this.totalDependencies, left);
          Module.setStatus(left ?'Preparing... (' + (this.totalDependencies-left) +
                        '/' + this.totalDependencies + ')' : 'All downloads complete.');
        }
      };
      Module.setStatus('Downloading...');
    // ========================================================================
-->
    </script>
<script async type="text/javascript" src="js/newlisp-js-lib.js"></script>


Regards
User avatar
Peter
 
Posts: 525
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: Tool: HtmlPreprocessor for SpiderBasic

by Peter Sat Aug 25, 2018 2:40 pm

HPW wrote:Which characters have to be escaped?


in your case: only the double quotes:

Code: Select all
"replace": "<script async type=\"text/javascript\" src=\"js/newlisp-js-lib.js\"></script>"


or replace the double quotes with single quotes:

Code: Select all
"replace": "<script async type='text/javascript' src='js/newlisp-js-lib.js'></script>"


(the rest of the script is commented out anyway)

Greetings ... Peter
HPW
 
Posts: 33
Joined: Thu May 04, 2017 4:25 pm

Re: Tool: HtmlPreprocessor for SpiderBasic

by HPW Sat Aug 25, 2018 3:08 pm

Hello Peter,

Thanks for the hint. Got it working now.
This time I place the code before </body>.
Finally I have the embedded newlisp-interpreter working.
And I learned a lot about mixing sb and JavaScript.

Regards
HPW
 
Posts: 33
Joined: Thu May 04, 2017 4:25 pm

Re: Tool: HtmlPreprocessor for SpiderBasic

by HPW Sun Aug 26, 2018 7:52 am

Hello Peter,

A related question:
I injected some JavaScript calls ito the Body of the spiderapp.
I want to Access the spide-objects from there like:
Code: Select all
            f_disablegadget(3,0);
            f_setgadgettext(5,text);

But Console repors that they are undefined.
And when examie the html I can not see any DOM structure.
So how do I access objects or commands from main-html?

Regards
HPW
 
Posts: 33
Joined: Thu May 04, 2017 4:25 pm

Re: Tool: HtmlPreprocessor for SpiderBasic

by HPW Sun Aug 26, 2018 8:47 am

Hello,
Also this did not work:
Code: Select all
            var statusElement = document.getElementById('widget_dijit_form_Textbox_1');
            statusElement.innerHTML = text;

Regards
Return to Tricks 'n' Tips

Who is online

Users browsing this forum: No registered users and 0 guests