简体   繁体   English

返回iframe主体的document.body

[英]document.body returning iframe body

I am developing a chrome extension that injects html to Linkedin. 我正在开发将html注入Linkedin的chrome扩展程序。 When I do $(document.body).append($("<div>")); 当我做$(document.body).append($("<div>")); from my Js file, it also appends for an Iframe component that's on the page.How ever when I type $(document.body) on the console it only returns main body. 从我的Js文件中,它还附加了页面上的Iframe组件,但是当我在控制台上键入$(document.body)时,它只会返回主体。 How do I restrict it to only main body from my js file.? 如何将其限制为仅来自js文件的主体?

My manifest file: 我的清单文件:

{
    "manifest_version":2,
    "name": "Hawk! Beta",
    "description": "Hawk",
    "version": "1.0",
    "permissions": [
        "activeTab",
        "tabs",
        "storage",
        "https://ajax.googleapis.com/",
        "https://*.ngrok.io/"
    ],
    "icons": { 
        "16": "img/icon.png",
        "48": "img/icon.png",
        "128": "img/icon.png" 
    },
    "browser_action": {
        "default_icon": {                    
            "19": "img/icon.png",          
            "38": "img/icon.png"          
        }
    },
    "background": {
        "scripts": [
            "js/lib/jquery.min.js", 
            "js/lib/asteroid/ddp.js",
            "js/lib/asteroid/q/q.js",
            "js/lib/asteroid/asteroid/dist/asteroid.chrome.js",
            "js/lib/asteroid/asteroid/dist/plugins/facebook-login.js",
            "js/lib/asteroid/asteroid/dist/plugins/google-login.js",
            "js/lib/asteroid/asteroid/dist/plugins/github-login.js",
            "js/lib/bootstrap.min.js",
            "js/background.js"
        ]
    },
    "content_scripts": [
        {
          "matches": [
             "https://www.linkedin.com/*"
          ],
          "all_frames" : true,
          "match_about_blank": true,
          "js": [
            "js/lib/jquery.min.js", 
            "js/lib/handlebars.js",
            "js/content_scripts/getPagesSource.js"
          ],
          "css":[
            "css/custom.css"
          ]
        }
    ],
    "web_accessible_resources": [
        "img/*",
        "js/*",
        "js/lib/*",
        "css/custom.css",
        "html/*"
    ]
}

The code I used to inject html 我用来注入html的代码

function injectHtml(){
    console.log(document.body); //this returns 2 bodies
    $(document.body).append($("<div>", {
        "class": "myapp-container"
    }));
}

I only want to inject into main body and not into iframe body. 我只想注入主体而不是iframe主体。

As suggested by Patrick and wOxxOm , I edited the manifest file's content_scripts to look like below. 根据Patrick和wOxxOm的建议,我编辑了清单文件的content_scripts如下所示。

"content_scripts": [
    {
      "matches": [
         "https://www.linkedin.com/*"
      ],
      "all_frames" : false, //this had to be changed 
      "match_about_blank": true,
      "js": [
        "js/lib/jquery.min.js", 
        "js/lib/handlebars.js",
        "js/content_scripts/getPagesSource.js"
      ],
      "css":[
        "css/custom.css"
      ]
    }
]

It now works fine. 现在可以正常工作了。 Thanks guys. 多谢你们。

Edit: all_frames: 编辑:all_frames:

Controls whether the content script runs in all frames of the matching page, or only the top frame. 控制内容脚本是在匹配页面的所有框架中运行,还是仅在顶部框架中运行。

For more we have, this link to know more 要了解更多,请点击此链接以了解更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM