我的Javascript代码有问题。我的意图是在页面顶部显示文字,但不是显示文字而是获取实际代码而不是' Hello World':
我的错误图片:http://i.stack.imgur.com/Ot65M.png
这是我的manifest.json:
"content_scripts": [
{
"matches": ["https://www.google.com/*"],
"js": ["contentscript.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": ["script.js"]
这是我的contentscripts.js:
var iframe = document.createElement("iframe");
iframe.style.width = "100%";
iframe.style.height = "50px";
iframe.style.border = "0px";
iframe.src = chrome.extension.getURL("script.js");
document.body.insertBefore(iframe, document.body.firstChild);
script.js就是:
document.write("Hello World!");
答案 0 :(得分:0)
您的脚本仍需要包含在<script>
元素中。
var script = document.createElement('script');
script.src = chrome.extension.getURL("script.js");
// Have to insert the iframe before `contentWindow` is available.
document.body.insertBefore(iframe, document.body.firstChild);
iframe.contentWindow.document.body.appendChild(script);
答案 1 :(得分:0)
更新:这对我有用:
在manifest.json中:(我有SSL问题所以改变了谷歌&#34;匹配&#34;从https到http。)
{
"manifest_version": 2,
"name": "testing document write",
"description": "testing document write from separate file",
"version": "1.0",
"content_scripts": [
{
"matches": ["http://google.com/*"],
"js": ["contentscript.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": ["scriptjs.html"]
}
在contentscripts.js中:
var iframe = document.createElement("iframe");
iframe.style.width = "100%";
iframe.style.height = "50px";
iframe.style.border = "0px";
iframe.src = 'http://fully-qualified-URL/scriptjs.html'; // <-- LOAD HTML
document.body.insertBefore(iframe, document.body.firstChild);
在scriptjs.html中:
<script>
document.write('Hello World');
</script>