Extractor.stop()

This commit is contained in:
2020-01-15 14:18:31 +08:00
parent 2224db1ad1
commit da7ae057f4
3 changed files with 40 additions and 6 deletions

View File

@ -13,10 +13,14 @@ All you need to do is:
## Qucik Start
Extract current page
```js
$('.item', ['a', 'a@href']);
new Extractor().task('.item', ['a', 'a@href']).start();
```
> `$(...args)` is the short form of `new Extractor().task(...args).start();`, which is introduced later.
Extract multiple pages (1-10, interval 1)
```js
@ -54,6 +58,15 @@ function (itemsSelector:string, fieldSelectors:string[], urls:ExtractResult)
Close the target tab, in which current tasks is running.
Or use `job.stop()`:
```js
job = new Extractor().task('.search-list-item', ['a@href'], ["http://sample.com/abc"])
.task('list-item', ["a.title", "p.content"])
.start();
job.stop();
```
## Extract Attributes.
e.g.: link text and target (use 'selector@attribute')

View File

@ -51,14 +51,19 @@ export class Extractor {
/**
* Start the task chain.
*/
async start() {
start() {
return this._startTasks(0);
}
stop() {
for (let i = 0; i < this._tasks.length; i++) {
this._tasks[i].stop();
}
}
/**
* restart from specified task, but don't restart the previous tasks.
* @param {number} from where to restart the tasks, begins with 0
*/
async restart(from: number = 0) {
restart(from: number = 0) {
let id = this._checkTaskId(from, 0);
if (id < 0) return;
for (let i = id; i < this._tasks.length; i++) {

View File

@ -10,6 +10,7 @@ export class Task {
private _itemsSelector: string;
private _fieldSelectors: string[];
private _urls: string[] = [];
private _running = false;
constructor(options: any, ...arg: any);
constructor(options: any, itemsSelector: string, fieldSelectors: string[]);
@ -49,8 +50,13 @@ export class Task {
this._data_keys = [];
return this;
}
stop() {
this._running = false;
}
async execute(tab: chrome.tabs.Tab, upstreamData?: ExtractResult): Promise<void> {
if (!tab) return Promise.reject("No tab to execute the task.");
if (this._running) return Promise.reject("The task is running. Please wait...");
this._running = true;
let urls = this._urls
if (!urls.length) {
if (upstreamData) {
@ -63,6 +69,10 @@ export class Task {
this._data[key] = results;
this._data_keys.push(key);
}
let runningCheck = (fn: () => Promise<any>): Promise<any> => {
if (!this._running) return Promise.reject("The task is stopped by user.");
return fn();
}
return urls.reduce((p, url, i) => p.then(
results => {
if (i > 0 && results instanceof Array) {
@ -70,12 +80,13 @@ export class Task {
saveResult(results, lastURL);
}
if (this._data[url]) return;
let pms: Promise<any> = redirectTab(tab, url);
let pms: Promise<any> = runningCheck(() => redirectTab(tab, url));
if (this._options["scrollToBottom"]) {
pms = pms.then(() => scrollToBottom(tab));
pms = pms.then(() => runningCheck(() => scrollToBottom(tab)));
}
return pms.then(
() => extractTabData(tab, this._itemsSelector, this._fieldSelectors)
() => runningCheck(() => extractTabData(tab, this._itemsSelector, this._fieldSelectors))
);
}
), Promise.resolve<string[][]>(null)).then(
@ -83,9 +94,14 @@ export class Task {
if (results && results.length) {
let lastURL = urls[urls.length - 1];
saveResult(results, lastURL);
return;
this._running = false;
}
}
).catch(
e => {
this._running = false;
return Promise.reject(e);
}
);
}
}