Commit 1054bc75 authored by edwin's avatar edwin

refactoring, disable btn when processing

parent db752e45
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
// adding the lightbox (control panel) to the body // adding the lightbox (control panel) to the body
// by grabbing the lightbox.html // by grabbing the lightbox.html
function addLightBox() { async function addLightBox() {
if (document.URL === baseurl) { if (document.URL === baseurl) {
fetch(chrome.extension.getURL("fetch/lightbox.html")) try{
.then(response => response.text())
.then(result => document.body.insertAdjacentHTML('beforeend', result)) let response = await fetch(chrome.extension.getURL("fetch/lightbox.html"));
.then(() => { let result = await response.text();
document.body.insertAdjacentHTML('beforeend', result);
// turn on/ off lightbox // turn on/ off lightbox
document.querySelector("#lightbox-switch").addEventListener("click", function () { document.querySelector("#lightbox-switch").addEventListener("click", function () {
document.getElementById("fetch-lightbox").classList.add("on"); document.getElementById("fetch-lightbox").classList.add("on");
...@@ -36,8 +38,9 @@ ...@@ -36,8 +38,9 @@
} }
}); });
}) }catch(error){
.catch(error => console.error("Unable to fetch lightbox.html" + error)); console.error("Unable to fetch lightbox.html" + error);
}
} }
} }
...@@ -74,6 +77,7 @@ ...@@ -74,6 +77,7 @@
// Reading the textarea and fetch the SKUs one by one // Reading the textarea and fetch the SKUs one by one
// then create DOM and append it // then create DOM and append it
async function startFetching() { async function startFetching() {
document.querySelectorAll("#fetch-lightbox button").forEach(x => x.disabled = true);
let SKUString = document.getElementById("sku-list").value; let SKUString = document.getElementById("sku-list").value;
if (SKUString.length) { if (SKUString.length) {
let SKUlist = SKUString.split("\n"); let SKUlist = SKUString.split("\n");
...@@ -87,6 +91,7 @@ ...@@ -87,6 +91,7 @@
document.querySelector("#fetch-lightbox .info-message").innerHTML = ""; document.querySelector("#fetch-lightbox .info-message").innerHTML = "";
for (let SKU of SKUlist) { for (let SKU of SKUlist) {
// fetch product URL // fetch product URL
let currentms = new Date().getTime(); let currentms = new Date().getTime();
let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`, `${SKU} product image`); let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`, `${SKU} product image`);
...@@ -94,7 +99,6 @@ ...@@ -94,7 +99,6 @@
container.insertAdjacentHTML('beforeend', DOMString); container.insertAdjacentHTML('beforeend', DOMString);
clientSideFilter(); clientSideFilter();
// fetch model URL // fetch model URL
currentms = new Date().getTime(); currentms = new Date().getTime();
assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=datacategory%2Fasset&w=${SKU}&r=north&z=${fetchNumber}&c=DEFAULT`, `${SKU} model image`); assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=datacategory%2Fasset&w=${SKU}&r=north&z=${fetchNumber}&c=DEFAULT`, `${SKU} model image`);
...@@ -107,10 +111,12 @@ ...@@ -107,10 +111,12 @@
} else { } else {
alert("It is empty on SKU list."); alert("It is empty on SKU list.");
} }
document.querySelectorAll("#fetch-lightbox button").forEach(x => x.disabled = false);
} }
// this function base on startFetching, will be used after user selected all assets // this function base on startFetching, will be used after user selected all assets
async function downloadFetching() { async function downloadFetching() {
document.querySelectorAll("#fetch-lightbox button").forEach(x => x.disabled = true);
// grab selected DOM, and turn from NodeList to Array // grab selected DOM, and turn from NodeList to Array
let DOMArray = [...document.querySelectorAll("#fetch-lightbox .debug-box .asset-item.on")]; let DOMArray = [...document.querySelectorAll("#fetch-lightbox .debug-box .asset-item.on")];
...@@ -131,6 +137,7 @@ ...@@ -131,6 +137,7 @@
// remove the clicked state on asset item // remove the clicked state on asset item
document.querySelectorAll("#fetch-lightbox .debug-box .asset-item.on").forEach(x => x.classList.remove("on")); document.querySelectorAll("#fetch-lightbox .debug-box .asset-item.on").forEach(x => x.classList.remove("on"));
document.querySelectorAll("#fetch-lightbox button").forEach(x => x.disabled = false);
} }
function createDOMString(SKU, assetlist){ function createDOMString(SKU, assetlist){
...@@ -178,6 +185,7 @@ ...@@ -178,6 +185,7 @@
// perform operation similar to old extension // perform operation similar to old extension
async function extensionDownloading() { async function extensionDownloading() {
document.querySelectorAll("#fetch-lightbox button").forEach(x => x.disabled = true);
let SKUString = document.getElementById("sku-list").value; let SKUString = document.getElementById("sku-list").value;
if (SKUString.length) { if (SKUString.length) {
let SKUlist = SKUString.split("\n"); let SKUlist = SKUString.split("\n");
...@@ -191,17 +199,15 @@ ...@@ -191,17 +199,15 @@
for (let SKU of SKUlist) { for (let SKU of SKUlist) {
// first link
let currentms = new Date().getTime(); let currentms = new Date().getTime();
// old
// let assetlist = await fetchSKU(SKU, `${baseurl}api/asset?time=${currentms}&t=datacategory%2Fasset&w=${SKU}&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`);
// new
let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`, `${SKU} product image`); let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`, `${SKU} product image`);
await downloadList(assetlist, `${SKU}`); await downloadList(assetlist, `${SKU}`);
await sleep(sleepTime); await sleep(sleepTime);
let is_old = assetlist.length === 0; let is_old = assetlist.length === 0;
// &t=datacategory%2Fasset&w=341947&r=north&z=30&c=DEFAULT // second link
currentms = new Date().getTime(); currentms = new Date().getTime();
assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=datacategory%2Fasset&w=${SKU}&r=north&z=${fetchNumber}&c=DEFAULT`, `${SKU} model image`); assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=datacategory%2Fasset&w=${SKU}&r=north&z=${fetchNumber}&c=DEFAULT`, `${SKU} model image`);
let downloadlist = filtering(assetlist, SKU); let downloadlist = filtering(assetlist, SKU);
...@@ -218,6 +224,7 @@ ...@@ -218,6 +224,7 @@
} else { } else {
alert("It is empty on SKU list."); alert("It is empty on SKU list.");
} }
document.querySelectorAll("#fetch-lightbox button").forEach(x => x.disabled = false);
} }
//grabbing single SKU data //grabbing single SKU data
...@@ -226,43 +233,38 @@ ...@@ -226,43 +233,38 @@
return; return;
} }
let assetlist = [];
let config = getConfig(); let config = getConfig();
addDebugMessage(`Start fetching SKU=${message}`); addDebugMessage(`Start fetching SKU=${message}`);
return await fetch(url, config) try{
.then(response => response.json()) let request = await fetch(url, config);
.then(async function (result) { let response = await request.json();
await sleep(sleepTime); await sleep(sleepTime);
//get json data assetlist = response.assets;
let assetlist = result.assets; let scrollId = response.scrollId;
let scrollId = result.scrollId; let total = response.total;
let total = result.total;
// fetch the remaining item by simluating scroll operation // fetch the remaining item by simluating scroll operation
for (let i = total - fetchNumber; i > 0; i -= fetchNumber) { for (let i = total - fetchNumber; i > 0; i -= fetchNumber) {
addDebugMessage(`fetching SKU=${message} Process: ${i} remains`); addDebugMessage(`fetching SKU=${message} Process: ${i} remains`);
let response = await fetch(`${baseurl}api/asset/more?scrollId=${scrollId}`, config) let req = await fetch(`${baseurl}api/asset/more?scrollId=${scrollId}`, config);
.then(response => response.json()) let res = await req.json();
.catch(error => { assetlist = assetlist.concat(res.assets);
addDebugMessage("Fetch error on fetchSKU - Scroll : " + error);
throw new Error("Fetch error on fetchSKU - Scroll : " + error);
});
assetlist = assetlist.concat(response.assets);
await sleep(sleepTime / 1.5); await sleep(sleepTime / 1.5);
} }
addDebugMessage(`fetching SKU=${message} completed, total item: ${total}`); addDebugMessage(`fetching SKU=${message} completed, total item: ${total}`);
return assetlist; }catch(error){
})
.catch(error => {
addDebugMessage("Fetch error on fetchSKU : " + error); addDebugMessage("Fetch error on fetchSKU : " + error);
throw new Error("Fetch error on fetchSKU : " + error); throw new Error("Fetch error on fetchSKU : " + error);
}); }
return assetlist;
} }
// filter asset by Date // filter asset by Date
...@@ -344,24 +346,29 @@ ...@@ -344,24 +346,29 @@
postheader.append("csrf-token", document.querySelector('meta[name="csrf-token"]').content); postheader.append("csrf-token", document.querySelector('meta[name="csrf-token"]').content);
postheader.append("origin", "https://digitallibrary-external.fastretailing.com"); postheader.append("origin", "https://digitallibrary-external.fastretailing.com");
let response;
// send POST request for getting download token // send POST request for getting download token
let response = await fetch(`${baseurl}api/asset/downloadtoken`, { try{
let request = await fetch(`${baseurl}api/asset/downloadtoken`, {
"method": "POST", "method": "POST",
"headers": postheader, "headers": postheader,
"mode": "cors", "mode": "cors",
"redirect": "follow", "redirect": "follow",
"body": json "body": json
}).then(response => response.json()) });
.catch(error => { response = await request.json();
}catch(error){
addDebugMessage("Fetch error on DownloadList - POST request: " + error); addDebugMessage("Fetch error on DownloadList - POST request: " + error);
throw new Error("Fetch error on DownloadList - POST request: " + error); throw new Error("Fetch error on DownloadList - POST request: " + error);
}); }
// send GET request for downloading the zip file // send GET request for downloading the zip file
fetch(`/api/asset/download/${response.token}`, getConfig()) if(response){
.then(response => response.blob()) try{
.then(blob => { let request = await fetch(`/api/asset/download/${response.token}`, getConfig());
let blob = await request.blob();
// https://stackoverflow.com/questions/32545632/how-can-i-download-a-file-using-window-fetch // https://stackoverflow.com/questions/32545632/how-can-i-download-a-file-using-window-fetch
let url = window.URL.createObjectURL(blob); let url = window.URL.createObjectURL(blob);
...@@ -373,11 +380,12 @@ ...@@ -373,11 +380,12 @@
a.remove(); //afterwards we remove the element again a.remove(); //afterwards we remove the element again
addDebugMessage(`download ${name}.zip completed`); addDebugMessage(`download ${name}.zip completed`);
})
.catch(error => { }catch(error){
addDebugMessage("Fetch error on DownloadList - GET request: " + error); addDebugMessage("Fetch error on DownloadList - GET request: " + error);
throw new Error("Fetch error on DownloadList - GET request: " + error); throw new Error("Fetch error on DownloadList - GET request: " + error);
}); }
}
} else if (assetlist.length === 1) { } else if (assetlist.length === 1) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment