Chrome 扩展程序将 html 注入到特定的 div

use*_*376 5 google-chrome google-chrome-extension content-script

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

这是我想注入一些代码的示例页面:http : //netdna.webdesignerdepot.com/uploads7/creating-a-modal-window-with-html5-and-css3/demo.html

到目前为止,我已经做到了: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"]
}
Run Code Online (Sandbox Code Playgroud)

测试.html:

Test html code
Run Code Online (Sandbox Code Playgroud)

iframeInjector.js:

var iframe  = document.createElement ("iframe");
iframe.src  = chrome.extension.getURL ("test.html");
document.body.insertBefore (iframe, document.body.openModal);
Run Code Online (Sandbox Code Playgroud)

在示例页面上有一个名为“openModal”的 div,如何将我的 html 代码注入到该 div 中?

另一个问题:有没有办法将页面标题读取到变量并在我感染的 html 代码中使用该变量?

谢谢..

dev*_*l69 4

那么您想要向一个 divid="openModal"添加 iframe 吗?

iframeInjector.js

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