Javascript 上下文菜单数组?

Javascript 上下文菜单数组?,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我已经为一家公司的网站编写了一个简单的扩展,它可以读取项目数组,并在我将鼠标悬停在上面时提醒项目的标题。这非常有效,但不是我想要的: let projects=document.getElementsByClassName(“项目名称”); 提取数据=函数(){ 让title=this.GetElementsByCassName(“项目名称ng绑定”)[0].getAttribute(“title”) 警报(标题); }; 对于(var i=0;i console.log('背景脚本已收到消息并

我已经为一家公司的网站编写了一个简单的扩展,它可以读取项目数组,并在我将鼠标悬停在上面时提醒项目的标题。这非常有效,但不是我想要的:

let projects=document.getElementsByClassName(“项目名称”);
提取数据=函数(){
让title=this.GetElementsByCassName(“项目名称ng绑定”)[0].getAttribute(“title”)
警报(标题);
};
对于(var i=0;i
我想稍微修改一下我的代码,这样就不用在鼠标悬停时发出警报,而是将警报作为一个选项添加到Chrome上下文菜单中。类似于:

chrome.contextMenus.create({
标题:“警报项目标题”,
上下文:[“选择”],//上下文类型
onclick:extract\u data//A回调函数
});
这并不完全正确,因为我需要将上下文菜单更改为依赖于项目行,正如现在编写的,上下文菜单中没有对
projects
的引用


有没有简单的方法可以将我的
鼠标更改为
上下文菜单的值

如果你想让男人们显示带有所选项目标题的项目,我想你运气不好。我不相信有办法做到这一点

但是如果你想让男人们在你的选择中显示一个项目,试试这个

 chrome.contextMenus.create({
    "id": "1",
     title: "Do you want to alert '%s' project title",
    "contexts": ["selection"],
    "documentUrlPatterns": [...],
     onclick: extract_data // A callback function
 })

如果你想让menù显示带有选定项目标题的项目,我认为你运气不好。我不相信有办法做到这一点

但是如果你想让男人们在你的选择中显示一个项目,试试这个

 chrome.contextMenus.create({
    "id": "1",
     title: "Do you want to alert '%s' project title",
    "contexts": ["selection"],
    "documentUrlPatterns": [...],
     onclick: extract_data // A callback function
 })

也许我太匆忙了


如果您希望menù显示标题为选定项目(非选定文本)的项目 如果要将警报作为单个上下文菜单项onclick事件移动 我想你有机会

但您必须以声明方式注入内容脚本

/* in manifest.json */
...
"content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["cs.js"],
    "run_at": "document_idle"
}]
...


/* in content script "cs.js" */
function extract_data() {
    var title = this.getElementsByClassName("project-name ng-binding")[0].getAttribute("title");
    chrome.runtime.sendMessage({'title': title}, resp =>
        console.log('backgroung script has received the message and send back this message: ' + resp.msg)
    )    
}
var projects = document.getElementsByClassName("project-name");
for (var i = 0; i < projects.length; i++)
    projects[i].addEventListener('mouseover', extract_data, false)


/* in background script */
function createMenu() {
    chrome.contextMenus.create({
        'id': '1',
        'title': 'Alert Project Title',
        'contexts': ["selection"],
        'documentUrlPatterns': ["<all_urls>"],
        'enabled': false
    }, _ => {
        if (chrome.runtime.lastError) {} else {}
    })
}
function handleInstalled(details) {
    createMenu()
}
chrome.runtime.onInstalled.addListener(handleInstalled);
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
    chrome.contextMenus.update(
        '1',
        {
            "title": "Do you want to alert " + msg.title + " project title",
            'contexts': ["selection"],
            'documentUrlPatterns': ["<all_urls>"],
            "enabled": true,
            "onclick": _ => alert(msg.title)
        },
        _ => {
            sendResponse( {'msg': chrome.runtime.lastError ? 'Some error...' : 'Fine!' } )
        }
    )
    return true
})
manifest.json中的
/**/
...
“内容脚本”:[{
“匹配项”:[“”],
“js”:[“cs.js”],
“运行时间”:“文档空闲”
}]
...
/*在内容脚本“cs.js”中*/
函数提取_数据(){
var title=this.getElementsByClassName(“项目名称ng绑定”)[0].getAttribute(“title”);
chrome.runtime.sendMessage({'title':title},resp=>
console.log('背景脚本已收到消息并发回此消息:'+resp.msg)
)    
}
var projects=document.getElementsByClassName(“项目名称”);
对于(var i=0;i {
if(chrome.runtime.lastError){}else{}
})
}
已安装函数句柄(详细信息){
createMenu()
}
chrome.runtime.onInstalled.addListener(handleInstalled);
chrome.runtime.onMessage.addListener((msg、sender、sendResponse)=>{
chrome.contextMenus.update(
'1',
{
“标题”:“是否要提醒”+msg.title+“项目标题”,
“上下文”:[“选择”],
“documentUrlPatterns”:[“”],
“启用”:正确,
“onclick”:=>alert(msg.title)
},
_ => {
sendResponse({'msg':chrome.runtime.lastError?'Some error…':'Fine!'})
}
)
返回真值
})

也许我太匆忙了


如果您希望menù显示标题为选定项目(非选定文本)的项目 如果要将警报作为单个上下文菜单项onclick事件移动 我想你有机会

但您必须以声明方式注入内容脚本

/* in manifest.json */
...
"content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["cs.js"],
    "run_at": "document_idle"
}]
...


/* in content script "cs.js" */
function extract_data() {
    var title = this.getElementsByClassName("project-name ng-binding")[0].getAttribute("title");
    chrome.runtime.sendMessage({'title': title}, resp =>
        console.log('backgroung script has received the message and send back this message: ' + resp.msg)
    )    
}
var projects = document.getElementsByClassName("project-name");
for (var i = 0; i < projects.length; i++)
    projects[i].addEventListener('mouseover', extract_data, false)


/* in background script */
function createMenu() {
    chrome.contextMenus.create({
        'id': '1',
        'title': 'Alert Project Title',
        'contexts': ["selection"],
        'documentUrlPatterns': ["<all_urls>"],
        'enabled': false
    }, _ => {
        if (chrome.runtime.lastError) {} else {}
    })
}
function handleInstalled(details) {
    createMenu()
}
chrome.runtime.onInstalled.addListener(handleInstalled);
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
    chrome.contextMenus.update(
        '1',
        {
            "title": "Do you want to alert " + msg.title + " project title",
            'contexts': ["selection"],
            'documentUrlPatterns': ["<all_urls>"],
            "enabled": true,
            "onclick": _ => alert(msg.title)
        },
        _ => {
            sendResponse( {'msg': chrome.runtime.lastError ? 'Some error...' : 'Fine!' } )
        }
    )
    return true
})
manifest.json中的
/**/
...
“内容脚本”:[{
“匹配项”:[“”],
“js”:[“cs.js”],
“运行时间”:“文档空闲”
}]
...
/*在内容脚本“cs.js”中*/
函数提取_数据(){
var title=this.getElementsByClassName(“项目名称ng绑定”)[0].getAttribute(“title”);
chrome.runtime.sendMessage({'title':title},resp=>
console.log('背景脚本已收到消息并发回此消息:'+resp.msg)
)    
}
var projects=document.getElementsByClassName(“项目名称”);
对于(var i=0;i {
if(chrome.runtime.lastError){}else{}
})
}
已安装函数句柄(详细信息){
createMenu()
}
chrome.runtime.onInstalled.addListener(handleInstalled);
chrome.runtime.onMessage.addListener((msg、sender、sendResponse)=>{
chrome.contextMenus.update(
'1',
{
“标题”:“是否要提醒”+msg.title+“项目标题”,
“上下文”:[“选择”],
“documentUrlPatterns”:[“”],
“启用”:正确,
“onclick”:=>alert(msg.title)
},
_ => {
sendResponse({'msg':chrome.runtime.lastError?'Some error…':'Fine!'})
}
)
返回真值
})

注意:后台脚本中的警报仅在清单V2中有效。如果要构建MV3扩展,则必须用通知替换警报,否则必须从后台向内容脚本发送消息,要求最后一个脚本显示该警报。注意:后台脚本中的警报仅在清单V2中有效。如果您正在构建MV3扩展,则必须将警报替换为通知,否则您必须从后台向内容脚本发送一条消息,请求最后一个通知