I am Using Joomla CMS, and I want to manipulate the DOM in render. I want to modify the inline scripts to execute when the page has loaded. But I have problems with a Google maps script that has HTML tags in its code, when I try to get the content of the script using nodeValue, it cuts it off at the first closing of the </div>; I also tried not to modify anything just load the HTML and then save it with saveHTML() and the same problem happens.
Part of page code (page.html)
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<script src="https://maps.googleapis.com/maps/api/js?key=XXXXXX" type="text/javascript"></script>
</head>
<body>
<header></header>
<main>
<div class="wrapper">
<div class="inner">
<div class="map-container">
<div class="google-map">
<div id="map-canvas95" class="map-canvas" style="width:100%;height:400px">
<script type="text/javascript">
//API demos Used(synchronous loading, info window,)
var myLatlng95 = new google.maps.LatLng(-11.926755,-77.05359);
var mapOptions95 = {
scrollwheel: false,
zoom: 15,
center: myLatlng95,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.LEFT_TOP
},
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: true,
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
}
};
var map95 = new google.maps.Map(document.getElementById('map-canvas95'), mapOptions95);
//Info Window
var contentString95 = '<div id="content">'+
'<div id="siteNotice"></div>'+
'<h1 id="firstHeading" class="firstHeading">Market Title</h1>'+
'<div id="bodyContent">'+
'<p>Graphic Design. Lorem ipsum dolor sit amet, consectetur adipiscing elit. of Quisque ultricies vestibulum molestie.</p>'+
'</div>'+
'</div>';
var infowindow95 = new google.maps.InfoWindow({
content: contentString95,
maxWidth: 300
});
//Marker
var marker95 = new google.maps.Marker({
position: myLatlng95,
map: map95,
title: 'Market Title' });
//Event for open Info Window
google.maps.event.addListener(marker95, 'click', function() {
infowindow95.open(map95,marker95);
});
</script>
</div>
</div>
</div>
</div>
</div>
</main>
<footer></footer>
</body>
</html>
My DOM Script
//$app = JFactory::getApplication('site'); for Joomla only
//$body = $app->getBody(false);//for Joomla only
$body = file_get_contents('page.html');
$domDoc = new DOMdocument();
libxml_use_internal_errors(true);
$domDoc->loadHTML($body);
libxml_clear_errors();
$scripts =$domDoc->getElementsByTagName('script');
$openScript = "\nwindow.addEventListener('DOMContentLoaded', function() {";
$closeScript = "});\n";
foreach ($scripts as $script) {
$newScript = $openScript.$script->nodeValue.$closeScript;
$script->nodeValue = (!empty($script->nodeValue)) ? $newScript : $script->nodeValue;
}
//$body = $domDoc->saveHTML(); for Joomla
//$app->setBody($body);for Joomla
$domDoc->saveHTMLFIle('newDom.html');
In result file you can see script cutted
<script>
window.addEventListener('DOMContentLoaded', function() {
var contentString<?php echo $uniqid; ?> = '<div id="content">'+
'<div id="siteNotice"></div>;
});
</script>
Like this Google maps script, I have seen other scripts that have HTML tags in their code. How do I solve this?. Thanks.
</occurring within the content of a script element, should be escaped as<\/, to avoid any parser problems. I am guessing the occurrence of</trips the PHP DOM parser up as well here. “I want to modify the inline scripts to execute when the page has loaded.” - can you explain which way exactly you are doing that? The code indicates that you are trying to “clone” the script element, but what are you then eventually doing with it?$bodyvalue? This is important to the minimal reproducible example.