final kaoss |
05-25-2012 11:05 PM |
Well for those of you using vb supercharged, I've managed to make it load async which means even faster!
Go to your AdminCP --> Plugins & Products --> Plugin Manager
Find Product vB4: SuperCharged
Open JS Optimiser
Replace all of the code in there with this, then save.
Code:
if($vbulletin->options['vbsc_js_enable'] == 1){
$c_vbsc = file_exists(DIR."/clientscript/vbsc.js");
$c_vbscsize = $c_vbsc && filesize(DIR."/clientscript/vbsc.js") > 10;
$c_global = $c_vbsc && filemtime(DIR."/clientscript/vbulletin-core.js") < filemtime(DIR."/clientscript/vbsc.js");
if((!$c_vbsc || !$c_vbscsize || !$c_global) && $vbulletin->options['vbsc_js_common'] == 1){
$err = "<strong style='font-size:13px;background-color:#000;padding:5px;color:#ff9000;font-weight:bold;text-align:center;width:100%;display:block;'>";
if(!$c_vbsc){
$err .= "The file ".DIR."/clientscript/vbsc.js is missing. Ensure the file exists (download and extract vbsc.zip again if not) and that you have the file chmodded as per the installation instructions, then run the Compress Javascript under vB4: SuperCharged in your Admin CP. Report any errors you encounter when running Compress Javascript.<br /><br />";
} else if(!$c_vbscsize){
$err .= "The file ".DIR."/clientscript/vbsc.js is empty. Ensure you have the file chmodded as per the installation instructions and run the Compress Javascript under vB4: SuperCharged in your Admin CP. Report any errors you encounter when running Compress Javascript.<br /><br />";
} else if(!$c_global){
$err .= "The ".DIR."/clientscript/vbsc.js cache has a modification time of before ".DIR."/clientscript/vbulletin-core.js. Your cache is almost certainly out of date. Please run Compress Javascript under vB4: SuperCharged in your Admin CP.<br />If you recently updated vBulletin, you should of course run Compress CSS also :) <br />";
}
$err .= "To prevent issues, your forum is currently running without optimised Javascript as a fallback.<br /><br />As a last resort if you cannot get this message to disappear, disable the Javascript Optimiser.</strong>";
$output = $err.$output;
} else {
/*function vbsc_removejs($var){
//return strpos($var, "threadlist") === false;
return true;
}*/
//eval($vbulletin->options['vbsc_js_files']);
//$GLOBALS['js_files'] = array_filter($GLOBALS['js_files'], 'vbsc_removejs');
require_once (DIR . '/includes/vbsc_global.php');
$GLOBALS['js_uncached'] = '';
function in_arrayi( $needle, $haystack ) {
$found = false;
foreach( $haystack as $value ) {
if( strtolower( $value ) == strtolower( $needle ) ) {
$found = true;
}
}
return $found;
}
function vbsc_jsleave($match){
return (/*stripos($match, "FeatureLoader.js.php") !== false ||*/ stripos($match, "google_ad") !== false || stripos($match, "show_ad") !== false || stripos($match, "_google") !== false || stripos($match, "recaptcha") !== false/* || strpos($match, "treeview.css") !== false || strpos($match, "initVbTreeMenus") !== false || strpos($match, "treeview-min.js") !== false || strpos($match, "SHOW_ALL_TREE_ELEMENTS_THRESHOLD") !== false*/);
}
function vbsc_jsinline($matches){
/*if(strpos($matches[0], "google_ad") !== false || strpos($matches[0], "show_ad") !== false){
return $matches[0];
}*/
// workaround for cms sections and pm new window thing
if(stripos($matches[0], "loadVbCss") === false && stripos($matches[0], "getBaseUrl") === false){
if(vbsc_jsleave($matches[0]))
return $matches[0];
// whitelist specific terms, otherwise leave the javascript where it is as it's likely an embedded script such as ads, custom search etc.
if(stripos($matches[0], "YAHOO") === false && stripos($matches[0], "vbulletin") === false && stripos($matches[0], "forum") === false && stripos($matches[0], "vbphrase") === false && stripos($matches[0], "vB_") === false && stripos($matches[0], "inlinemod") === false && stripos($matches[0], "vbseo") === false && stripos($matches[0], "vsa") === false)
return $matches[0];
}
if(strpos($matches[0], "SESSIONURL =") === false){
$GLOBALS['js_uncached'] .= $matches[0];
return '';
} else {
return $matches[0];
}
}
//var_dump($GLOBALS["js_files"]);
function vbsc_jscached($matches){
global $vbulletin;
//var_dump($matches);
//$ret = "";
//var_dump($matches);
if(in_arrayi($matches[2], $GLOBALS["js_files"]) && strpos($matches[1], "<!--") === false && $vbulletin->options['vbsc_js_common'] == 1){
//return "";
} else {
//return $matches[0];
$GLOBALS["js_uncached"] .= $matches[0];
if(strpos($matches[1], "<!--") !== false){
// handle IE conditional comments and normal comments
if(strpos($matches[1], "[if ") === false){
$GLOBALS["js_uncached"] .= " -->";
} else {
$GLOBALS["js_uncached"] .= " <![endif]-->";
}
}
}
if(vbsc_jsleave($matches[0]))
return $matches[0];
//return strpos($matches[1], "if") !== false ? $matches[1] : $ret;
// leave original comment opener in place as closing comment is not covered by the regex
return $matches[1];
}
$output = str_replace(array("<!-- Editor Scripts -->", "<!-- scripts for quick reply and quick edit -->", "<!-- / ajax errors table -->", "<!-- ajax errors table -->", "<!-- lightbox scripts -->
", "<!-- / lightbox scripts -->"), "", $output);
//type="text\/javascript"
// handle potentially cached vBulletin javascript code
$output = preg_replace_callback('/(<!--.*?]>\s+)?<script [^>]*?src=".*?clientscript\/([^?"]+).*?">\s*?<\/script>?/i', 'vbsc_jscached', $output);
// handle YUI treeview code amongst other things
$output = preg_replace_callback('/<script[^>]*?type=[\'"]text\/javascript[\'"] src="http:\/\/yui.yahooapis.com\/.*?">\s*?<\/script>/is', 'vbsc_jsinline', $output);
// handle inline javascript code
$output = preg_replace_callback('/<script[^>]*?type=[\'"]text\/javascript[\'"]>.*?<\/script>/is', 'vbsc_jsinline', $output);
$readyfix = "";
if($vbulletin->options['vbsc_js_common'] != 1){
$readyfix = '<script type="text/javascript">YAHOO.util.Event.onDOMReady(function() {if(YAHOO.vBulletin.vB_XHTML_Ready) return; console.log("Fire vB_XHTML_Ready");vB_XHTML_Ready.fire();YAHOO.vBulletin.vB_XHTML_Ready = true;});</script>';
}
// append vbsc.js
$output = preg_replace('/(<\/body>)/i', ($vbulletin->options['vbsc_js_common'] == 1 ? '<script type="text/javascript" src="clientscript/vbsc.js" async="async"></script>' : '').$GLOBALS["js_uncached"].$readyfix.'$1', $output, 1);
//$output = preg_replace('/<script type="text\/javascript">\s+<!--\s+\/\/ Main vBulletin Javascript Initialization/', '<script type="text/javascript" src="/clientscript/vbsc.js"></script>'.$GLOBALS["js_uncached"].'$1', $output, 1);
//$output = str_replace('SECURITYTOKEN', 'vbsc_SECURITYTOKEN', $output);
}
}
It will now defer the parsing of the vbsc.js file, saving a few hundred KB and increasing your page load time!
|