|
|
|
@ -53,7 +53,8 @@ class Index extends CI_Controller {
|
|
|
|
|
//
|
|
|
|
|
//提取和下载所有JS脚本,包括链接文件和页面脚本
|
|
|
|
|
$link_js_array = array();
|
|
|
|
|
$js_content = '';
|
|
|
|
|
$js_inline_content = '';
|
|
|
|
|
$js_jquery_content = '';
|
|
|
|
|
foreach ($html_object->find('script') as $link_script) {
|
|
|
|
|
if (!empty($link_script->src)) {
|
|
|
|
|
$link_js_array[] = $link_script->src;
|
|
|
|
@ -62,22 +63,19 @@ class Index extends CI_Controller {
|
|
|
|
|
//网页内的js不需要提取
|
|
|
|
|
//$js_content.= $link_script->innertext;//js的内容
|
|
|
|
|
// $js_content.= $link_script;//js的内容,包含<script>标签
|
|
|
|
|
|
|
|
|
|
//$link_script_content = $link_script;
|
|
|
|
|
//含有$表示调用了jquery的函数,添加延迟加载defer
|
|
|
|
|
// if (strpos($link_script_content, '$') !== FALSE) {
|
|
|
|
|
// $link_script_content = str_replace('defer', 'defer', $link_script_content);
|
|
|
|
|
// $link_script_content = str_replace('<script', '<script defer', $link_script_content);
|
|
|
|
|
// $js_content.= $link_script_content;
|
|
|
|
|
// } else {
|
|
|
|
|
$js_content.= $link_script; //js的内容,包含<script>标签
|
|
|
|
|
// }
|
|
|
|
|
if (strpos($link_script, '$') !== FALSE) {
|
|
|
|
|
$js_jquery_content.= $link_script->innertext;
|
|
|
|
|
} else {
|
|
|
|
|
$js_inline_content.= $link_script; //js的内容,包含<script>标签
|
|
|
|
|
}
|
|
|
|
|
$link_script->outertext = ''; //删除js,移动到页底
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$js_scr_content = '';
|
|
|
|
|
foreach ($link_js_array as $item) {
|
|
|
|
|
//$js_content.= GET_HTTP($this->format_url($item, $websitehost));
|
|
|
|
|
$js_scr_content.= GET_HTTP($this->format_url($item, $websitehost));
|
|
|
|
|
}
|
|
|
|
|
//echo $js_content;
|
|
|
|
|
//把网页内容和css提交到purifycss处理
|
|
|
|
@ -105,15 +103,15 @@ class Index extends CI_Controller {
|
|
|
|
|
$lastload_js.='<link rel="stylesheet" type="text/css" href="' . $item . '"/>';
|
|
|
|
|
}
|
|
|
|
|
$lastload_js.='</noscript><script>var loadDeferredStyles=function(){var addStylesNode=document.getElementById("deferred-styles");var replacement=document.createElement("div");replacement.innerHTML=addStylesNode.textContent;document.body.appendChild(replacement);addStylesNode.parentElement.removeChild(addStylesNode)};var raf=requestAnimationFrame||mozRequestAnimationFrame||webkitRequestAnimationFrame||msRequestAnimationFrame;if(raf){raf(function(){window.setTimeout(loadDeferredStyles,0)})}else{window.addEventListener("load",loadDeferredStyles)};</script>';
|
|
|
|
|
//把js移动到页面底部
|
|
|
|
|
foreach ($link_js_array as $item) {
|
|
|
|
|
$lastload_js.='<script src="' . $item . '"></script>';
|
|
|
|
|
}
|
|
|
|
|
$lastload_js.=$js_content;
|
|
|
|
|
|
|
|
|
|
$js_content = $js_scr_content . $js_jquery_content;
|
|
|
|
|
//延迟加载js,需要把返回的js代码保存到一个文件中,然后替换占位符,以便加载js文件
|
|
|
|
|
$lastload_js.='<script defer src="##DEFERSCRIPTDEFER##"></script>';
|
|
|
|
|
$lastload_js.=$js_inline_content;
|
|
|
|
|
|
|
|
|
|
$html_object = str_replace('</body>', $lastload_js . '</body>', $html_object);
|
|
|
|
|
}
|
|
|
|
|
echo $html_object;
|
|
|
|
|
echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $html_object, 'js_content' => $js_content)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//格式化url,保证请求的URL有域名,//更换为对应的域名路径
|
|
|
|
|