save and load state
This commit is contained in:
		| @ -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 | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| }); | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
|  | ||||
| @ -1,12 +1,8 @@ | ||||
| const EXT_NAME = "DataExtracter"; | ||||
|  | ||||
| const URL_REG = getWebUrl(); | ||||
|  | ||||
| const ACTION_EXTRACT = `${EXT_NAME}:Extract`; | ||||
| const ACTION_GOTO_URL = `${EXT_NAME}:GoToTUL`; | ||||
| const ACTION_REPORT_IN = `${EXT_NAME}:ReportIn`; | ||||
| const ACTION_QUERY_URL = `${EXT_NAME}:QueryURL`; | ||||
| const ACTION_SCROLL_BOTTOM = `${EXT_NAME}:ScrollToBottom`; | ||||
|  | ||||
| const MSG_ELEMENT_NOT_FOUND = new ConstMessage(1, "No element found for at least one selector, maybe it's not loaded yet"); | ||||
| const MSG_URL_SKIPPED = new ConstMessage(100, "Skipped current URL"); | ||||
| const ACTION_UPLOAD_STATE = `${EXT_NAME}:UploadStateFile`; | ||||
|  | ||||
| @ -9,6 +9,10 @@ class ConstMessage { | ||||
|     } | ||||
| } | ||||
|  | ||||
| const URL_REG = getWebUrl(); | ||||
| const MSG_ELEMENT_NOT_FOUND = new ConstMessage(1, "No element found for at least one selector, maybe it's not loaded yet"); | ||||
| const MSG_URL_SKIPPED = new ConstMessage(100, "Skipped current URL"); | ||||
|  | ||||
| function saveFile(data, mimeType, fileName) { | ||||
|     fileName = fileName || document.title || "result"; | ||||
|     var blob; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user