forked from Adleraci/adlerka.top
Fix script on open page
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
<?php
|
||||
function generateScriptData($input) :string
|
||||
{
|
||||
// Convert PHP array to JSON string
|
||||
$jsonString = json_encode($input);
|
||||
function generateScriptData($phpArray) {
|
||||
// Check if the array is associative and single-level
|
||||
if (is_array($phpArray) && count($phpArray) > 0 && count(array_filter(array_keys($phpArray), 'is_string')) === count($phpArray)) {
|
||||
// Generate JavaScript code to save each array element to local storage
|
||||
echo "<script>";
|
||||
foreach ($phpArray as $key => $value) {
|
||||
$escapedKey = addslashes($key); // Escape special characters in the key
|
||||
$escapedValue = addslashes($value); // Escape special characters in the value
|
||||
|
||||
// Output JavaScript code with the JSON string
|
||||
return "<script>let pageData = JSON.parse('$jsonString');</script>";
|
||||
echo "localStorage.setItem('$escapedKey', '$escapedValue');";
|
||||
}
|
||||
echo "</script>";
|
||||
} else {
|
||||
echo "<script>console.error('Invalid PHP array. Must be single-level and associative.');</script>";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user