1

我正在开发一个Chrome扩展,我希望它将iframe插入Google主页,但该html只显示为纯文本。为什么会发生?当插入到网页时,HTML显示为纯文本

这是目前我的代码:

manifest.json的

{ 
    "manifest_version": 2, 
    "name": "Google", 
    "version": "1.0", 
    "description": "Google iframe", 
    "icons": { 
     "48": "img/rsz_apple-touch-icon-144x144.png", 
     "16": "img/favicon-16x16.png", 
     "128": "img/rsz_1apple-touch-icon-144x144.png" 
    }, 
    "content_scripts": [{ 
     "matches": ["https://www.google.com.au/"], 
     "js": ["content-script.js"] 
    }], 
    "browser_action": { 
     "default_icon": "img/favicon-16x16.png", 
     "default_title": "Press the button to see more actions", 
     "default_popup": "popup.html" 
    }, 
    "background": { 
     "page": "background.html" 
    }, 
    "permissions": [ 
     "activeTab" 
    ] 
} 

内容的script.js

var div=document.createElement("div"); 
document.getElementById("searchform").appendChild(div); 
div.innerText="<iframe src='https://www.google.com.au/'>ERROR</iframe>"; 

这里是它表明: screenshot

谢谢你的帮助。

回答

1

innerText检索并设置内容为纯文本。如果你想设置HTML,使用innerHTML。

var div=document.createElement("div"); 
document.getElementById("searchform").appendChild(div); 
div.innerHTML="<iframe src='https://www.google.com.au/'>ERROR</iframe>";