【问题标题】:How to find `script` element with my code?如何使用我的代码查找 `script` 元素?
【发布时间】:2020-12-27 07:59:41
【问题描述】:

我在 Dynamics 365 表单上注册了我的 JavaScript 函数:

我的代码运行良好(TypeScript 转译的结果):

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

但是,如果我尝试根据包含我的代码的 script 元素来查找,那么我什么也没找到。我在浏览器的控制台 (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);
            }
        }
    }
}

如何找到直接包含我的代码或通过scr属性指向它的script元素?

【问题讨论】:

    标签: javascript dynamics-crm microsoft-dynamics


    【解决方案1】:

    事实证明我的脚本更深(在其他框架中)。我添加了代码以递归检查所有帧并能够找到我要查找的内容:

    /** 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));
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2014-03-27
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多