Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
need a little help with a javascript
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 75

PostPosted: Thu Nov 23, 2023 5:51 pm    Post subject: need a little help with a javascript Reply with quote

Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Activation Countdown</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
        }

        #currentActivation {
            font-size: 18px;
        }

        #countdown {
            font-size: 24px;
        }

        #nextActivation {
            font-size: 18px;
        }
    </style>
</head>
<body>

<div id="currentActivation"></div>
<div id="countdown"></div>
<div id="nextActivation"></div>

<script>
    // Fetch activation times from the text file
    async function fetchActivationTimes() {
        try {
            const response = await fetch('lunarCountdown.txt');
            const text = await response.text();
            const gateTimes = eval(`(${text})`);
            console.log("gateTimes:", gateTimes);

            // Continue with the rest of your code
            // ...
        } catch (error) {
            console.error("Error fetching activation times:", error);
        }
    }

    // Call the fetchActivationTimes function
    fetchActivationTimes();
</script>

</body>
</html>


this is a small sample from the text file
Code:
const gate21Times = {
    line_1: "2023-12-20 15:07:12+0000",
    line_2: "2023-12-20 16:44:23+0000",
    line_3: "2023-12-20 18:21:39+0000",
    line_4: "2023-12-20 19:58:59+0000",
    line_5: "2023-12-20 21:36:25+0000",
    line_6: "2023-11-23 17:26:23+0000",
};
const gate51Times = {
    line_1: "2023-11-23 19:02:13+0000",
    line_2: "2023-11-23 20:38:06+0000",
    line_3: "2023-11-23 22:14:01+0000",
    line_4: "2023-11-23 23:49:58+0000",
    line_5: "2023-11-24 01:25:57+0000",
    line_6: "2023-11-24 03:01:59+0000",
};
const gate42Times = {
    line_1: "2023-11-24 04:38:04+0000",
    line_2: "2023-11-24 06:14:11+0000",
    line_3: "2023-11-24 07:50:20+0000",
    line_4: "2023-11-24 09:26:33+0000",
    line_5: "2023-11-24 11:02:48+0000",
    line_6: "2023-11-24 12:39:07+0000",
};
the page comes empty :( i tried and tried btw .. thank you in advance
Back to top
View user's profile Send private message
Banana
Moderator
Moderator


Joined: 21 May 2004
Posts: 1478
Location: Germany

PostPosted: Thu Nov 23, 2023 7:30 pm    Post subject: Reply with quote

How do you run this?
If you only do "open file" on your browser without a webserver you will end up with this: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSRequestNotHttp
There is an error in the browser console

If you run it (whcih you should) on a webserver, then there is still an error in the browser console
Error fetching activation times: SyntaxError: expected expression, got keyword 'const'

I hope you know what you are doing since using eval() is bad. Really really use it with code and data your can control.

EDIT:
Dusted off my javascript skills and: The code you want to eval() does not contain anything to be evalued.
If you want to load of some vars and include them you need to do something like this:

Your lunarCountdown.txt file
Code:
const gate21Times = {
    line_1: "2023-12-20 15:07:12+0000",
    line_2: "2023-12-20 16:44:23+0000",
    line_3: "2023-12-20 18:21:39+0000",
    line_4: "2023-12-20 19:58:59+0000",
    line_5: "2023-12-20 21:36:25+0000",
    line_6: "2023-11-23 17:26:23+0000",
};
const gate51Times = {
    line_1: "2023-11-23 19:02:13+0000",
    line_2: "2023-11-23 20:38:06+0000",
    line_3: "2023-11-23 22:14:01+0000",
    line_4: "2023-11-23 23:49:58+0000",
    line_5: "2023-11-24 01:25:57+0000",
    line_6: "2023-11-24 03:01:59+0000",
};
const gate42Times = {
    line_1: "2023-11-24 04:38:04+0000",
    line_2: "2023-11-24 06:14:11+0000",
    line_3: "2023-11-24 07:50:20+0000",
    line_4: "2023-11-24 09:26:33+0000",
    line_5: "2023-11-24 11:02:48+0000",
    line_6: "2023-11-24 12:39:07+0000",
};
export {gate21Times, gate51Times, gate42Times};


Your js code in the html file:
Code:
.....
const response = await fetch('lunarCountdown.txt');
const text = await response.text();
let data = await import(`data:text/javascript,${text}`);

// now you can access it
alert(data.gate21Times.line_1);
alert(data.gate51Times.line_4);

_________________
My personal space
My delta-labs.org snippets do expire

PFL - Portage file list - find which package a file or command belongs to.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 21918

PostPosted: Thu Nov 23, 2023 9:58 pm    Post subject: Reply with quote

Using eval is almost always wrong. For this use case, you should return your data as a JSON dictionary, then use JSON.parse to convert it into a data structure that the script in the page can use.

However, I will also state that for static data like this, it should just be in the page, and save the browser the extra network trip of reading it. Fetching data asynchronously only makes sense when the author of the page cannot predict exactly what data will be returned.
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 75

PostPosted: Fri Nov 24, 2023 1:18 am    Post subject: Reply with quote

Thank you guys, I will read that several times and run this tomorrow as I am a little tipsy right now .. Forgot to say that if i enter the data in the script itself it works but it's a little tedious as i have to keep entering the info. Yes I run that from the terminal "firefox index.html" . I only used const because of the way it run inside the script, but i can always use it like this which is much simpler
Code:
Gate 61:
  line_1: 2023-12-15 02:29:51+00:00
  line_2: 2023-12-15 04:03:53+00:00
  line_3: 2023-12-15 05:37:51+00:00
  line_4: 2023-12-15 07:11:47+00:00
  line_5: 2023-12-15 08:45:41+00:00
  line_6: 2023-12-15 10:19:33+00:00

Any criticism or new way of doing this is more than welcome 8O
p.s. I am new to coding and know a little more python and know nothing of javascript 8O
Back to top
View user's profile Send private message
Banana
Moderator
Moderator


Joined: 21 May 2004
Posts: 1478
Location: Germany

PostPosted: Fri Nov 24, 2023 6:10 am    Post subject: Reply with quote

At first, as always: https://en.wikipedia.org/wiki/KISS_principle

A start about JS could be https://www.w3schools.com/js/ and https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps

I would suggest to avoid any frameworks for now and get the hang of it. Then move on and do not re-event the wheel.

Also make sure you have a webserver at hand, like apache or nginx, since just opening the file in firefox does not work like any other web application you find on the web.
_________________
My personal space
My delta-labs.org snippets do expire

PFL - Portage file list - find which package a file or command belongs to.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum