0

I have following textarea element having some templated codes in it and it cannot be modified as there is no option in blogger:


<textarea hidden class='hidden' id='tempJs'><script defer='' src='/js/cookienotice.js' type="976adfed5ff69a3a2e21c1d0-text/javascript"></script>

<script type="976adfed5ff69a3a2e21c1d0-text/javascript">

    document.addEventListener('DOMContentLoaded', function(event) {

      // Some JS

    });

  </script>

<script type="976adfed5ff69a3a2e21c1d0-text/javascript" src="https://www.blogger.com/static/v1/widgets/1197256859-widgets.js"></script>

<script type="976adfed5ff69a3a2e21c1d0-text/javascript">

window['__wavt'] = 'AOuZoY7izjkWOpen881b-llDDktDVnRQIQ:1672120416426';

//...Some JS

_WidgetManager._RegisterWidget('_HTMLView', new _WidgetInfo('HTML9', 'gmp_custom_script', document.getElementById('HTML9'), {}, 'displayModeFull'));

</script></textarea>

I want to extract the following parts of codes from textarea.innerText:

window['__wavt'] = 'AOuZoY7izjkWOpen881b-llDDktDVnRQIQ:1672120416426';

//...Some JS

_WidgetManager._RegisterWidget('_HTMLView', new _WidgetInfo('HTML9', 'gmp_custom_script', document.getElementById('HTML9'), {}, 'displayModeFull'));

I tried the following:

<!--[ My Script ]-->

<script>

/*<![CDATA [*/

  let tempEl = document.getElementById("tempJs"),

  tempTx = tempEl.innerText,

  widgetJs = tempTx.split("<script type='text/javascript'>\n")[1].split("\n<\/script>")[0].trim();

tempEl.remove()

/*]]>*/

</script>

But, the thing is type attribute is changing all the time also there are four script tags

How can I extract only that part from string?

1 Answers1

0

Turn the textarea text into a document. Then you can select all script tags, then iterate through their .textContent to identify the one with a reference to __wavt.

// Your code
const doc = new DOMParser().parseFromString(
  document.querySelector('textarea').value,
  'text/html'
);
const script = [...doc.querySelectorAll('script')]
  .find(script => script.textContent.includes('__wavt'));
console.log(script.textContent);
<!-- Built-in code -->
<textarea hidden class='hidden' id='tempJs'><script defer='' src='/js/cookienotice.js' type="976adfed5ff69a3a2e21c1d0-text/javascript"></script>

<script type="976adfed5ff69a3a2e21c1d0-text/javascript">

    document.addEventListener('DOMContentLoaded', function(event) {

      // Some JS

    });

  </script>

<script type="976adfed5ff69a3a2e21c1d0-text/javascript" src="https://www.blogger.com/static/v1/widgets/1197256859-widgets.js"></script>

<script type="976adfed5ff69a3a2e21c1d0-text/javascript">

window['__wavt'] = 'AOuZoY7izjkWOpen881b-llDDktDVnRQIQ:1672120416426';

//...Some JS

_WidgetManager._RegisterWidget('_HTMLView', new _WidgetInfo('HTML9', 'gmp_custom_script', document.getElementById('HTML9'), {}, 'displayModeFull'));

</script></textarea>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320