|
|
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?:\\/\\/)?'+ |
|
|
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ |
|
|
'((\\d{1,3}\\.){3}\\d{1,3}))'+ |
|
|
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ |
|
|
'(\\?[;&a-z\\d%_.~+=-]*)?'+ |
|
|
'(\\#[-a-z\\d_]*)?$','i'); |
|
|
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() |
|
|
|