File size: 4,052 Bytes
d47e7bb
 
 
 
 
 
f8d91ab
 
d47e7bb
 
 
f8d91ab
 
 
6950d8a
f8d91ab
 
 
 
 
 
 
 
 
 
 
 
6950d8a
f8d91ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d47e7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8d91ab
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const preview = document.getElementById('preview');
const noImage = document.getElementById('no-image');
const takeBtn = document.getElementById('take-btn');
const linkBtn = document.getElementById('link-btn');
const amazonLink = document.getElementById('amazon_link');
const status = document.getElementById('status');
const apply_token = document.getElementById("apply_token")
const hf_token = document.getElementById("hf_token")

let currentFilename = null;

HF_TOKEN = ""

async function set_token() {
    const token_value = hf_token.value.trim();

    if (!token_value) {
        console.error("Token part is empty.");
        return;
    }

    try {
        const resp = await fetch('/set_token', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ token: token_value })
        });

        const data = await resp.json();

        if (data.success) {
            main_container.style.display = 'block';
            console.log("Token recorded :", data);
        } else {
            console.error("Error :", data.message || data);
        }
    } catch (error) {
        console.error("Network error :", error);
    }
}

async function check_token()
{
    if( HF_TOKEN == "" )
    {
        const resp = await fetch('/check_token', { method: 'POST' });
        const data = await resp.json();

        console.log(data)

        if( data.success )
        {
           main_container.style.display = 'block';
           HF_TOKEN = data.token
           hf_token.value = data.token
           return true
        }
        else
        {
           main_container.style.display = 'none';
           HF_TOKEN = ""
           status.innerHTML = data.message
           hf_token.value = ""
           return false
        }
    }
    else
    {
          return true
    }
}

function validURL(str) {
  var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
    '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
    '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
    '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
    '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
    '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  return !!pattern.test(str);
}

async function getLink() {
    linkBtn.disabled = true;
    linkBtn.textContent = 'Analyzing...';
    status.textContent = '';

    try {
        const resp = await fetch('/get_link', { method: 'POST' });
        const data = await resp.json();

        if (data.success) {
            status.textContent = 'Amazon link generation complete!';
            url = data.url
            if( validURL(url) )
            {
                amazonLink.innerHTML = "<a href=\""+data.url+"\">"+url+"</a>"
                window.open(url, '_blank').focus();
            }
        }
        else
        {
            status.textContent = 'Error: ' + data.error;
        }
    } catch (e) {
        status.textContent = 'Failed to get a link';
        console.error(e);
    }

    linkBtn.disabled = false;
    linkBtn.textContent = 'Amazon this object';
}

async function takePhoto() {
    takeBtn.disabled = true;
    takeBtn.textContent = 'Taking...';
    status.textContent = '';

    try {
        const resp = await fetch('/take_photo', { method: 'POST' });
        const data = await resp.json();

        if (data.success) {
            preview.src = data.preview;
            preview.style.display = 'block';
            noImage.style.display = 'none';
            currentFilename = data.filename;
            status.textContent = 'Capture success!';
        } else {
            status.textContent = 'Error: ' + data.error;
        }
    } catch (e) {
        status.textContent = 'Failed to take photo';
        console.error(e);
    }

    takeBtn.disabled = false;
    takeBtn.textContent = 'Capture object';
}

takeBtn.addEventListener('click', takePhoto);
linkBtn.addEventListener('click', getLink);
apply_token.addEventListener('click', set_token);

check_token()