
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/
    
// 复用操作
/*-------------------------------------------------------------------------------------------*/	
    class spider
    {
		static async anal(str,pat,fun,par)
		{
			//最佳策略，被抓取网页源码里，有换行才换（可以不要某些行，
			//但不要，将本是一行的写成几行。虽然强行写成几行还是可以
			//匹配，但效率会降低）
			//会忽略缩进
			pat=new RegExp(pat.replace(/\n\s*|\r\n\s*/g,'[\\s|\\S]*?'),'g');
			let arr=[];
			let res='';
			while(res=pat.exec(str))
            {
				res.shift();
				Reflect.deleteProperty(res,'input')
				Reflect.deleteProperty(res,'index')
				if(fun)
				{
					await fun(res,par)
				}
				else
				{
					arr.push(res)
				} 
			}
			return arr;
        }
        
		static async getResource(page,url,save,tag)
		{
            if(!page || !url)return false;
            const fs = require('fs');
            const content = await spider.getResourceContent(page, url);
            const contentBuffer = Buffer.from(content, 'base64');
            if(save)
            {
                if(tag)
                {
                    fs.writeFile(save, contentBuffer, 'base64',function(){})
                }
                else
                {
                    return fs.writeFileSync(save, contentBuffer, 'base64');
                }
            }
            else
            {
                return contentBuffer
            }
        }
        
        static async getResourceTree(page) 
        {
            var resource = await page._client.send('Page.getResourceTree');
            return resource.frameTree;
        }

        static async getResourceContent(page, url) 
        {
            const assert = require('assert');
            const { content, base64Encoded } = await page._client.send
            (
                'Page.getResourceContent',
                { 
                    frameId: String(page.mainFrame()._id), 
                    url 
                }
            );
            assert.equal(base64Encoded, true);
            return content;
        }
    }
/*-------------------------------------------------------------------------------------------*/    
module.exports=spider;