chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (!info.srcUrl)
return;
console.log(info.srcUrl);
$.ajax({
url : "https://hizliresim.com/p/eklenti-yukle",
headers : {
Accept : "application/json"
},
type : "POST",
data : {
remote_file_url : info.srcUrl
},
success : function (data) {
console.log(data);
for (var imageIndex = 0; imageIndex < data.images.length; imageIndex++) {
var resultImage = data.images[imageIndex];
if (resultImage.status == 0) {
var input = document.createElement('input');
document.body.appendChild(input);
input.value = resultImage.image_url.replace('hizli', 'i.hizli') + ".jpg";
input.select();
document.execCommand('copy', false, null);
input.remove();
}
}
}
});
});
chrome.runtime.onInstalled.addListener(function (details) {
chrome.contextMenus.create({
id : "menu_upload_image",
title : "Hızlı Resim'e Yükle",
contexts : ["image"]
});
});
这是background.js
。它重新加载扩展时有效,但过了一会儿,图像的上下文菜单就消失了。
我认为chrome.runtime.onInstalled.addListener
可能还不够。
可能是什么问题?
编辑:这是manifest.json
{
"background": {
"scripts": [ "jquery.js","background.js" ]
},
"content_security_policy": "script-src 'self' 'unsafe-eval' https://ssl.google-analytics.com; object-src 'self'",
"content_scripts": [ {
"all_frames": true,
"js": [ "go.js" ],
"matches": [ "http://*/*", "https://*/*" ],
"run_at": "document_end"
} ],
"description": "test",
"icons": {
"128": "icon.jpg",
"16": "icon.jpg",
"48": "icon.jpg"
},
"manifest_version": 2,
"name": "testaddon",
"permissions": [ "contextMenus","bookmarks", "unlimitedStorage", "notifications", "clipboardWrite", "notifications", "clipboardRead", "management", "tabs", "history", "cookies", "idle", "storage", "webRequest", "webRequestBlocking", "contentSettings", "*://*/*" ],
"version": "14.53"
}
答案 0 :(得分:2)
选项1
(当background page持久时,这是默认的)
您在安装扩展时创建上下文菜单。这就是为什么它只在安装后显示,直到浏览器关闭
您应该在扩展开始时创建上下文菜单按钮(从onInstalled侦听器中删除它):
chrome.contextMenus.onClicked.addListener(function (info, tab) {
//....
});
chrome.contextMenus.create({
id : "menu_upload_image",
title : "Hızlı Resim'e Yükle",
contexts : ["image"]
});
选项2
(使用Event page时)
当后台页面不是持久性时,您应该将其添加到清单("persistent": false
):
{
....
"background": {
"persistent": false,
},
....
}
在这种情况下,您可以在onInstalled
事件处理程序中创建上下文菜单。
请参阅here中的前两个示例,其中可以看到两种方法之间的差异。