![](/img/trans.png)
[英]Calling an injected button from the content script to the background.js
[英]Background.js Doesn't Find Content Injected with Content Script
我的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);
}
我无法从Background脚本中获取divHTML,出现某种错误并且没有警报框。 背景和内容之间可以进行交流吗? 我被迫在Background脚本中实现菜单,对吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.