Skip to content

Instantly share code, notes, and snippets.

@keslo
Last active November 12, 2018 19:31
Show Gist options
  • Save keslo/0fc7f7b5ae9487947d8ef2e30747466d to your computer and use it in GitHub Desktop.
Save keslo/0fc7f7b5ae9487947d8ef2e30747466d to your computer and use it in GitHub Desktop.
LiveReload in MODX Evo (Evo CMS)
global $modx;
// проверяем авторизацию менеджера
if ($_SESSION['mgrInternalKey'] == 1) {
// вешаем запись в localStorage при сохранении чанка
$e = $modx->Event;
if ($e->name == "OnChunkFormRender" || $e->name == "OnTempFormRender") {
$script = '<script>
var btn = document.getElementById("Button1");
btn.addEventListener("click", function() {
localStorage["evo-reload"] = "true";
});
</script>';
$e->output($script);
}
// на страницу фронта добавляем скрипт отслеживания изменений localStorage
if ($e->name == "OnWebPagePrerender") {
// скрипт для страницы
$script = "
<script>
window.addEventListener('storage', function(events) {
if (localStorage['evo-reload'] === 'true') {
localStorage['evo-reload'] = 'false';
setTimeout(function() { location.reload() }, 100);
}
});
</script></body>
";
$output = $modx->documentOutput;
$output = str_replace('</body>', $script, $output);
$modx->documentOutput = $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment