优化页面css和js加载

hotfix/远程访问多媒体中心
尹诚诚 8 years ago
parent 687d4e8209
commit e55c7204be

@ -0,0 +1,9 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require '/lib/simple_html_dom.php';
class Simple_html_dom_lib extends CI_Controller{
}

@ -0,0 +1,116 @@
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Index extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('simple_html_dom_lib');
}
public function index() {
$this->load->view('welcome');
}
public function optimize() {
$htmlsource = $this->input->post('htmlsource');
$websitehost = $this->input->post('websitehost');
if (empty($htmlsource) || empty($websitehost)) {
$this->output->set_status_header(500);
echo 'error:htmlsource or websitehost is empty!';
return false;
}
//域名后面不能有/
if (substr($websitehost, -1, 1) == '/') {
$websitehost = substr($websitehost, 0, -1);
}
$html_object = str_get_html($htmlsource);
if (!empty($html_object)) {
//提取和下载所有CSS样式包括链接文件和页面样式
$link_css_array = array();
$css_content = '';
foreach ($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 = ''; //删除链接
}
}
//print_r($link_css_array);
foreach ($link_css_array as $item) {
$css_content.= GET_HTTP($this->format_url($item, $websitehost));
}
foreach ($html_object->find('style') as $style_css) {
if ($style_css->type == "text/css") {
$css_content .= $style_css->innertext;
}
}
// echo $css_content;
// echo $html_object;die();
//
//提取和下载所有JS脚本包括链接文件和页面脚本
$link_js_array = array();
$js_content = '';
foreach ($html_object->find('script') as $link_script) {
if (!empty($link_script->src)) {
$link_js_array[] = $link_script->src;
$link_script->outertext = ''; //删除链接,移动到页底
} else {
//网页内的js不需要提取
//$js_content.= $link_script->innertext;//js的内容
$js_content.= $link_script;
$link_script->outertext = ''; //删除js移动到页底
}
}
foreach ($link_js_array as $item) {
//$js_content.= GET_HTTP($this->format_url($item, $websitehost));
}
//echo $js_content;
//把网页内容和css提交到purifycss处理
$optimize_css = GET_HTTP('http://184.172.113.219:33033/', 'html_source=' . urlencode($htmlsource) . '&html_css=' . urlencode($css_content), 'POST');
if (empty($optimize_css)) {
$this->output->set_status_header(500);
echo 'css精简错误';
echo '<!-- ' . $css_content . ' -->';
return FALSE;
}
//把精简的css添加到head后面
$html_object = str_replace('</title>', '</title><style type="text/css">' . $optimize_css . "</style>", $html_object);
//在最后加载原始css文件和js文件
$lastload_js = '<script>';
//把css移动到页面底部
foreach ($link_css_array as $item) {
$lastload_js.='var elem=document.createElement("link");elem.rel="stylesheet";elem.type="text/css";elem.href="' . $item . '";document.body.appendChild(elem);';
}
$lastload_js.='</script>';
//把js移动到页面底部
foreach ($link_js_array as $item) {
$lastload_js.='<script src="' . $item . '"></script>';
}
$lastload_js.=$js_content;
$html_object = str_replace('</body>', $lastload_js . '</body>', $html_object);
}
echo $html_object;
}
//格式化url保证请求的URL有域名//更换为对应的域名路径
function format_url($url, $host = '') {
if (substr($url, 0, 8) == 'https://' || substr($url, 0, 7) == 'http://') {
return urldecode($url);
}
if (substr($url, 0, 2) == '//') { //https或http
return urldecode(str_replace('//', 'http://', $url));
}
return urldecode($host . $url);
}
}

@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Expires" CONTENT="0"/>
<meta http-equiv="Cache-Control" CONTENT="no-cache"/>
<meta http-equiv="Pragma" CONTENT="no-cache"/>
<title>HTML-Compressor</title>
</head>
<script type="text/javascript">
</script>
<body>
<h1>页面样式精简</h1>
<form name="form_htmlsource" id="form_htmlsource" action="<?php echo site_url('apps/htmlcompressor/index/optimize'); ?>" method="post">
<input type="text" name="websitehost" id="websitehost" style="width:200px;" placeholder="域名" /><br/><br/>
<textarea name="htmlsource" id="htmlsource" rows="22" style="width:800px;" placeholder="网页代码" ></textarea>
<input type="submit"/>
</form>
</body>
</html>

File diff suppressed because it is too large Load Diff

@ -10,3 +10,6 @@ require APPPATH.'/libraries/lib/alipay_service.class.php';
class xxx extends CI_Controller{ class xxx extends CI_Controller{
调用方法
$this->load->library('image_lib');
Loading…
Cancel
Save