firstChild
дает вам текстовый узел с текстом скрипта. Вы можете использовать его свойство nodeValue
( spec , MDN ) для получения и установки текста:
const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{"foo": 1}'; // Completely replace it
console.log(script.firstChild.nodeValue);
Live Пример:
const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{"foo": 1}'; // Completely replace it
console.log(script.firstChild.nodeValue);
<script id="myjsonid" type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": "<%- config.url + url_for(path) %>",
"headline": "<%- page.title %>",
"datePublished": "<%= page.date %>",
"dateModified": "<%= page.updated %>",
"image": "<%= page.thumbnail %>",
"author": {
"@type": "Person",
"name": "<%= config.author %>",
"image": {
"@type": "ImageObject",
"url": "<%= theme.img.avatar %>"
},
"description": "<%- theme.uiux.slogan %>"
},
"publisher": {
"@type": "Organization",
"name": "<%= config.title %>",
"logo": {
"@type": "ImageObject",
"url": "<%= theme.head.high_res_favicon %>"
}
},
"keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>",
"description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>"
}
</script>
Вы также можете использовать textContent
( spec , MDN ) или innerText
(spec , MDN ) на самом элементе script
:
const script = document.getElementById("myjsonid");
script.firstChild.nodeValue = '{}'; // Completely replace it
console.log(script.firstChild.nodeValue);
Live Пример:
const script = document.getElementById("myjsonid");
script.textContent = '{"foo": 1}'; // Completely replace it
console.log(script.firstChild.nodeValue);
script.innerText = '{"foo": 2}'; // Completely replace it
console.log(script.firstChild.nodeValue);
<script id="myjsonid" type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": "<%- config.url + url_for(path) %>",
"headline": "<%- page.title %>",
"datePublished": "<%= page.date %>",
"dateModified": "<%= page.updated %>",
"image": "<%= page.thumbnail %>",
"author": {
"@type": "Person",
"name": "<%= config.author %>",
"image": {
"@type": "ImageObject",
"url": "<%= theme.img.avatar %>"
},
"description": "<%- theme.uiux.slogan %>"
},
"publisher": {
"@type": "Organization",
"name": "<%= config.title %>",
"logo": {
"@type": "ImageObject",
"url": "<%= theme.head.high_res_favicon %>"
}
},
"keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>",
"description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>"
}
</script>
В комментарии вы сказали, что хотите изменить одну конкретную часть структуры. Для этого используйте JSON.parse
в строке, чтобы получить дерево объектов для JSON, внесите изменения в это дерево, а затем используйте JSON.stringify
, чтобы получить строку JSON для обратной записи в элемент script
:
const script = document.getElementById("myjsonid");
const obj = JSON.parse(script.firstChild.nodeValue);
obj.image = "***THIS IS THE UPDATE***";
script.firstChild.nodeValue = JSON.stringify(obj);
console.log(script.firstChild.nodeValue);
Live Пример:
const script = document.getElementById("myjsonid");
const obj = JSON.parse(script.firstChild.nodeValue);
obj.image = "***THIS IS THE UPDATE***";
script.firstChild.nodeValue = JSON.stringify(obj);
console.log(script.firstChild.nodeValue);
<script id="myjsonid" type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": "<%- config.url + url_for(path) %>",
"headline": "<%- page.title %>",
"datePublished": "<%= page.date %>",
"dateModified": "<%= page.updated %>",
"image": "<%= page.thumbnail %>",
"author": {
"@type": "Person",
"name": "<%= config.author %>",
"image": {
"@type": "ImageObject",
"url": "<%= theme.img.avatar %>"
},
"description": "<%- theme.uiux.slogan %>"
},
"publisher": {
"@type": "Organization",
"name": "<%= config.title %>",
"logo": {
"@type": "ImageObject",
"url": "<%= theme.head.high_res_favicon %>"
}
},
"keywords": "<% if(page.tags && page.tags.each) { page.tags.each(function(tag) { %><%- tag.name + ',' %><% })} %><%= theme.head.keywords %>",
"description": "<% if(page.description) { %><%= page.description %><% } else if(page.excerpt){ %><%= strip_html(page.excerpt).replace(/^s*/, '').replace(/s*$/, '') %><% } else if (config.description){ %><%= config.description %><% } %>"
}
</script>