Я получаю строку:
'<script class="heyo">document.write("hello")<\/script>'
Я должен создать с помощью VanillaJS встроенный элемент сценария.
Есть ли способ запустить выполнение скрипта?
Работает, но слишком сложно:
const scriptString = '<script class="heyo">document.write("hello")<\/script>';
document.body.insertAdjacentHTML('beforeend', scriptString);
const pseudoScript = document.body.querySelector('.heyo');
const newScriptEl = document.createElement('script');
[...pseudoScript.attributes].forEach(attr => {
newScriptEl.setAttribute(attr.nodeName, attr.nodeValue);
});
newScriptEl.innerHTML = pseudoScript.text;
document.body.append(newScriptEl);
pseudoScript.remove();
Любые лучшие идеи приветствуются.