-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathRemoveMagentoInitScripts.php
More file actions
executable file
·81 lines (69 loc) · 2.58 KB
/
RemoveMagentoInitScripts.php
File metadata and controls
executable file
·81 lines (69 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace React\React;
use Magento\Framework\App\Config\ScopeConfigInterface as Config;
use Magento\Framework\App\Response\HttpInterface as HttpResponse;
class RemoveMagentoInitScripts
{
private $flag = false;
private $actionFilter = [
'catalog_category_view',
'cms_index_index',
'cms_page_view',
'catalog_product_view',
'catalogsearch_result_index',
'cms_noroute_index',
'customer_account_login',
'customer_account_create',
];
/**
* Modify the final HTML output before sending it to the browser.
*
* @param HttpResponse $subject
* @param string $result
* @return string
*/
public function afterGetContent(HttpResponse $subject, $result)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get(\Magento\Framework\App\Request\Http::class);
$config = $objectManager->get(Config::class);
$removeAdobeJSJunk = boolval($config->getValue('react_vue_config/junk/remove'));
if (isset($_GET['js-junk']) && $_GET['js-junk'] === "false") {
$removeAdobeJSJunk = false;
}
if (isset($_GET['js-junk']) && $_GET['js-junk'] === "true") {
$removeAdobeJSJunk = true;
}
if ($removeAdobeJSJunk) {
$actionName = $request->getFullActionName();
$content = $result;
if (!in_array($actionName, $this->actionFilter)) {
return $result;
}
if ((!is_string($content) || empty($content) || $this->flag)) {
return $result;
}
$startTime = microtime(true);
// Remove all `<script type="text/x-magento-init">` blocks
$result = preg_replace('/<script[^>]+type=["\']text\/x-magento-init["\'][^>]*>.*?<\/script>/is', '', $result);
//$this->flag = true;
$endTime = microtime(true);
$time = $endTime - $startTime;
header("Server-Timing: x-mag-init;dur=" . number_format($time * 1000, 2), false);
}
if ($removeAdobeJSJunk) {
return $result;
}
$html = $result;
if ($html == '') {
return $result;
}
$conditionalJsPattern = '@(?:<script type="text/javascript"|<script)(.*)</script>@msU';
preg_match_all($conditionalJsPattern, $html, $_matches);
$jsHtml = implode('', $_matches[0]);
$html = preg_replace($conditionalJsPattern, '', $html);
$html .= $jsHtml;
$result = $html;
return $result;
}
}