Version Notes
Be careful using option "Defer js"
Download this release
Release Info
Developer | Denis Derbenyov |
Extension | Radishok_LazyJs |
Version | 0.0.0.1 |
Comparing to | |
See all releases |
Version 0.0.0.1
app/code/local/Radishok/LazyJs/Model/Observer.php
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Radishok_LazyJs
|
4 |
+
*
|
5 |
+
* NOTICE OF LICENSE
|
6 |
+
*
|
7 |
+
* This source file is subject to the Open Software License (OSL 3.0)
|
8 |
+
* that is bundled with this package in the file LICENSE.txt.
|
9 |
+
* It is also available through the world-wide-web at this URL:
|
10 |
+
* http://opensource.org/licenses/osl-3.0.php
|
11 |
+
* If you did not receive a copy of the license and are unable to
|
12 |
+
* obtain it through the world-wide-web, please send an email
|
13 |
+
* to license@magentocommerce.com so we can send you a copy immediately.
|
14 |
+
*
|
15 |
+
* @category Radishok
|
16 |
+
* @package Radishok_LazyJs
|
17 |
+
* @copyright Copyright (c) 2014 Radishok
|
18 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
19 |
+
*/
|
20 |
+
|
21 |
+
class Radishok_LazyJs_Model_Observer extends Varien_Object{
|
22 |
+
const MODULE_ENABLED = 'radishoklazyjs/settings/active';
|
23 |
+
const DEFER_JS = 'radishoklazyjs/settings/defer_js';
|
24 |
+
protected $_allJs = array();
|
25 |
+
protected $_jsScripts = array();
|
26 |
+
protected $_jsScriptsSrc = array();
|
27 |
+
protected $_inlineScripts = array();
|
28 |
+
|
29 |
+
protected function _getResultScript(){
|
30 |
+
if(Mage::getStoreConfig(self::DEFER_JS)){
|
31 |
+
$script =
|
32 |
+
'<script type="text/javascript">'
|
33 |
+
. "//<![CDATA[
|
34 |
+
;(function(){
|
35 |
+
if (window.addEventListener)
|
36 |
+
window.addEventListener(\"load\", downloadJSAtOnload, false);
|
37 |
+
else if (window.attachEvent)
|
38 |
+
window.attachEvent(\"onload\", downloadJSAtOnload);
|
39 |
+
else window.onload = downloadJSAtOnload;
|
40 |
+
|
41 |
+
var scripts = ".Mage::helper('core')->jsonEncode($this->_jsScriptsSrc).";
|
42 |
+
function addScript(i){
|
43 |
+
var script = document.createElement('script');
|
44 |
+
script.onload = function(){
|
45 |
+
if(++i < scripts.length){
|
46 |
+
addScript(i);
|
47 |
+
} else {
|
48 |
+
".$this->_getFormatedInlineJsCode()
|
49 |
+
. " }
|
50 |
+
}
|
51 |
+
script.setAttribute('src',scripts[i]);
|
52 |
+
document.getElementsByTagName(\"head\")[0].appendChild(script);
|
53 |
+
}
|
54 |
+
function downloadJSAtOnload(){
|
55 |
+
var i = 0;
|
56 |
+
addScript(i);
|
57 |
+
}
|
58 |
+
})();"
|
59 |
+
. '//]]>'
|
60 |
+
. '</script>';
|
61 |
+
} else {
|
62 |
+
$script = implode("\n",$this->_allJs)."\n";
|
63 |
+
}
|
64 |
+
|
65 |
+
return $script;
|
66 |
+
}
|
67 |
+
protected function _getFormatedInlineJsCode(){
|
68 |
+
$code = implode("\n;",$this->_inlineScripts);
|
69 |
+
return str_replace(array('<![CDATA[',']]>'), '', $code);
|
70 |
+
}
|
71 |
+
protected function _replaceCallback($matches) {
|
72 |
+
$this->_allJs[] = $matches[0];
|
73 |
+
return '';
|
74 |
+
}
|
75 |
+
protected function _replaceCallbackDefer($matches) {
|
76 |
+
if (isset($matches[2]) && trim($matches[2])) {
|
77 |
+
$this->_inlineScripts[] = $matches[2];
|
78 |
+
} elseif(strpos($matches[1],' src=')) {
|
79 |
+
preg_match('#(.*?)src="(.*)"#is',$matches[1],$src);
|
80 |
+
$this->_jsScriptsSrc[] = $src[2];
|
81 |
+
$this->_jsScripts[] = $matches[0];
|
82 |
+
} else {
|
83 |
+
Mage::logException(new Exception('Unknown script type :'.$matches[0]));
|
84 |
+
return $matches[0];
|
85 |
+
}
|
86 |
+
return '';
|
87 |
+
}
|
88 |
+
|
89 |
+
public function afterToHtml($observer) {
|
90 |
+
if(Mage::getStoreConfig(self::MODULE_ENABLED)){
|
91 |
+
if ($observer->getBlock()->getNameInLayout() == 'root'){
|
92 |
+
$object = $observer->getTransport();
|
93 |
+
if(Mage::getStoreConfig(self::DEFER_JS)){
|
94 |
+
$calback = array($this, '_replaceCallbackDefer');
|
95 |
+
} else {
|
96 |
+
$calback = array($this, '_replaceCallback');
|
97 |
+
}
|
98 |
+
$result = preg_replace_callback(
|
99 |
+
array('#<script(.*?)>(.*?)</script>#is', '#<script(.*?)/>#is'),
|
100 |
+
$calback, $object->getHtml()
|
101 |
+
);
|
102 |
+
$script = $this->_getResultScript();
|
103 |
+
$result = str_replace('</body>', $script.'</body>', $result);
|
104 |
+
$object->setHtml($result);
|
105 |
+
}
|
106 |
+
}
|
107 |
+
}
|
108 |
+
}
|
app/code/local/Radishok/LazyJs/etc/config.xml
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<!--
|
3 |
+
/**
|
4 |
+
* Radishok_LazyJs
|
5 |
+
*
|
6 |
+
* NOTICE OF LICENSE
|
7 |
+
*
|
8 |
+
* This source file is subject to the Open Software License (OSL 3.0)
|
9 |
+
* that is bundled with this package in the file LICENSE.txt.
|
10 |
+
* It is also available through the world-wide-web at this URL:
|
11 |
+
* http://opensource.org/licenses/osl-3.0.php
|
12 |
+
* If you did not receive a copy of the license and are unable to
|
13 |
+
* obtain it through the world-wide-web, please send an email
|
14 |
+
* to license@magentocommerce.com so we can send you a copy immediately.
|
15 |
+
*
|
16 |
+
* @category Radishok
|
17 |
+
* @package Radishok_LazyJs
|
18 |
+
* @copyright Copyright (c) 2014 Radishok
|
19 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
20 |
+
*/
|
21 |
+
-->
|
22 |
+
<config>
|
23 |
+
<modules>
|
24 |
+
<Radishok_LazyJs>
|
25 |
+
<version>0.0.0.1</version>
|
26 |
+
</Radishok_LazyJs>
|
27 |
+
</modules>
|
28 |
+
<global>
|
29 |
+
<models>
|
30 |
+
<radishok_lazyjs>
|
31 |
+
<class>Radishok_LazyJs_Model</class>
|
32 |
+
</radishok_lazyjs>
|
33 |
+
</models>
|
34 |
+
</global>
|
35 |
+
<frontend>
|
36 |
+
<events>
|
37 |
+
<core_block_abstract_to_html_after>
|
38 |
+
<observers>
|
39 |
+
<radishok_lazyjs_after_html>
|
40 |
+
<class>radishok_lazyjs/observer</class>
|
41 |
+
<method>afterToHtml</method>
|
42 |
+
</radishok_lazyjs_after_html>
|
43 |
+
</observers>
|
44 |
+
</core_block_abstract_to_html_after>
|
45 |
+
</events>
|
46 |
+
</frontend>
|
47 |
+
<adminhtml>
|
48 |
+
<acl>
|
49 |
+
<resources>
|
50 |
+
<admin>
|
51 |
+
<children>
|
52 |
+
<system>
|
53 |
+
<children>
|
54 |
+
<config>
|
55 |
+
<children>
|
56 |
+
<radishoklazyjs>
|
57 |
+
<title>Radishok</title>
|
58 |
+
</radishoklazyjs>
|
59 |
+
</children>
|
60 |
+
</config>
|
61 |
+
</children>
|
62 |
+
</system>
|
63 |
+
</children>
|
64 |
+
</admin>
|
65 |
+
</resources>
|
66 |
+
</acl>
|
67 |
+
</adminhtml>
|
68 |
+
<default>
|
69 |
+
<radishoklazyjs>
|
70 |
+
<settings>
|
71 |
+
<active>1</active>
|
72 |
+
<defer_js>0</defer_js>
|
73 |
+
</settings>
|
74 |
+
</radishoklazyjs>
|
75 |
+
</default>
|
76 |
+
</config>
|
app/code/local/Radishok/LazyJs/etc/system.xml
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<!--
|
3 |
+
/**
|
4 |
+
* Magento
|
5 |
+
*
|
6 |
+
* NOTICE OF LICENSE
|
7 |
+
*
|
8 |
+
* This source file is subject to the Open Software License (OSL 3.0)
|
9 |
+
* that is bundled with this package in the file LICENSE.txt.
|
10 |
+
* It is also available through the world-wide-web at this URL:
|
11 |
+
* http://opensource.org/licenses/osl-3.0.php
|
12 |
+
* If you did not receive a copy of the license and are unable to
|
13 |
+
* obtain it through the world-wide-web, please send an email
|
14 |
+
* to license@magentocommerce.com so we can send you a copy immediately.
|
15 |
+
*
|
16 |
+
* @category Phoenix
|
17 |
+
* @package Phoenix_Moneybookers
|
18 |
+
* @copyright Copyright (c) 2014 Phoenix Medien GmbH & Co. KG (http://www.phoenix-medien.de)
|
19 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
20 |
+
*/
|
21 |
+
-->
|
22 |
+
<config>
|
23 |
+
<tabs>
|
24 |
+
<radishok>
|
25 |
+
<label>Radishok</label>
|
26 |
+
<sort_order>201</sort_order>
|
27 |
+
</radishok>
|
28 |
+
</tabs>
|
29 |
+
<sections>
|
30 |
+
<radishoklazyjs>
|
31 |
+
<label>LazyJs</label>
|
32 |
+
<tab>radishok</tab>
|
33 |
+
<frontend_type>text</frontend_type>
|
34 |
+
<sort_order>10</sort_order>
|
35 |
+
<show_in_default>1</show_in_default>
|
36 |
+
<show_in_website>1</show_in_website>
|
37 |
+
<show_in_store>1</show_in_store>
|
38 |
+
<groups>
|
39 |
+
<settings>
|
40 |
+
<label>Settings</label>
|
41 |
+
<frontend_type>text</frontend_type>
|
42 |
+
<sort_order>10</sort_order>
|
43 |
+
<show_in_default>1</show_in_default>
|
44 |
+
<show_in_website>1</show_in_website>
|
45 |
+
<show_in_store>1</show_in_store>
|
46 |
+
<fields>
|
47 |
+
<active>
|
48 |
+
<label>Module Enabled</label>
|
49 |
+
<frontend_type>select</frontend_type>
|
50 |
+
<source_model>adminhtml/system_config_source_yesno</source_model>
|
51 |
+
<sort_order>10</sort_order>
|
52 |
+
<show_in_default>1</show_in_default>
|
53 |
+
<show_in_website>1</show_in_website>
|
54 |
+
<show_in_store>1</show_in_store>
|
55 |
+
</active>
|
56 |
+
<defer_js>
|
57 |
+
<label>Defer js</label>
|
58 |
+
<frontend_type>select</frontend_type>
|
59 |
+
<source_model>adminhtml/system_config_source_yesno</source_model>
|
60 |
+
<sort_order>20</sort_order>
|
61 |
+
<show_in_default>1</show_in_default>
|
62 |
+
<show_in_website>1</show_in_website>
|
63 |
+
<show_in_store>1</show_in_store>
|
64 |
+
<comment><![CDATA[No = do not defer js, only move javascript code before </body> ends]]></comment>
|
65 |
+
</defer_js>
|
66 |
+
</fields>
|
67 |
+
</settings>
|
68 |
+
</groups>
|
69 |
+
</radishoklazyjs>
|
70 |
+
</sections>
|
71 |
+
</config>
|
app/etc/modules/Radishok_LazyJs.xml
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<!--
|
3 |
+
/**
|
4 |
+
* Radishok_LazyJs
|
5 |
+
*
|
6 |
+
* NOTICE OF LICENSE
|
7 |
+
*
|
8 |
+
* This source file is subject to the Open Software License (OSL 3.0)
|
9 |
+
* that is bundled with this package in the file LICENSE.txt.
|
10 |
+
* It is also available through the world-wide-web at this URL:
|
11 |
+
* http://opensource.org/licenses/osl-3.0.php
|
12 |
+
* If you did not receive a copy of the license and are unable to
|
13 |
+
* obtain it through the world-wide-web, please send an email
|
14 |
+
* to license@magentocommerce.com so we can send you a copy immediately.
|
15 |
+
*
|
16 |
+
* @category Radishok
|
17 |
+
* @package Radishok_LazyJs
|
18 |
+
* @copyright Copyright (c) 2014 Radishok
|
19 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
20 |
+
*/
|
21 |
+
-->
|
22 |
+
<config>
|
23 |
+
<modules>
|
24 |
+
<Radishok_LazyJs>
|
25 |
+
<active>true</active>
|
26 |
+
<codePool>local</codePool>
|
27 |
+
</Radishok_LazyJs>
|
28 |
+
</modules>
|
29 |
+
</config>
|
package.xml
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<package>
|
3 |
+
<name>Radishok_LazyJs</name>
|
4 |
+
<version>0.0.0.1</version>
|
5 |
+
<stability>stable</stability>
|
6 |
+
<license uri="http://opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
|
7 |
+
<channel>community</channel>
|
8 |
+
<extends/>
|
9 |
+
<summary>This Extension will speed up frontend rendering by placing JavaScript at the bottom of the page </summary>
|
10 |
+
<description>When you put your JavaScript at the top of the page, the browser will start loading your JS files before the markup, images and text. And since browsers load JavaScript synchronously, nothing else will load while the JavaScript is loading. So there will be a timeframe of a few seconds where the user will see a blank page, while the JavaScript is loading. On the other hand, if you place your JavaScript at the bottom of the page, the user will see the page loading first, and after that the JavaScript will load in the background. So if for example your CSS & HTML takes 5 seconds to load, and your JavaScript takes another 5 seconds, putting our JavaScript on the top of the page will give the user a “perceived” loading time of 10 seconds, and putting it on the bottom will give a “perceived” loading time of 5 seconds.</description>
|
11 |
+
<notes>Be careful using option "Defer js"</notes>
|
12 |
+
<authors><author><name>Denis Derbenyov</name><user>radishok</user><email>ddenisiy@gmail.com</email></author></authors>
|
13 |
+
<date>2014-07-20</date>
|
14 |
+
<time>10:00:33</time>
|
15 |
+
<contents><target name="magelocal"><dir name="Radishok"><dir name="LazyJs"><dir name="Model"><file name="Observer.php" hash="5d01b427ac031039a0e4ed072dcd051e"/></dir><dir name="etc"><file name="config.xml" hash="3244a321790fa95f8b0a11d77fa86ca9"/><file name="system.xml" hash="e03e1ae13a6ccc422a4954fd6a3edf95"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Radishok_LazyJs.xml" hash="594198ce4094a6ac5c47c7d99f36d5d3"/></dir></target></contents>
|
16 |
+
<compatible/>
|
17 |
+
<dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
|
18 |
+
</package>
|