php 用于打包任何带有嵌入式 javascript 和 css 的 URL?
php for packaging any URL with embedded javascript and css?
我们需要一个 cronjob 来创建我们匿名页面的静态版本。
每个 URL 应保存为单个 HTML 文档,所有外部 <script src="">
标签替换为它们的 javascript 并替换所有 <link href="">
标签与他们 CSS。 (css 不需要内联为属性)。
在我重新发明轮子之前,PHP中有没有简单的打包脚本可以做到这一点?
我的简单解决方案...
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$html = curl_exec($ch);
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno) . ' -- ' . $url;
die ("cURL error ({$errno}):\n {$error_message}");
}
curl_close($ch);
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->recover = true;
$internalErrors = libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors($internalErrors);
foreach($doc->getElementsByTagName('script') as $script) {
if($script->hasAttribute('src')) {
$path = $script->getAttribute('src');
if (strpos($path, 'http') !== 0) {
if (strpos($path, '?') > -1) $path = substr($path, 0, strpos($path, '?'));
$path = ROOT_CD . $path;
}
$source = file_get_contents($path);
$new = $doc->createElement('script');
$new->setAttribute('type', 'text/javascript');
$new->setAttribute('sourcePath', $script->getAttribute('src'));
$new->setAttribute('language', 'javascript');
$source = $doc->createTextNode($source);
$new->appendChild($source);
$script->parentNode->replaceChild($new, $script);
}
}
foreach($doc->getElementsByTagName('link') as $script) {
if($script->hasAttribute('href') && $script->hasAttribute('rel')) {
$type = $script->getAttribute('rel');
if ($type !== 'stylesheet') continue;
$path = $script->getAttribute('href');
if (strpos($path, 'http') !== 0) {
if (strpos($path, '?') > -1) $path = substr($path, 0, strpos($path, '?'));
$path = ROOT_CD . $path;
}
$source = file_get_contents($path);
$new = $doc->createElement('style');
$new->setAttribute('type', 'text/css');
$new->setAttribute('sourcePath', $script->getAttribute('src'));
$source = $doc->createTextNode($source);
$new->appendChild($source);
$script->parentNode->replaceChild($new, $script);
}
}
$html = $doc->saveHTML();
require_once '../preprocessing/minifier.php';
$html = compressHTML($html);
file_put_contents("static-login-".$platform.".html", $html);
echo $html;
我们需要一个 cronjob 来创建我们匿名页面的静态版本。
每个 URL 应保存为单个 HTML 文档,所有外部 <script src="">
标签替换为它们的 javascript 并替换所有 <link href="">
标签与他们 CSS。 (css 不需要内联为属性)。
在我重新发明轮子之前,PHP中有没有简单的打包脚本可以做到这一点?
我的简单解决方案...
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$html = curl_exec($ch);
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno) . ' -- ' . $url;
die ("cURL error ({$errno}):\n {$error_message}");
}
curl_close($ch);
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->recover = true;
$internalErrors = libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors($internalErrors);
foreach($doc->getElementsByTagName('script') as $script) {
if($script->hasAttribute('src')) {
$path = $script->getAttribute('src');
if (strpos($path, 'http') !== 0) {
if (strpos($path, '?') > -1) $path = substr($path, 0, strpos($path, '?'));
$path = ROOT_CD . $path;
}
$source = file_get_contents($path);
$new = $doc->createElement('script');
$new->setAttribute('type', 'text/javascript');
$new->setAttribute('sourcePath', $script->getAttribute('src'));
$new->setAttribute('language', 'javascript');
$source = $doc->createTextNode($source);
$new->appendChild($source);
$script->parentNode->replaceChild($new, $script);
}
}
foreach($doc->getElementsByTagName('link') as $script) {
if($script->hasAttribute('href') && $script->hasAttribute('rel')) {
$type = $script->getAttribute('rel');
if ($type !== 'stylesheet') continue;
$path = $script->getAttribute('href');
if (strpos($path, 'http') !== 0) {
if (strpos($path, '?') > -1) $path = substr($path, 0, strpos($path, '?'));
$path = ROOT_CD . $path;
}
$source = file_get_contents($path);
$new = $doc->createElement('style');
$new->setAttribute('type', 'text/css');
$new->setAttribute('sourcePath', $script->getAttribute('src'));
$source = $doc->createTextNode($source);
$new->appendChild($source);
$script->parentNode->replaceChild($new, $script);
}
}
$html = $doc->saveHTML();
require_once '../preprocessing/minifier.php';
$html = compressHTML($html);
file_put_contents("static-login-".$platform.".html", $html);
echo $html;