load->library('simple_html_dom_lib'); } public function index(){ $this->load->view('welcome'); } //选择处理方式 public function choose_way(){ $this->htmlsource = $this->input->post('htmlsource'); $this->websitehost = $this->input->post('websitehost'); $this->template_name = $this->input->post('template_name'); $this->create_amp = $this->input->post('create_amp'); $this->debug = $this->input->post('debug'); /*$this->htmlsource = file_get_contents('aa.htm'); $this->websitehost = 'https://data.chinarundreisen.com'; $this->create_amp = 'true'; $this->template_name = 'gm';*/ if (empty($this->htmlsource) || empty($this->websitehost)) { $this->output->set_status_header(500); echo 'error:htmlsource or websitehost is empty!'; log_message('error', "htmlsource or websitehost is empty! " . $this->websitehost); return false; } //域名后面不能有/ if (substr($this->websitehost, -1, 1) == '/') { $this->websitehost = substr($this->websitehost, 0, -1); } //进行页面解析 $this->html_object = str_get_html($this->htmlsource); //PC或AMP的构造方式 if (!empty($this->create_amp)) { echo $this->create_amp(); return; }else{ echo $this->optimize(); return; } } //html优化处理 function optimize(){ //提取css样式地址 $link_css_array = array(); $css_content = ''; foreach ($this->html_object->find('link') as $link_css) { //将所有的样式链接存储为一个数组 if ($link_css->rel == 'stylesheet' && !empty($link_css->href)) { $link_css_array[] = $link_css->href; $link_css->outertext = ''; //删除链接 } } //循环下载所有的css样式,拼接成字符串 foreach ($link_css_array as $item) { $get_http_temp = GET_HTTP($this->format_url($item, $this->websitehost)); if ($get_http_temp == false) { $this->output->set_status_header(404); echo 'CSS文件下载错误'; log_message('error', "optimize CSS文件下载错误! " . $item); return FALSE; } $css_content .= $get_http_temp; } //找出所有style写的内置样式 foreach ($this->html_object->find('style') as $style_css) { if ($style_css->type == "text/css") { $css_content .= $style_css->innertext; } } //提取和下载所有JS脚本,包括链接文件和页面脚本 $link_js_array = array(); $js_inline_content = ''; $js_jquery_content = ''; foreach ($this->html_object->find('script') as $link_script) { if (!empty($link_script->src)) { if (empty($link_script->defer) && empty($link_script->async)) { //没有defer标记的才处理,否则让js延后加载 $link_js_array[] = $link_script->src; $link_script->outertext = ''; //删除链接,移动到页底 } } else { //网页内的js不需要提取 //$js_content.= $link_script->innertext;//js的内容 // $js_content.= $link_script;//js的内容,包含'; $js_content = $js_scr_content . $js_jquery_content; //延迟加载js,需要把返回的js代码保存到一个文件中,然后替换占位符,以便加载js文件 $lastload_js .= ''; $lastload_js .= $js_inline_content; $this->html_object = str_replace('', $lastload_js . '', $this->html_object); echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $this->html_object, 'js_content' => $js_content))); } //生成AMP版本 用移动端版本生成 /* 1.加载站点AMP模板,模板只有导航、头部、底部 2.删除全部js引用和内链代码 3.删除全部css引用,内联css放入