helper function $
This commit is contained in:
@ -15,7 +15,6 @@ class Extractor {
|
||||
task(...args) {
|
||||
if (!testArgs(...args)) {
|
||||
console.log(`Invalid task arguments: ${argsToString(...args)}\n\n${signitures}\n`);
|
||||
// break call chain to avoid unexpected task running
|
||||
return this;
|
||||
}
|
||||
// given >2 arguments means the task specifies target page,
|
||||
@ -31,6 +30,7 @@ class Extractor {
|
||||
clear() {
|
||||
this._tasks = [];
|
||||
this._results = [];
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Start the task chain.
|
||||
@ -64,6 +64,7 @@ class Extractor {
|
||||
result => {
|
||||
this._results[this._tasks[this._tasks.length - 1]] = result;
|
||||
this._running = false;
|
||||
console.log("Tasks are all done.")
|
||||
this.save();
|
||||
}
|
||||
).catch(err => {
|
||||
@ -122,9 +123,16 @@ class Extractor {
|
||||
console.log(`No result for task #${taskid}. Forget to call ".start()"?`);
|
||||
return;
|
||||
}
|
||||
if (confirm(
|
||||
`Click confirm to download if the sample data looks good (${result.data.length} items):\n\n${result.toString(50) || "- Empty -"}`
|
||||
)) {
|
||||
if (result.data.length <= 1) { // 1 for selector headers
|
||||
console.log(`No result for task #${taskid}. Forget to call ".start()"?`);
|
||||
return;
|
||||
}
|
||||
let msg = `
|
||||
Please confirm to download (${result.data.length - 1} items):
|
||||
|
||||
${result.toString(50) || "- Empty -"}
|
||||
`.trim();
|
||||
if (confirm(msg)) {
|
||||
saveFile(result, "text/csv");
|
||||
}
|
||||
}
|
||||
|
||||
3
scripts/background/helpers.js
Normal file
3
scripts/background/helpers.js
Normal file
@ -0,0 +1,3 @@
|
||||
function $(...args) {
|
||||
return new Extractor().task(...args).start();
|
||||
}
|
||||
@ -1,17 +1,21 @@
|
||||
const signitures = `
|
||||
## Usage
|
||||
new Extractor().task(...args).task(...args).start();
|
||||
// single task
|
||||
$(...args);
|
||||
// managed task chains
|
||||
e = new Extractor();
|
||||
e.task(...args).task(...args).start();
|
||||
|
||||
## Extractor.task() Signitures:
|
||||
function(itemsSelector:string, fieldSelectors:string[])
|
||||
function(itemsSelector:string, fieldSelectors:string[], url:string, from:number, to:number, interval:number)
|
||||
function(itemsSelector:string, fieldSelectors:string[], url:string, pages:number[])
|
||||
function(itemsSelector:string, fieldSelectors:string[], urls:string[])
|
||||
## Task Call Signitures:
|
||||
function(itemsSelector:string, fieldSelectors:string[]);
|
||||
function(itemsSelector:string, fieldSelectors:string[], url:string, from:number, to:number, interval:number);
|
||||
function(itemsSelector:string, fieldSelectors:string[], url:string, pages:number[]);
|
||||
function(itemsSelector:string, fieldSelectors:string[], urls:string[]);
|
||||
|
||||
## Example:
|
||||
// extract all links text & url under '.item' elements
|
||||
// use 'selector@attr' to get attribute of the field elements
|
||||
new Extractor().task(".item", ["a", "a@href"]).start();
|
||||
$(".item", ["a", "a@href"]);
|
||||
|
||||
## See Detailed Help:
|
||||
https://git.jebbs.co/jebbs/data-extracter-extesion
|
||||
|
||||
Reference in New Issue
Block a user