我正在通过chrome扩展名将iframe(newframe
)注入主域。现在,我要访问插入的iframe的html文件中存在的div(id='contentDiv'
),并将其innerHTML
更改为“ content”。我正在从 background.js 发送一条消息,并在收到消息后运行以下代码:-
manifest.json
//some code
"web_accessible_resources": [
"Frame.html"
],
"content_scripts": [
{
"run_at": "document_start",
"matches": ["*://*/*"],
"js": ["script.js"]
}
],
"permissions": [
"activeTab",
"storage",
"webNavigation",
"tabs",
"https://foo.com/"
]
//some code
script.js
var newframe = document.createElement('iframe');
//some settings for the iframe
newFrame.src = chrome.runtime.getURL("Frame.html");
document.body.appendChild(newFrame);
var doc = newframe.contentWindow.document;
var contentDiv = doc.getElementById('contentDiv');
contentDiv.innerHTML = "coupons";
使用此代码时,即使iframe也无法注入,并且出现错误Error in event handler for runtime.onMessage: TypeError: Cannot read property 'document' of null
。我相信manifest.json
一切都很好,因为iframe的注入较早。