Chuck Norris Facts!
#11
LOL! awesome. Full screen and everything even with robot voices!
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#12
(04-21-2023, 08:11 PM)dbox Wrote: Have you ever wished you had a desktop background that would tell you random Chuck Norris facts?  Haven't we all?

Well now your wish has become a reality:
Code: (Select All)
Dim img
img = _LoadImage("https://images01.military.com/sites/default/files/styles/full/public/2021-04/chucknorris.jpeg.jpg?itok=2b4A6n29")
_PutImage , img
_Fullscreen

Do
    _Delay 2

    Dim result As Object
    result = Fetch("https://api.chucknorris.io/jokes/random")

    If result.ok Then
        Dim obj As Object
        obj = JSON.parse(result.text)
        Say obj.value   
    End If
Loop


Sub Say (text As String)
    $If Javascript Then
        var synth = window.speechSynthesis;
        if (synth) {
            var utterance = new SpeechSynthesisUtterance(text);
            synth.speak(utterance);   
            while (synth.speaking) {
                await QB.sub__Delay(.5);
            }
            success = -1;
        }
    $End If
End Sub

View in QBJS

This is really great. Questions:

1. Everything is JS in QBJS - except how you write it using QB syntax or not, still transpiles to JS, right?
2. Fetch! That's awesome, does this have a QB64 counterpart?
3. JSON.parse - native JS inside the QBJS part - it's a little confusing to me - this is javascript inside of QBJS but without $If Javascript like lower down - can you explain how you do that, or how we would know what we can do? If it's in the docs, I'm sorry.

Thanks for sharing this great and simple example.
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#13
(04-27-2023, 03:17 AM)grymmjack Wrote: 2. Fetch! That's awesome, does this have a QB64 counterpart?

https://qb64phoenix.com/qb64wiki/index.p...ding_Files

The first example might be enough to get a text file.
Reply
#14
(04-27-2023, 03:17 AM)grymmjack Wrote: This is really great. Questions:

1. Everything is JS in QBJS - except how you write it using QB syntax or not, still transpiles to JS, right?

Yes, in much the same way that C/C++ is the target of the QB64 compiler, Javascript is the target of the QBJS compiler.  For the browser the generated JS is effectively the executable.  When you click Run in the QBJS IDE it first compiles the QBJS source to Javascript and then runs the generated code.

Quote:2. Fetch! That's awesome, does this have a QB64 counterpart?

@mnrvovrfc pointed you in the right direction on this one.  There is not a single method analog in QB64, but you can use the HTTP methods to do the same thing.

Quote:3. JSON.parse - native JS inside the QBJS part - it's a little confusing to me - this is javascript inside of QBJS but without $If Javascript like lower down - can you explain how you do that, or how we would know what we can do? If it's in the docs, I'm sorry.

I'm afraid for this example I used a couple of undocumented features: the Fetch method and the JSON library, so I understand why it is confusing.  The reason I haven't documented these yet is because I am still evaluating their current implementation and they are likely still subject to change.  (Chuck Norris is aware of all undocumented features.)

The Fetch method in particular, while very convenient for downloading text or JSON, does not really have provision for downloading binary content.  Also, since adding this method early on in development I have refined the naming conventions that I'm using in QBJS to avoid future naming conflicts.  In general, I'm taking this approach:

* Undecorated keywords are generally reserved for QBasic methods.
* Methods prefixed with an underscore (_) are reserved for QB64 methods.
* New QBJS methods will be provided in libraries prefixed with the library name (e.g. Dom.Add, JSON.Parse, Storage.Get)

The exception to this rule is for the Import and Export keywords in QBJS that allow you to include this extended library functionality in your QBJS program.  There is more detail on the subject here for anyone interested.

The JSON functionality needs to be documented.  The reason it has not been to this point is because it currently is "built-in" and does not require an Import statement to include in your QBJS program which is also inconsistent with the rules I outlined above.

Thanks for the feedback and questions!
Reply
#15
If the text file to retrieve is a JSON and it's a certain format, then something rather simple to extract the "facts" could be employed:

https://staging.qb64phoenix.com/showthre...4#pid11254

Otherwise for a JSON there is this to consider:

https://staging.qb64phoenix.com/showthre...8#pid14448
Reply
#16
(04-27-2023, 11:45 AM)dbox Wrote:
Quote:2. Fetch! That's awesome, does this have a QB64 counterpart?

@mnrvovrfc pointed you in the right direction on this one.  There is not a single method analog in QB64, but you can use the HTTP methods to do the same thing.

Quote:3. JSON.parse - native JS inside the QBJS part - it's a little confusing to me - this is javascript inside of QBJS but without $If Javascript like lower down - can you explain how you do that, or how we would know what we can do? If it's in the docs, I'm sorry.

I'm afraid for this example I used a couple of undocumented features: the Fetch method and the JSON library, so I understand why it is confusing.  The reason I haven't documented these yet is because I am still evaluating their current implementation and they are likely still subject to change.  (Chuck Norris is aware of all undocumented features.)

The Fetch method in particular, while very convenient for downloading text or JSON, does not really have provision for downloading binary content.  Also, since adding this method early on in development I have refined the naming conventions that I'm using in QBJS to avoid future naming conflicts.  In general, I'm taking this approach:

* Undecorated keywords are generally reserved for QBasic methods.
* Methods prefixed with an underscore (_) are reserved for QB64 methods.
* New QBJS methods will be provided in libraries prefixed with the library name (e.g. Dom.Add, JSON.Parse, Storage.Get)

The exception to this rule is for the Import and Export keywords in QBJS that allow you to include this extended library functionality in your QBJS program.  There is more detail on the subject here for anyone interested.

The JSON functionality needs to be documented.  The reason it has not been to this point is because it currently is "built-in" and does not require an Import statement to include in your QBJS program which is also inconsistent with the rules I outlined above.

Thanks for the feedback and questions!

Thanks @mnrvovrfc I'll check that link.

OK got it.

Also LOL on Chuck Norris being aware of the undocumented features.

Thanks @dbox for the explanation.
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply




Users browsing this thread: 9 Guest(s)