save and load state

This commit is contained in:
2020-01-12 16:19:38 +08:00
parent f1cf32b83a
commit c7f4fe7cc4
7 changed files with 85 additions and 17 deletions

View File

@ -1,9 +1,31 @@
var __EXTRACTOR_STATE__ = "";
class Extractor {
constructor(options) {
this._tasks = [];
this._running = false;
this._options = options;
}
/**
* Save current state, in case we restore it later.
*/
save() {
saveFile(JSON.stringify(this), 'application/json', 'state.json');
}
/**
* Restore previous state by loading from saved state.
*/
load() {
if (!__EXTRACTOR_STATE__) {
console.log('No state found. \nPlease upload a saved state from the popup window first.');
return;
}
let state = JSON.parse(__EXTRACTOR_STATE__);
__EXTRACTOR_STATE__ = "";
this._options = state._options;
this._tasks = state._tasks.map(t => new Task(this._options, 'whaterver', ['whaterver']).load(t));
return this;
}
/**
* Add a task to Extractor. \n
* One Extractor could has multiple tasks, which orgnized in a task chian.
@ -76,7 +98,7 @@ class Extractor {
}, Promise.resolve(undefined)).then(
() => {
this._running = false;
this.save();
this.export();
}
).catch(err => {
this._running = false;
@ -84,10 +106,10 @@ class Extractor {
});
}
/**
* Save result of a task
* export result of a task to CSV
* @param {number} taskid which task id to save, begins with 0
*/
save(taskid) {
export(taskid) {
let id = this._checkTaskId(taskid, this._tasks.length - 1);
if (id < 0) return;
let results = this._tasks[id].results

View File

@ -50,10 +50,18 @@ function sendMessage(tab, req, log, cond, interval, limit = 0) {
});
}
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (!message.action || !message.action.startsWith(EXT_NAME)) {
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (!request.action || !request.action.startsWith(EXT_NAME)) {
return;
}
sendResponse("Calling from user pages is not allowed.");
return;
switch (request.action) {
case ACTION_UPLOAD_STATE:
sendResponse('recieved!');
__EXTRACTOR_STATE__ = request.state;
console.log(`State (${request.name}) recieved. Use following to load it: \nsome_var = new Extractor().load()`);
break;
default:
sendResponse("Request not supported.");
break;
}
});

View File

@ -17,6 +17,15 @@ class Task {
this._fieldSelectors = args.shift();
this._urls = parseUrls(...args);
}
load(state) {
this._itemsSelector = state._itemsSelector;
this._data = state._data;
this._data_keys = state._data_keys;
this._itemsSelector = state._itemsSelector;
this._fieldSelectors = state._fieldSelectors;
this._urls = state._urls;
return this;
}
get urls() {
return this._urls;
}