118 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const signitures = `
 | |
| # DataExtracter Help
 | |
| ----------------------------
 | |
| 
 | |
| ## Signitures:
 | |
| ----------------------------
 | |
| 
 | |
| function extract(itemsSelector:string, fieldSelectors:string[])
 | |
| function extract(itemsSelector:string, fieldSelectors:string[], url:string, from:number, to:number, interval:number)
 | |
| function extract(itemsSelector:string, fieldSelectors:string, url:string, pages:number[])
 | |
| function extract(itemsSelector:string, fieldSelectors:string[], urls:string[])
 | |
| function extract(itemsSelector:string, fieldSelectors:string[], urls:ExractResult)
 | |
| 
 | |
| ## Examples:
 | |
| ----------------------------
 | |
| 
 | |
| ### Extract current page
 | |
| extract(".list-item", ["a.title", "p.content"])
 | |
| 
 | |
| ### Extract multiple pages (1-10, interval 1)
 | |
| extract(".list-item", ["a.title", "p.content"],"http://sample.com/?pn=\${page}", 1, 10, 1)
 | |
| 
 | |
| ### Extract multiple urls (list)
 | |
| extract(".list-item", ["a.title", "p.content"],["http://sample.com/abc","http://sample.com/xyz"])
 | |
| 
 | |
| ### Extract specified pages (1,3,5)
 | |
| extract(".list-item", ["a.title", "p.content"], "http://sample.com/?pn=\${page}", [1, 3, 5])
 | |
| 
 | |
| ## Advanced Examples:
 | |
| ----------------------------
 | |
| 
 | |
| ### Extract link text and target (use 'selector@attribute')
 | |
| extract('.list-item', ['a.title', 'a.title@href'])
 | |
| 
 | |
| ### Collect links from page(s) & Extract data of each link
 | |
| >> (Available only in console of extension background page)
 | |
| 
 | |
| extract('body',["a.title", "p.content"], await getData('.list-item', ['.item a@href'],["http://sample.com/abc"]))
 | |
| `.trim();
 | |
| 
 | |
| 
 | |
| function saveFile(data, mimeType, fileName) {
 | |
|     fileName = fileName || document.title || "result";
 | |
|     var blob;
 | |
|     if (typeof window.Blob == "function") {
 | |
|         blob = new Blob([data], {
 | |
|             type: mimeType
 | |
|         })
 | |
|     } else {
 | |
|         var BlobBuiler = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder;
 | |
|         var builer = new BlobBuiler();
 | |
|         builer.append(data);
 | |
|         blob = builer.getBlob(mimeType)
 | |
|     }
 | |
|     var URL = window.URL || window.webkitURL;
 | |
|     var url = URL.createObjectURL(blob);
 | |
|     var link = document.createElement("a");
 | |
|     if ('download' in link) {
 | |
|         link.style.visibility = "hidden";
 | |
|         link.href = url;
 | |
|         link.download = fileName;
 | |
|         document.body.appendChild(link);
 | |
|         var j = document.createEvent("MouseEvents");
 | |
|         j.initEvent("click", true, true);
 | |
|         link.dispatchEvent(j);
 | |
|         document.body.removeChild(link)
 | |
|     } else if (navigator.msSaveBlob) {
 | |
|         navigator.msSaveBlob(blob, fileName)
 | |
|     } else {
 | |
|         location.href = url
 | |
|     }
 | |
| }
 | |
| 
 | |
| function testArgs(...args) {
 | |
|     switch (args.length) {
 | |
|         case 0, 1:
 | |
|             return false;
 | |
|         case 2:
 | |
|             return args[0] && args[1] &&
 | |
|                 (typeof args[0] == "string") &&
 | |
|                 (args[1] instanceof Array) &&
 | |
|                 testArrayVals(args[1], v => typeof v == "string");
 | |
|         case 3:
 | |
|             return args[0] && args[1] &&
 | |
|                 typeof args[0] == "string" &&
 | |
|                 args[1] instanceof Array &&
 | |
|                 testArrayVals(args[1], v => typeof v == "string") &&
 | |
|                 (
 | |
|                     (
 | |
|                         args[2] instanceof Array &&
 | |
|                         testArrayVals(args[2], v => typeof v == "string")
 | |
|                     ) || (
 | |
|                         args[2] instanceof ExractResult
 | |
|                     )
 | |
|                 );
 | |
|         case 4:
 | |
|             return args[0] && args[1] &&
 | |
|                 typeof args[0] == "string" &&
 | |
|                 args[1] instanceof Array &&
 | |
|                 testArrayVals(args[1], v => typeof v == "string") &&
 | |
|                 typeof args[2] == "string" &&
 | |
|                 args[3] instanceof Array &&
 | |
|                 testArrayVals(args[3], v => typeof v == "number");
 | |
|         case 6:
 | |
|             return args[0] && args[1] &&
 | |
|                 typeof args[0] == "string" &&
 | |
|                 args[1] instanceof Array &&
 | |
|                 testArrayVals(args[1], v => typeof v == "string") &&
 | |
|                 typeof args[2] == "string" &&
 | |
|                 !isNaN(args[3]) && !isNaN(args[4]) && !isNaN(args[5]);
 | |
|         default:
 | |
|             return false;
 | |
|     }
 | |
| 
 | |
|     function testArrayVals(arr, tester) {
 | |
|         return arr.reduce((p, c) => p && tester(c), true);
 | |
|     }
 | |
| } |