gpt4 book ai didi

javascript - 上下文菜单有时会消失

转载 作者:行者123 更新时间:2023-12-02 15:11:54 24 4
gpt4 key购买 nike

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"
}

最佳答案

选项 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 时)
当后台页面不是持久性的时,您应该将其添加到 list 中 ("persistent": false):

{
....
"background": {
"persistent": false,

},
....
}

在这种情况下,您可以将上下文菜单创建保留在 onInstalled 事件处理程序中。

查看 here 中的前 2 个示例,两种方法之间的差异是可见的。

关于javascript - 上下文菜单有时会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34763430/

24 4 0