How to find `script` element with my code?

Viewed 81

I registered my JavaScript function on Dynamics 365 form:

enter image description here

My code works fine (the result TypeScript transpiling):

var Softline_Stuff;
(function (Softline_Stuff) {
    function hello() {
        alert("Hello, World! v2");
    }
    Softline_Stuff.hello = hello;
})(Softline_Stuff || (Softline_Stuff = {}));
//# sourceMappingURL=isv_stuff.js.map

But if I try to find according script element which contains my code then I found nothing. I run this code in browser's console (Google Chrome):

// Search inside of main document and child documents are located in iframes:
let docs = [document];
for(let frameIndex = 0; frameIndex < frames.length; frameIndex++) {
    docs = [...docs, frames[frameIndex].document];
}

for(let doc of docs) {
    const tags = doc.getElementsByTagName('script');
    for(let index = 0; index < tags.length; index++) {
        // Check src attribute value
        const path = tags[index].getAttribute("src");
        if(path && path.includes("isv_")){
            console.log(path);
        }
        // if src value is null it means script is inlined
        else if(!path) {
            const code = tags[index].textContent;
            // My code contains this string:
            if(code && code.includes("Hello, World!")){
                console.log(code);
            }
        }
    }
}

How to find script element which contains my code directly or points to it through scr attribute?

1 Answers

It turns out my scripts were even deeper (in other frames). I added code to recursively check all frames and was able to find what I was looking for:

/** Find all `script` elements with `src` attribute value that are matched to 
`regexp` parameter. */
function findScripts(regexp) {
    /** The function of recursively extracting documents from frames. */
    const fillDocs = function (docs, frames) {
        for(let frameIndex = 0; frameIndex < frames.length; frameIndex++) {
            const doc = frames[frameIndex].document;
            if(!docs.includes(doc)){
                docs.push(doc);
            }
            fillDocs(docs, frames[frameIndex].frames);
        }
    }

    let docs = [document];
    fillDocs(docs, frames);

    let result = [];

    for(let doc of docs) {
        const tags = doc.getElementsByTagName('script');
        for(let index = 0; index < tags.length; index++) {
            const path = tags[index].getAttribute("src");
            if(path && path.match(regexp)){
                result = [...result, tags[index]];
            }
        }
    }
    return result;
}

// The `findScripts` function using example:
findScripts(/isv_/i).map(n => ({
    type: n.getAttribute("type"), 
    src: n.getAttribute("src"),
    documentTitle: n.ownerDocument.title,
})).forEach(n => console.log(n));

The output:

enter image description here

Related