specify that it is meant to be used externally
This commit is contained in:
		
							parent
							
								
									1b83298c76
								
							
						
					
					
						commit
						f0cb206a89
					
				
					 2 changed files with 57 additions and 69 deletions
				
			
		|  | @ -1,69 +0,0 @@ | ||||||
| /* reader.js |  | ||||||
| Read the contents of the page.   |  | ||||||
| */ |  | ||||||
| 
 |  | ||||||
| export default class reader { |  | ||||||
| 	constructor(scraper_fields) { |  | ||||||
| 		let field_content; |  | ||||||
| 
 |  | ||||||
| 		if (Array.isArray(scraper_fields)) { |  | ||||||
| 			// Convert scraper_fields to a dictionary.
 |  | ||||||
| 			let scraper_fields_all = {}; |  | ||||||
| 
 |  | ||||||
| 			for (let field_index = 0; field_index < scraper_fields.length; field_index++) { |  | ||||||
| 				scraper_fields_all[field_index] = scraper_fields[field_index]; |  | ||||||
| 			} |  | ||||||
| 
 |  | ||||||
| 			scraper_fields = scraper_fields_all; |  | ||||||
| 		}; |  | ||||||
| 		if (typeof scraper_fields === "object" && scraper_fields != null && scraper_fields) { |  | ||||||
| 
 |  | ||||||
| 			/* Read for the particular fields. */ |  | ||||||
| 			function read(fields) { |  | ||||||
| 				let field_names = Object.keys(fields), field_data = {}; |  | ||||||
| 
 |  | ||||||
| 				for (let field_index = 0; field_index < fields.length; field_index++) { |  | ||||||
| 					let field_current = fields[field_names[field_index]]; |  | ||||||
| 
 |  | ||||||
| 					if (field_current) { |  | ||||||
| 						// Check if array.
 |  | ||||||
| 						if (Array.isArray(field_current)) { |  | ||||||
| 							// Temporarily create an empty list. 
 |  | ||||||
| 							field_data[field_names[field_index]] = []; |  | ||||||
| 							 |  | ||||||
| 							if (typeof field_current[0] === "object" && field_current[0] != null && field_current[0]) { |  | ||||||
| 								field_data[field_names[field_index]].push(read(field_current[0])); |  | ||||||
| 							} else { |  | ||||||
| 								let matching_elements = (document.querySelectorAll(field_current[0])); |  | ||||||
| 								for (let field_current_index = 0; field_current_index < matching_elements.length; field_current_index++) { |  | ||||||
| 									field_data[field_names[field_index]].push(matching_elements[field_current_index].innerText); |  | ||||||
| 								}; |  | ||||||
| 							}; |  | ||||||
| 							 |  | ||||||
| 							// Must cease to exist if it was just empty anyway. 
 |  | ||||||
| 							if ((field_data[field_names[field_index]]).length <= 0) { |  | ||||||
| 								delete field_data[field_names[field_index]]; |  | ||||||
| 							} |  | ||||||
| 						} else if (typeof field_current === "object" && field_current != null && field_current) { |  | ||||||
| 							field_data[field_names[field_index]] = read(field_current); |  | ||||||
| 						} else { |  | ||||||
| 							if (document.querySelector(field_current)) { |  | ||||||
| 								field_data[field_names[field_index]] = document.querySelector(field_current).innerText; |  | ||||||
| 							} |  | ||||||
| 						}; |  | ||||||
| 					}; |  | ||||||
| 				}; |  | ||||||
| 
 |  | ||||||
| 				return field_data; |  | ||||||
| 			}; |  | ||||||
| 
 |  | ||||||
| 			field_content = read(scraper_fields); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		if (Object.keys(field_content).length > 0) { |  | ||||||
| 			for (let field_name in Object.keys(field_content)) { |  | ||||||
| 				this[field_name] = field_content[field_name]; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
							
								
								
									
										57
									
								
								scripts/outside/scraper.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								scripts/outside/scraper.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,57 @@ | ||||||
|  | /* reader.js | ||||||
|  | Read the contents of the page.   | ||||||
|  | */ | ||||||
|  | 
 | ||||||
|  | export default class scraper { | ||||||
|  | 	constructor(scraper_fields) { | ||||||
|  | 		let field_content; | ||||||
|  | 
 | ||||||
|  | 		if ((typeof scraper_fields).includes("object") && scraper_fields != null && scraper_fields) { | ||||||
|  | 
 | ||||||
|  | 			/* Read for the particular fields. */ | ||||||
|  | 			function read(fields) { | ||||||
|  | 				let field_data = {}; | ||||||
|  | 				 | ||||||
|  | 				console.log(Object.keys(fields)); | ||||||
|  | 
 | ||||||
|  | 				(Object.keys(fields)).forEach((FIELD_NAME) => { | ||||||
|  | 					let FIELD = {"name": FIELD_NAME, "value": fields[FIELD_NAME]}; | ||||||
|  | 
 | ||||||
|  | 					if (FIELD[`value`]) { | ||||||
|  | 						// Check if array.
 | ||||||
|  | 						if (Array.isArray(FIELD[`value`])) { | ||||||
|  | 							// Temporarily create an empty list. 
 | ||||||
|  | 							field_data[FIELD[`name`]] = []; | ||||||
|  | 							 | ||||||
|  | 							if (typeof FIELD[`value`][0] == "object" && FIELD[`value`][0] != null && !Array.isArray(FIELD[`value`][0])) { | ||||||
|  | 								field_data[FIELD[`name`]].push(read(FIELD[`value`][0])); | ||||||
|  | 							} else { | ||||||
|  | 								let ELEMENTS = (document.querySelectorAll(FIELD[`value`][0])); | ||||||
|  | 								 | ||||||
|  | 								if (ELEMENTS.length > 0) { | ||||||
|  | 									(ELEMENTS).forEach((ELEMENT) => { | ||||||
|  | 										field_data[FIELD[`name`]].push(ELEMENT.innerText); | ||||||
|  | 									}) | ||||||
|  | 								}; | ||||||
|  | 							}; | ||||||
|  | 						} else if ((typeof FIELD[`value`]).includes(`obj`) && FIELD[`value`] != null) { | ||||||
|  | 							field_data[FIELD[`name`]] = read(FIELD[`value`]); | ||||||
|  | 						} else if (document.querySelector(FIELD[`value`])) { | ||||||
|  | 							field_data[FIELD[`name`]] = document.querySelector(FIELD[`value`]).innerText; | ||||||
|  | 						}; | ||||||
|  | 					}; | ||||||
|  | 				}); | ||||||
|  | 
 | ||||||
|  | 				return field_data; | ||||||
|  | 			}; | ||||||
|  | 			field_content = read(scraper_fields); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if (Object.keys(field_content).length > 0) { | ||||||
|  | 			for (let field_name in Object.keys(field_content)) { | ||||||
|  | 				console.log(field_name); | ||||||
|  | 				this[field_name] = field_content[field_name]; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue