Google chrome Chrome扩展将html注入特定div

Google chrome Chrome扩展将html注入特定div,google-chrome,google-chrome-extension,content-script,Google Chrome,Google Chrome Extension,Content Script,我正在尝试为chrome做一个扩展,将html代码注入特定页面上的特定div 这是我想要注入一些代码的示例页面: 到目前为止,我已经做到了: manifest.json: { "manifest_version": 2, "content_scripts": [ { "js": [ "iframeInjector.js" ], "matches": [ "http://netdna.webdesignerdepot.com/*

我正在尝试为chrome做一个扩展,将html代码注入特定页面上的特定div

这是我想要注入一些代码的示例页面:

到目前为止,我已经做到了: manifest.json:

{
"manifest_version":         2,
"content_scripts":          [ {
    "js":       [ "iframeInjector.js" ],
    "matches":  [   "http://netdna.webdesignerdepot.com/*/*/*"
    ]
} ],
"description":              "Inject html",
"name":                     "Inject html",
"version":                  "1",
"web_accessible_resources": ["test.html"]
}
test.html:

Test html code
iframeInjector.js:

var iframe  = document.createElement ("iframe");
iframe.src  = chrome.extension.getURL ("test.html");
document.body.insertBefore (iframe, document.body.openModal);
在示例页面上有一个名为“OpenModel”的div,如何将html代码注入该div

另一个问题:是否有办法读取变量的页面标题,并在我的html代码中使用该变量,我会感染它


谢谢..

所以有一个带有
id=“openModal”
的div,您想将iframe附加到其中

iframeInjector.js

var iframe  = document.createElement ("iframe");
iframe.src  = chrome.extension.getURL ("test.html");
var yourDIV = document.getElementById("openModal");
yourDIV.appendChild(iframe);

这是非常有用的!只是一个评论。。。在manifiest.json上声明要包含的html以使其成为可用的“web\u可访问的\u资源”是非常重要的:[“test.html”]我之所以添加此内容,是因为我只阅读了答案,而且我必须阅读洞问题来了解它为什么不起作用Chrome.extension.getURL不推荐使用。改用chrome.runtime.getURL!