Commit 9a6fd148 authored by edwin's avatar edwin

use chrome local storage store variable to prevent empty sku array => undefined name

parent cce8427c
'use strict'; 'use strict';
var index = 0;
var skus = [];
var noImages = "";
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{ chrome.declarativeContent.onPageChanged.addRules([{
...@@ -13,53 +10,51 @@ chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { ...@@ -13,53 +10,51 @@ chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
}]); }]);
}); });
chrome.extension.onMessage.addListener(function(request,sender,sendResponse){ chrome.extension.onMessage.addListener(function(request,sender,sendResponse){
if(request.idArray) {
// init
index = 0;
noImages = "";
skus = request.idArray.split(','); chrome.storage.local.get(['idArray', 'index'], function (items) {
console.log(skus); let idArray = items.idArray.split(',');
let length = idArray.length;
let index = items.index;
} else if(request.nothing == "true") { if(request.nothing == "true"){
console.log("Message : nothing "); chrome.storage.local.set({index: (index+1)});
index++; index += 1;
} else if(request.noImage == "true") { console.log("Message : nothing ");
console.log("Message : noImages");
noImages += (request.currentSku + ": no results/ no valid images\n"); }else if(request.noImage == "true") {
index += 2; chrome.storage.local.set({index: (index+2)});
} index += 2;
console.log("Current index : ", index); console.log("Message : noImages");
console.log("Current noImages : " , noImages); }
if(index >= skus.length) {
console.log("Last file was downloaded"); if(index >= length) {
alert(noImages); console.log("Last file was downloaded");
chrome.storage.local.remove(['idArray', 'index']);
}
});
index = 0;
noImages = "";
chrome.storage.local.remove('idArray');
}
}); });
chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) { chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
// if(!skus.length){
// chrome.storage.local.get('idArray', function (items) {
// skus = items.idArray.split(',');
// });
// }
if(item.url.includes("https://digitallibrary-external.fastretailing.com/api/asset")){ if(item.url.includes("https://digitallibrary-external.fastretailing.com/api/asset")){
var new_filename = skus[index] + ".zip"; chrome.storage.local.get(['idArray', 'index'], function (items) {
suggest({filename: new_filename}); let index = items.index;
index++; let idArray = items.idArray.split(',');
if(index >= skus.length) { let new_filename = idArray[index] + ".zip";
console.log("Last file was downloaded");
alert(noImages); suggest({filename: new_filename});
chrome.storage.local.set({index: (index+1)});
index = 0; index += 1;
noImages = "";
chrome.storage.local.remove('idArray'); if(index >= idArray.length) {
} console.log("Last file was downloaded");
chrome.storage.local.remove(['idArray', 'index']);
}
return true;
});
return true;
} }
}); });
...@@ -139,7 +139,7 @@ ...@@ -139,7 +139,7 @@
// turn DOM into object array with id and file version // turn DOM into object array with id and file version
let downloadlist = DOMArray.map(x => { let downloadlist = DOMArray.map(x => {
return{ return {
"id" : x.querySelector(".asset-id").innerHTML, "id" : x.querySelector(".asset-id").innerHTML,
"fileVersion" : x.querySelector(".asset-version").innerHTML "fileVersion" : x.querySelector(".asset-version").innerHTML
}; };
......
...@@ -17,10 +17,10 @@ inputButton.onclick = function(element) { ...@@ -17,10 +17,10 @@ inputButton.onclick = function(element) {
console.log("Final text = ", finalText); console.log("Final text = ", finalText);
idArray = finalText.join(','); idArray = finalText.join(',');
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.storage.local.set({idArray : idArray}, function () { chrome.storage.local.set({idArray : idArray, index: 0}, function () {
console.log("set storage"); console.log("set storage");
}); });
chrome.runtime.sendMessage({idArray: idArray}); // chrome.runtime.sendMessage({idArray: idArray});
}); });
} }
......
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