Radishok_LazyJs - Version 0.0.0.6

Version Notes

Skip moving google-analytics to footer

Download this release

Release Info

Developer Denis Derbenyov
Extension Radishok_LazyJs
Version 0.0.0.6
Comparing to
See all releases


Code changes from version 0.0.0.5 to 0.0.0.6

app/code/local/Radishok/LazyJs/Model/Observer.php CHANGED
@@ -1,4 +1,5 @@
1
  <?php
 
2
  /**
3
  * Radishok_LazyJs
4
  *
@@ -17,17 +18,19 @@
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
- const DISABLED_PAGES = 'radishoklazyjs/settings/disabled_pages';
25
- protected $_allJs = array();
26
- protected $_jsScripts = array();
27
-
28
- protected function _getResultScript(){
29
- if(Mage::getStoreConfig(self::DEFER_JS)){
30
- $script =
31
  '<script type="text/javascript">'
32
  . "//<![CDATA[
33
  ;(function(){
@@ -37,7 +40,7 @@ class Radishok_LazyJs_Model_Observer extends Varien_Object{
37
  window.attachEvent(\"onload\", downloadJSAtOnload);
38
  else window.onload = downloadJSAtOnload;
39
 
40
- var scripts = ".Mage::helper('core')->jsonEncode($this->_jsScripts).";
41
  function addScript(i){
42
  var script = document.createElement('script');
43
  script.type = 'text/javascript';
@@ -65,75 +68,86 @@ class Radishok_LazyJs_Model_Observer extends Varien_Object{
65
  . '//]]>'
66
  . '</script>';
67
  } else {
68
- $script = implode("\n",$this->_allJs)."\n";
69
  }
70
-
71
  return $script;
72
  }
73
- protected function _getFormatedInlineJsCode(){
74
- $code = implode("\n;",$this->_inlineScripts);
75
- return str_replace(array('<![CDATA[',']]>'), '', $code);
 
 
76
  }
77
- protected function _replaceCallback($matches) {
 
 
78
  $this->_allJs[] = $matches[0];
79
  return '';
80
  }
81
- protected function _replaceCallbackDefer($matches) {
 
 
82
  $script = array();
83
  if (isset($matches[2]) && trim($matches[2])) {
84
  $script['inline'] = true;
85
  $script['text'] = $matches[2];
86
- } elseif(strpos($matches[1],' src=')) {
87
  $src = '';
88
- preg_match('#(.*?)src="(.*)"#is',$matches[1],$src);
89
  $script['inline'] = false;
90
  $script['src'] = $src[2];
91
  } else {
92
- Mage::logException(new Exception('Unknown script type :'.$matches[0]));
93
  return $matches[0];
94
  }
95
  $this->_jsScripts[] = $script;
96
  return '';
97
  }
98
- protected function _replaceCallbackMain($matches) {
99
- if(strpos($matches[0],'<!--[if')===false && strpos($matches[0],'<![endif]-->')===false){
100
- if(Mage::getStoreConfig(self::DEFER_JS)){
 
 
 
101
  $callback = array($this, '_replaceCallbackDefer');
102
  } else {
103
  $callback = array($this, '_replaceCallback');
104
  }
105
- return call_user_func($callback,$matches);
106
  }
107
  return $matches[0];
108
  }
109
- public function afterToHtml($observer) {
110
- if ($observer->getBlock()->getNameInLayout() == 'root' && Mage::getStoreConfig(self::MODULE_ENABLED)){
 
 
111
  $request = Mage::app()->getRequest();
112
  $e = $request->getRequestString();
113
- $disabled_pages = explode("\n",Mage::getStoreConfig(self::DISABLED_PAGES));
114
  $object = $observer->getTransport();
115
- foreach($disabled_pages as $disabled_page){
116
- if(substr($disabled_page,-1)==='*'){
117
- $url = substr($disabled_page,0,-1);
118
- if(stripos($e, $url)===0){
119
  return;
120
  }
121
  } else {
122
- if($e===$disabled_page){
123
  return;
124
  }
125
  }
126
  }
127
- if(strripos($object->getHtml(),'</body>')===false){
128
  return;
129
  }
130
-
131
  $result = preg_replace_callback(
132
- array('#<!--\[if(.*?)\]>(.*?)<!\[endif\]-->|<script(.*?)>(.*?)</script>|<script(.*?)/>#is'),
133
- array($this,'_replaceCallbackMain'), $object->getHtml()
134
  );
135
  $script = $this->_getResultScript();
136
- $object->setHtml(str_replace('</body>', $script.'</body>', $result));
137
  }
138
  }
139
  }
1
  <?php
2
+
3
  /**
4
  * Radishok_LazyJs
5
  *
18
  * @copyright Copyright (c) 2014 Radishok
19
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
20
  */
21
+ class Radishok_LazyJs_Model_Observer extends Varien_Object
22
+ {
23
+ const MODULE_ENABLED = 'radishoklazyjs/settings/active';
24
+ const DEFER_JS = 'radishoklazyjs/settings/defer_js';
25
+ const DISABLED_PAGES = 'radishoklazyjs/settings/disabled_pages';
26
+ const SKIP_PHRASE_JS = 'radishoklazyjs/settings/skip_phrase_js';
27
+ protected $_allJs = array();
28
+ protected $_jsScripts = array();
29
 
30
+ protected function _getResultScript()
31
+ {
32
+ if (Mage::getStoreConfig(self::DEFER_JS)) {
33
+ $script =
 
 
 
 
 
 
34
  '<script type="text/javascript">'
35
  . "//<![CDATA[
36
  ;(function(){
40
  window.attachEvent(\"onload\", downloadJSAtOnload);
41
  else window.onload = downloadJSAtOnload;
42
 
43
+ var scripts = " . Mage::helper('core')->jsonEncode($this->_jsScripts) . ";
44
  function addScript(i){
45
  var script = document.createElement('script');
46
  script.type = 'text/javascript';
68
  . '//]]>'
69
  . '</script>';
70
  } else {
71
+ $script = implode("\n", $this->_allJs) . "\n";
72
  }
73
+
74
  return $script;
75
  }
76
+
77
+ protected function _getFormatedInlineJsCode()
78
+ {
79
+ $code = implode("\n;", $this->_inlineScripts);
80
+ return str_replace(array('<![CDATA[', ']]>'), '', $code);
81
  }
82
+
83
+ protected function _replaceCallback($matches)
84
+ {
85
  $this->_allJs[] = $matches[0];
86
  return '';
87
  }
88
+
89
+ protected function _replaceCallbackDefer($matches)
90
+ {
91
  $script = array();
92
  if (isset($matches[2]) && trim($matches[2])) {
93
  $script['inline'] = true;
94
  $script['text'] = $matches[2];
95
+ } elseif (strpos($matches[1], ' src=')) {
96
  $src = '';
97
+ preg_match('#(.*?)src="(.*)"#is', $matches[1], $src);
98
  $script['inline'] = false;
99
  $script['src'] = $src[2];
100
  } else {
101
+ Mage::logException(new Exception('Unknown script type :' . $matches[0]));
102
  return $matches[0];
103
  }
104
  $this->_jsScripts[] = $script;
105
  return '';
106
  }
107
+
108
+ protected function _replaceCallbackMain($matches)
109
+ {
110
+ if ((stripos($matches[0], '<!--[if') === false && stripos($matches[0], '<![endif]-->') === false)
111
+ && stripos($matches[0], Mage::getStoreConfig(self::SKIP_PHRASE_JS)) === false) {
112
+ if (Mage::getStoreConfig(self::DEFER_JS)) {
113
  $callback = array($this, '_replaceCallbackDefer');
114
  } else {
115
  $callback = array($this, '_replaceCallback');
116
  }
117
+ return call_user_func($callback, $matches);
118
  }
119
  return $matches[0];
120
  }
121
+
122
+ public function afterToHtml($observer)
123
+ {
124
+ if ($observer->getBlock()->getNameInLayout() == 'root' && Mage::getStoreConfig(self::MODULE_ENABLED)) {
125
  $request = Mage::app()->getRequest();
126
  $e = $request->getRequestString();
127
+ $disabled_pages = explode("\n", Mage::getStoreConfig(self::DISABLED_PAGES));
128
  $object = $observer->getTransport();
129
+ foreach ($disabled_pages as $disabled_page) {
130
+ if (substr($disabled_page, -1) === '*') {
131
+ $url = substr($disabled_page, 0, -1);
132
+ if (stripos($e, $url) === 0) {
133
  return;
134
  }
135
  } else {
136
+ if ($e === $disabled_page) {
137
  return;
138
  }
139
  }
140
  }
141
+ if (strripos($object->getHtml(), '</body>') === false) {
142
  return;
143
  }
144
+
145
  $result = preg_replace_callback(
146
+ array('#<!--\[if(.*?)\]>(.*?)<!\[endif\]-->|<script(.*?)>(.*?)</script>|<script(.*?)/>#is'),
147
+ array($this, '_replaceCallbackMain'), $object->getHtml()
148
  );
149
  $script = $this->_getResultScript();
150
+ $object->setHtml(str_replace('</body>', $script . '</body>', $result));
151
  }
152
  }
153
  }
app/code/local/Radishok/LazyJs/etc/config.xml CHANGED
@@ -22,7 +22,7 @@
22
  <config>
23
  <modules>
24
  <Radishok_LazyJs>
25
- <version>0.0.0.5</version>
26
  </Radishok_LazyJs>
27
  </modules>
28
  <global>
@@ -70,6 +70,7 @@
70
  <settings>
71
  <active>1</active>
72
  <defer_js>0</defer_js>
 
73
  </settings>
74
  </radishoklazyjs>
75
  </default>
22
  <config>
23
  <modules>
24
  <Radishok_LazyJs>
25
+ <version>0.0.0.6</version>
26
  </Radishok_LazyJs>
27
  </modules>
28
  <global>
70
  <settings>
71
  <active>1</active>
72
  <defer_js>0</defer_js>
73
+ <skip_phrase_js>google-analytics</skip_phrase_js>
74
  </settings>
75
  </radishoklazyjs>
76
  </default>
app/code/local/Radishok/LazyJs/etc/system.xml CHANGED
@@ -45,7 +45,7 @@
45
  <show_in_store>1</show_in_store>
46
  <fields>
47
  <active>
48
- <label>Module Enabled(version: 0.0.0.5)</label>
49
  <frontend_type>select</frontend_type>
50
  <source_model>adminhtml/system_config_source_yesno</source_model>
51
  <sort_order>10</sort_order>
@@ -92,6 +92,15 @@
92
  <show_in_store>1</show_in_store>
93
  <comment><![CDATA[Each request path on new line<br/>Example: <br/>/contacts<br/>/checkout/cart*<br/>(* character can be used at the end of line)]]></comment>
94
  </disabled_pages>
 
 
 
 
 
 
 
 
 
95
  </fields>
96
  </settings>
97
  </groups>
45
  <show_in_store>1</show_in_store>
46
  <fields>
47
  <active>
48
+ <label>Module Enabled(version: 0.0.0.6)</label>
49
  <frontend_type>select</frontend_type>
50
  <source_model>adminhtml/system_config_source_yesno</source_model>
51
  <sort_order>10</sort_order>
92
  <show_in_store>1</show_in_store>
93
  <comment><![CDATA[Each request path on new line<br/>Example: <br/>/contacts<br/>/checkout/cart*<br/>(* character can be used at the end of line)]]></comment>
94
  </disabled_pages>
95
+ <skip_phrase_js>
96
+ <label>Skip moving scripts which contains</label>
97
+ <frontend_type>text</frontend_type>
98
+ <sort_order>40</sort_order>
99
+ <show_in_default>1</show_in_default>
100
+ <show_in_website>1</show_in_website>
101
+ <show_in_store>1</show_in_store>
102
+ <comment><![CDATA[Default is 'google-analytics']]></comment>
103
+ </skip_phrase_js>
104
  </fields>
105
  </settings>
106
  </groups>
package.xml CHANGED
@@ -1,18 +1,18 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Radishok_LazyJs</name>
4
- <version>0.0.0.5</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 &amp; 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 &#x201C;perceived&#x201D; loading time of 10 seconds, and putting it on the bottom will give a &#x201C;perceived&#x201D; loading time of 5 seconds.</description>
11
- <notes>Skipping &lt;!--[if ...]&gt; &lt;![endif]--&gt; condition</notes>
12
  <authors><author><name>Denis Derbenyov</name><user>radishok</user><email>ddenisiy@gmail.com</email></author></authors>
13
- <date>2014-09-09</date>
14
- <time>19:49:19</time>
15
- <contents><target name="magelocal"><dir name="Radishok"><dir name="LazyJs"><dir name="Model"><file name="Observer.php" hash="c0719d0cb714c1036fc3a5c739f4164e"/></dir><dir name="etc"><file name="config.xml" hash="35a3fe5b2f42d2c9b27af4939b02150f"/><file name="system.xml" hash="ef7a3a00521f9dbc0637438e591f17c6"/></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>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Radishok_LazyJs</name>
4
+ <version>0.0.0.6</version>
5
  <stability>stable</stability>
6
+ <license>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 &amp; 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 &#x201C;perceived&#x201D; loading time of 10 seconds, and putting it on the bottom will give a &#x201C;perceived&#x201D; loading time of 5 seconds.</description>
11
+ <notes>Skip moving google-analytics to footer</notes>
12
  <authors><author><name>Denis Derbenyov</name><user>radishok</user><email>ddenisiy@gmail.com</email></author></authors>
13
+ <date>2015-06-27</date>
14
+ <time>10:52:23</time>
15
+ <contents><target name="magelocal"><dir name="Radishok"><dir name="LazyJs"><dir name="Model"><file name="Observer.php" hash="c68065256d7c4b297d20a43c1b947d55"/></dir><dir name="etc"><file name="config.xml" hash="735c8e650655529373bcd01a9df36cfb"/><file name="system.xml" hash="774de6a70397eb66b7e7dda38987b38c"/></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>