我的Chrome扩展有一个内容脚本,它将自定义DIV注入到当前页面中。这部分起作用了。
但是,该扩展还有一个右键单击上下文菜单,单击该菜单时,应该会以某种方式修改注入的DIV (比方说,在该DIV中添加一些文本)。问题是没有找到注入的内容。右键单击菜单处理程序在Background.js中,该文件对内容一无所知。
manifest.js
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"css": ["contentstyle.css"],
"js": ["jquery-1.11.2.min.js", "contentscript.js"],
"background": {
"persistent": true,
"scripts": ["background.js"]
},
contentscript.js
// Add Custom DIV - works OK
var div = document.createElement( 'div' );
div.id = 'infoDiv';
document.body.appendChild( div );
document.getElementById('infoDiv').innerHTML = 'TEST';
background.js
// Add menu - gets added, but can't see Injected Content from here
chrome.contextMenus.create({
"title": "My Right-Click Menu",
"contexts": ["image"],
"onclick" : changeDiv
});
function changeDiv(e)
{
var divHTML = document.getElementById('infoDiv').innerHTML;
alert('Current HTML in DIV: ' + divHTML);
}
我无法从后台脚本获得divHTML,有某种错误,没有警报框。背景和内容之间能有沟通吗?我被迫在后台脚本中实现菜单,对吗?
https://stackoverflow.com/questions/29931183
复制相似问题