From e55c7204bed02e4259375c41f6d241939439a425 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=B0=B9=E8=AF=9A=E8=AF=9A?=
Date: Fri, 9 Jun 2017 17:53:12 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A1=B5=E9=9D=A2css?=
=?UTF-8?q?=E5=92=8Cjs=E5=8A=A0=E8=BD=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
application/libraries/simple_html_dom_lib.php | 9 +
.../htmlcompressor/controllers/index.php | 116 ++
.../htmlcompressor/views/welcome.php | 22 +
lib/simple_html_dom.php | 1721 +++++++++++++++++
lib/第三方类库,调用方法请看.txt | 3 +
5 files changed, 1871 insertions(+)
create mode 100644 application/libraries/simple_html_dom_lib.php
create mode 100644 application/third_party/htmlcompressor/controllers/index.php
create mode 100644 application/third_party/htmlcompressor/views/welcome.php
create mode 100644 lib/simple_html_dom.php
diff --git a/application/libraries/simple_html_dom_lib.php b/application/libraries/simple_html_dom_lib.php
new file mode 100644
index 00000000..0e0d7192
--- /dev/null
+++ b/application/libraries/simple_html_dom_lib.php
@@ -0,0 +1,9 @@
+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 '';
+ return FALSE;
+ }
+
+ //把精简的css添加到head后面
+ $html_object = str_replace('', '", $html_object);
+
+//在最后加载原始css文件和js文件
+ $lastload_js = '';
+ //把js移动到页面底部
+ foreach ($link_js_array as $item) {
+ $lastload_js.='';
+ }
+ $lastload_js.=$js_content;
+
+ $html_object = str_replace('
+ 页面样式精简
+
+
+ ', $lastload_js . '', $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);
+ }
+
+}
diff --git a/application/third_party/htmlcompressor/views/welcome.php b/application/third_party/htmlcompressor/views/welcome.php
new file mode 100644
index 00000000..e18468fd
--- /dev/null
+++ b/application/third_party/htmlcompressor/views/welcome.php
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
HTML-Compressor
+
+
+
+