Extractor.stop()
This commit is contained in:
13
readme.md
13
readme.md
@ -13,10 +13,14 @@ All you need to do is:
|
|||||||
## Qucik Start
|
## Qucik Start
|
||||||
|
|
||||||
Extract current page
|
Extract current page
|
||||||
|
|
||||||
```js
|
```js
|
||||||
$('.item', ['a', 'a@href']);
|
$('.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)
|
Extract multiple pages (1-10, interval 1)
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -54,6 +58,15 @@ function (itemsSelector:string, fieldSelectors:string[], urls:ExtractResult)
|
|||||||
|
|
||||||
Close the target tab, in which current tasks is running.
|
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.
|
## Extract Attributes.
|
||||||
|
|
||||||
e.g.: link text and target (use 'selector@attribute')
|
e.g.: link text and target (use 'selector@attribute')
|
||||||
|
|||||||
@ -51,14 +51,19 @@ export class Extractor {
|
|||||||
/**
|
/**
|
||||||
* Start the task chain.
|
* Start the task chain.
|
||||||
*/
|
*/
|
||||||
async start() {
|
start() {
|
||||||
return this._startTasks(0);
|
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.
|
* restart from specified task, but don't restart the previous tasks.
|
||||||
* @param {number} from where to restart the tasks, begins with 0
|
* @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);
|
let id = this._checkTaskId(from, 0);
|
||||||
if (id < 0) return;
|
if (id < 0) return;
|
||||||
for (let i = id; i < this._tasks.length; i++) {
|
for (let i = id; i < this._tasks.length; i++) {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ export class Task {
|
|||||||
private _itemsSelector: string;
|
private _itemsSelector: string;
|
||||||
private _fieldSelectors: string[];
|
private _fieldSelectors: string[];
|
||||||
private _urls: string[] = [];
|
private _urls: string[] = [];
|
||||||
|
private _running = false;
|
||||||
|
|
||||||
constructor(options: any, ...arg: any);
|
constructor(options: any, ...arg: any);
|
||||||
constructor(options: any, itemsSelector: string, fieldSelectors: string[]);
|
constructor(options: any, itemsSelector: string, fieldSelectors: string[]);
|
||||||
@ -49,8 +50,13 @@ export class Task {
|
|||||||
this._data_keys = [];
|
this._data_keys = [];
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
stop() {
|
||||||
|
this._running = false;
|
||||||
|
}
|
||||||
async execute(tab: chrome.tabs.Tab, upstreamData?: ExtractResult): Promise<void> {
|
async execute(tab: chrome.tabs.Tab, upstreamData?: ExtractResult): Promise<void> {
|
||||||
if (!tab) return Promise.reject("No tab to execute the task.");
|
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
|
let urls = this._urls
|
||||||
if (!urls.length) {
|
if (!urls.length) {
|
||||||
if (upstreamData) {
|
if (upstreamData) {
|
||||||
@ -63,6 +69,10 @@ export class Task {
|
|||||||
this._data[key] = results;
|
this._data[key] = results;
|
||||||
this._data_keys.push(key);
|
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(
|
return urls.reduce((p, url, i) => p.then(
|
||||||
results => {
|
results => {
|
||||||
if (i > 0 && results instanceof Array) {
|
if (i > 0 && results instanceof Array) {
|
||||||
@ -70,12 +80,13 @@ export class Task {
|
|||||||
saveResult(results, lastURL);
|
saveResult(results, lastURL);
|
||||||
}
|
}
|
||||||
if (this._data[url]) return;
|
if (this._data[url]) return;
|
||||||
let pms: Promise<any> = redirectTab(tab, url);
|
|
||||||
|
let pms: Promise<any> = runningCheck(() => redirectTab(tab, url));
|
||||||
if (this._options["scrollToBottom"]) {
|
if (this._options["scrollToBottom"]) {
|
||||||
pms = pms.then(() => scrollToBottom(tab));
|
pms = pms.then(() => runningCheck(() => scrollToBottom(tab)));
|
||||||
}
|
}
|
||||||
return pms.then(
|
return pms.then(
|
||||||
() => extractTabData(tab, this._itemsSelector, this._fieldSelectors)
|
() => runningCheck(() => extractTabData(tab, this._itemsSelector, this._fieldSelectors))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
), Promise.resolve<string[][]>(null)).then(
|
), Promise.resolve<string[][]>(null)).then(
|
||||||
@ -83,9 +94,14 @@ export class Task {
|
|||||||
if (results && results.length) {
|
if (results && results.length) {
|
||||||
let lastURL = urls[urls.length - 1];
|
let lastURL = urls[urls.length - 1];
|
||||||
saveResult(results, lastURL);
|
saveResult(results, lastURL);
|
||||||
return;
|
this._running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
).catch(
|
||||||
|
e => {
|
||||||
|
this._running = false;
|
||||||
|
return Promise.reject(e);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user