scrollToBottom option

This commit is contained in:
2020-01-11 20:00:52 +08:00
parent 0cf04c3f79
commit 341abebc66
5 changed files with 150 additions and 69 deletions

View File

@ -1,7 +1,8 @@
class Extractor {
constructor() {
constructor(options) {
this._tasks = [];
this._running = false;
this._options = options;
}
/**
* Add a task to Extractor. \n
@ -10,7 +11,7 @@ class Extractor {
* @param {...any} args itemsSelector, fieldSelectors, and more args to specify target urls.
*/
task(...args) {
this._tasks.push(new Task(...args));
this._tasks.push(new Task(this._options, ...args));
return this;
}
/**
@ -32,7 +33,7 @@ class Extractor {
*/
async restart(from = 0) {
let id = this._checkTaskId(from, 0);
if (!id) return;
if (id < 0) return;
for (let i = id; i < this._tasks.length; i++) {
this._tasks[i].clean();
}
@ -68,10 +69,13 @@ class Extractor {
return task.execute(tab, undefined);
});
}, Promise.resolve(undefined)).then(
() => this.save()
() => {
this._running = false;
this.save();
}
).catch(err => {
this._running = false;
console.log(err)
console.log(err);
});
}
/**
@ -80,31 +84,34 @@ class Extractor {
*/
save(taskid) {
let id = this._checkTaskId(taskid, this._tasks.length - 1);
if (!id) return;
let result = new ExtractResult(this._tasks[id].results);
if (id < 0) return;
let results = this._tasks[id].results
results.unshift(this._tasks[id].fieldSelectors);
if (!result.data.length) {
let exResults = new ExtractResult(results);
if (!results.length) {
console.log(`No result for task #${id}. Forget to call ".start()"?`);
return;
}
let msg = `
Please confirm to download (${result.data.length - 1} items)
Please confirm to download (${results.length - 1} items)
${result.toString(50) || "- Empty -"}
${exResults.toString(50) || "- Empty -"}
`.trim();
if (confirm(msg)) {
saveFile(result, "text/csv");
saveFile(exResults, "text/csv");
}
}
_checkTaskId(id, defaultId) {
if (!this._tasks.length) {
console.log("No task found.");
return 0;
return -1;
}
if (defaultId && id === undefined) id = defaultId;
if (!isNaN(defaultId) && id === undefined) id = defaultId;
if (isNaN(id) || id < 0 || id >= this._tasks.length) {
console.log(`Invalid task id. Rang(0-${this._tasks.length - 1})`);
return 0;
return -1;
}
return id
}