Do javascript function in browser widget?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Do javascript function in browser widget?

Geoff Canyon via use-livecode
Folks,
I’ve found a few examples of simple inline script injection to the browser widget, such as do "document.body.style.background = ‘red';" in widget "Browser”

Is it possible to inject a multi-line javascript function - with variables definition, loops, etc - in this fashion or would the function need to be injected into the page within the browser as markup and then called from a one-line script from LiveCode?

Thanks & regards,
Keith
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Do javascript function in browser widget?

Geoff Canyon via use-livecode
Keith,

If you're in control of the HTML used in the widget, then I'd advise you
to build functions inside the HTML and just call them from LC instead of
executing script directly, such as:

---- in the html ----

<html>

  <head>

   ...

    <script>

    function paintItRed() {

      document.body.style.backgroundColor = "red";

    }

    </script>

  </head>

  <body>

  ...

  </body>

</html>

-------------------


Then in the LC part you can do something like:


   do "paintItRed()" in widget "Browser"


Which is more readable. Also, the body of the function inside the HTML
can be as complex as you want. Now, if you're not in control of the
HTML, I'd opt for some out-of-the-box strategy where you have the LC
HTTPD library serving the JS content and then use the LC "do .. in
widget" form to add a new <script> tag with the "src" attribute pointing
to the HTTPD library server, the script would load and execute in the
context of the loaded page. This should only be done after the page is
loaded though or your script risks executing before the HTML is complete.

Be aware that you can use the "do in widget" form with complex scripts,
but I'd rather use the form above which keeps the JS and LC separate
which is more of a personal preference than a technical reason to be honest.

om om

andre

On 11/6/2018 9:10 AM, Keith Clarke via use-livecode wrote:

> Folks,
> I’ve found a few examples of simple inline script injection to the browser widget, such as do "document.body.style.background = ‘red';" in widget "Browser”
>
> Is it possible to inject a multi-line javascript function - with variables definition, loops, etc - in this fashion or would the function need to be injected into the page within the browser as markup and then called from a one-line script from LiveCode?
>
> Thanks & regards,
> Keith
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Do javascript function in browser widget?

Geoff Canyon via use-livecode
In reply to this post by Geoff Canyon via use-livecode
Browser widget usage examples:
http://forums.livecode.com/viewtopic.php?f=93&t=29018

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Do javascript function in browser widget?

Geoff Canyon via use-livecode
In reply to this post by Geoff Canyon via use-livecode
Thanks Andre for the detailed response and HTTPD library steer - that’s new to me so I shall review with interest. :-)

I’m not in control of the pages, so that may be more useful than my first experiments - which were to download the page content into LC, add some markup and load the HTML text into the browser widget as a local ‘offline’ page.
Best,
Keith

> On 6 Nov 2018, at 10:38, Andre Alves Garzia via use-livecode <[hidden email]> wrote:
>
> Keith,
>
> If you're in control of the HTML used in the widget, then I'd advise you to build functions inside the HTML and just call them from LC instead of executing script directly, such as:
>
> ---- in the html ----
>
> <html>
>
>  <head>
>
>   ...
>
>    <script>
>
>    function paintItRed() {
>
>      document.body.style.backgroundColor = "red";
>
>    }
>
>    </script>
>
>  </head>
>
>  <body>
>
>  ...
>
>  </body>
>
> </html>
>
> -------------------
>
>
> Then in the LC part you can do something like:
>
>
>   do "paintItRed()" in widget "Browser"
>
>
> Which is more readable. Also, the body of the function inside the HTML can be as complex as you want. Now, if you're not in control of the HTML, I'd opt for some out-of-the-box strategy where you have the LC HTTPD library serving the JS content and then use the LC "do .. in widget" form to add a new <script> tag with the "src" attribute pointing to the HTTPD library server, the script would load and execute in the context of the loaded page. This should only be done after the page is loaded though or your script risks executing before the HTML is complete.
>
> Be aware that you can use the "do in widget" form with complex scripts, but I'd rather use the form above which keeps the JS and LC separate which is more of a personal preference than a technical reason to be honest.
>
> om om
>
> andre
>
> On 11/6/2018 9:10 AM, Keith Clarke via use-livecode wrote:
>> Folks,
>> I’ve found a few examples of simple inline script injection to the browser widget, such as do "document.body.style.background = ‘red';" in widget "Browser”
>>
>> Is it possible to inject a multi-line javascript function - with variables definition, loops, etc - in this fashion or would the function need to be injected into the page within the browser as markup and then called from a one-line script from LiveCode?
>>
>> Thanks & regards,
>> Keith
>> _______________________________________________
>> use-livecode mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Do javascript function in browser widget?

Geoff Canyon via use-livecode
In reply to this post by Geoff Canyon via use-livecode
Thanks for the link, Herman - some interesting use cases there …once I’ve got the connectivity in place. :-)
Best,
Keith

> On 6 Nov 2018, at 10:42, hh via use-livecode <[hidden email]> wrote:
>
> Browser widget usage examples:
> http://forums.livecode.com/viewtopic.php?f=93&t=29018
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Do javascript function in browser widget?

Geoff Canyon via use-livecode
In reply to this post by Geoff Canyon via use-livecode
Most examples of that link assume that you have control of the pages.
And most use the js of the browser widget as "helper" only, the widget doesn't display
anything (is hidden).
Now you wrote that you don't have control.

So your "first experiments" method is one way to go, then you have full control.

My experiences are that there are no general rules how to do that. The best method
depends on the current content with all its variabilities. Andre gave some appropriate
and useful options.
You can also remove all css and js and set the htmltext of the widget to a string
with your own (inline) css and/or (inline) js, with or without any "do in widget".
** Merge() is the tool to use your templates. **

> Keith C. wrote:
> Thanks for the link, Herman - some interesting use cases there …once I’ve got the
> connectivity in place. :-)
> --
> Thanks Andre for the detailed response and HTTPD library steer - that’s new to me so
> I shall review with interest. :-)
> I’m not in control of the pages, so that may be more useful than my first experiments
> - which were to download the page content into LC, add some markup and load the HTML
> text into the browser widget as a local ‘offline’ page.
>
>> hh wrote:
>> Browser widget usage examples:
>> http://forums.livecode.com/viewtopic.php?f=93&t=29018
>>
>> Andre G. wrote:
>> Then in the LC part you can do something like:
>>    do "paintItRed()" in widget "Browser"
>> Which is more readable. Also, the body of the function inside the HTML
>> can be as complex as you want. Now, if you're not in control of the
>> HTML, I'd opt for some out-of-the-box strategy where you have the LC
>> HTTPD library serving the JS content and then use the LC "do .. in
>> widget" form to add a new <script> tag with the "src" attribute pointing
>> to the HTTPD library server, the script would load and execute in the
>> context of the loaded page. This should only be done after the page is
>> loaded though or your script risks executing before the HTML is complete.
>>
>> Be aware that you can use the "do in widget" form with complex scripts,
>> but I'd rather use the form above which keeps the JS and LC separate
>> which is more of a personal preference than a technical reason to be honest.
>>
>>> Keith C. wrote:
>>> ... Is it possible to inject a multi-line javascript function - with variables
>>> definition, loops, etc - in this fashion or would the function need to be injected
>>> into the page within the browser as markup and then called from a one-line script
>>> from LiveCode?

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode