-
-
Save hereswhatidid/8c8edef106ee95138b03 to your computer and use it in GitHub Desktop.
<p>Some HTML goes here</p> | |
<script type="text/javascript" data-keepinline="true"> | |
// this script will remain here when rendered | |
alert( "hello!" ); | |
</script> | |
<script type="text/javascript"> | |
// this script will be forced to the bottom of the page | |
alert( "hello again!" ); | |
</script> |
<?php | |
Class Media extends MediaCore | |
{ | |
public static function deferScript($matches) | |
{ | |
if (!is_array($matches)) | |
return false; | |
$inline = ''; | |
if (isset($matches[0])) | |
$original = trim($matches[0]); | |
if (isset($matches[1])) | |
$inline = trim($matches[1]); | |
/* This is an inline script, add its content to inline scripts stack then remove it from content */ | |
if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline) | |
return ''; | |
/* This is an external script, if it already belongs to js_files then remove it from content */ | |
preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results); | |
if (isset($results[1]) && (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src))) | |
return ''; | |
/* return original string because no match was found */ | |
return $original; | |
} | |
} |
hello gRoussac there is one problem with Your commit original code written by hereswhatidid do keep-inline as well for external js code and that was important for code like conversion sale form gogole, i developed many modules which i sell in addons marketplace and now their are not working at all because of that changes you made :-) can you add as well to keep-inline work on external files.
Hi everybody !
I am using prestashop 1.6.1.4. And these codes don't work for me.
The most important for me it's this code :
public static function packJSinHTMLpregCallback($preg_matches)
{
if (!(trim($preg_matches[2])))
return $preg_matches[0];
$preg_matches[1] = $preg_matches[1].'/* <![CDATA[ */';
if (preg_match('/<\s*script.*(data-nocompress)[^>]*>/ims', $preg_matches[1]) !== 0) {
$preg_matches[2] = preg_replace('/\n?\s*\/\*\s*\*\/\n?/ims', "\n", $preg_matches[2]);
} else {
$preg_matches[2] = Media::packJS($preg_matches[2]);
}
$preg_matches[count($preg_matches) - 1] = '/* ]]> */'.$preg_matches[count($preg_matches) - 1];
unset($preg_matches[0]);
$output = implode('', $preg_matches);
return $output;
}
By
javiBertos
I have overrided the Media class and deleted the index cache but when I add a script like Google Tag Manager or another, it doesn't work.
When I want to call the function I do :
<script data-nocompress="true" >
[...]
</script>
May I have help please !
Thank you so much...
Hi,
With regards to the script below, is it necessary? Also how do we know that the script already belongs to js_files? Which file/folder should we refer to?
/* This is an external script, if it already belongs to js_files then remove it from content */ preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results); if (array_key_exists(1, $results)) { if (substr($results[1], 0, 2) == '//') { $protocol_link = Tools::getCurrentUrlProtocolPrefix(); $results[1] = $protocol_link.ltrim($results[1], '/'); } if (preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0) { if (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src)) return ''; } else { Context::getContext()->controller->removeJS($results[1]); } }
In PS 1.6.1.11 it seems a set of empty script tags are created at the end for me.
<script type="text/javascript">
<script>
</script>
I can not seem to find override/classes/Media.php
in my wordpress project.
Is this directory specific to the prestashop theme? As I'm having this issue with the Scalia theme, and can not find where to place this code.
If you want to prevent to compress a block of JS code to prevent problems (f.e. google retargeting code), I've added this code to the Media override class:
And in your script line, you must add data-nocompress="true"
I hope it would be helpful for you.
Regards!