How to collect all script tags of HTML page in a variable

后端 未结 6 1080
闹比i
闹比i 2020-12-03 19:01

I would like to collect all the code section present in the HTML page in some variable.

What should be the simpler w

6条回答
  •  被撕碎了的回忆
    2020-12-03 19:23

    Here you go --

    (function () { 
            'use strict';
            let logscript = function () {
                let js = document.scripts;
                for (let i = 0; i < js.length; i++) {
                    if (js[i].src) {
                            console.log(i, js[i].src);
                        } else {
                            console.log(i, js[i].innerHTML);
                    }   
                }
            };
            if (document.readyState === 'complete') {
                    logscript();
            } else {
                    window.addEventListener('load', logscript);
            }
    })();
    
    

提交回复
热议问题