【问题标题】:Can't seem to get chrome.contextMenus.onClicked to fire in Chrome extension似乎无法让 chrome.contextMenus.onClicked 在 Chrome 扩展程序中触发
【发布时间】:2019-04-23 21:17:25
【问题描述】:

我正在尝试编写我的第一个浏览器扩展程序,但我似乎无法通过单击上下文菜单项来触发单击处理程序。 console.log 调用似乎没有向控制台吐出任何东西,我尝试使用 alert() 以防万一“背景”脚本(不确定背景和内容脚本之间的区别究竟是什么),但是似乎也没有做任何事情。

当我在测试页面的输入中选择一些文本时,右键单击并选择加密或解密,没有任何反应;不在控制台或开发人员工具的网络选项卡中。我做错了什么?

ma​​nefest.json:

{
    "manifest_version": 2,

    "name": "my name",
    "description": "my description",
    "version": "1.0",

    "background": {
        "scripts": ["jquery-3.2.1.min.js", "background.js"],
        "persistent": false
    },
    "content_scripts": [
        {
            "matches": ["http://*/*", "https://*/*"],
            "js": ["content_script.js"]
        }
    ],
    "permissions": [
        "activeTab",
        "contextMenus",
        "http://my.hostname.com/"
    ]
}

background.js:

/**
 * A handler which will run the analysis of the DOM element's selected text
 */
function clickHandler(info, tab) {
    "use strict";
    console.log(info);
    console.log(tab);
    // get selected text as encrypt/decrypt
    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, "get selection", null, function(selection) {
            var data = {
                "action": action,
                "selection": selection
            };
            var max_length = 4095;
            if (data.selection.length > max_length) {
                data.selection = data.selection.substring(0, max_length);
            }
            var url = "http://my.hostname.com/";
            jQuery.post(url, data, function (response) {
                chrome.tabs.sendMessage(tabs[0].id, response);
            }, "json");
        });
    });
}

/**
 * Create a context menu items to allow encode/decode
 */
chrome.runtime.onInstalled.addListener(function() {
    chrome.contextMenus.create({
        "id": "encrypt",
        "title": "Encrypt",
        "type": "normal",
        "contexts": ["editable"]
    });

    chrome.contextMenus.create({
        "id": "decrypt",
        "title": "Decrypt",
        "type": "normal",
        "contexts": ["selection"]
    });

    chrome.contextMenus.onClicked.addListener(clickHandler);
});

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    您需要将 contextMenus.onClicked 移到 onInstalled 之外。

    chrome.runtime.onInstalled.addListener(function() {
     //create context menus
    })
    chrome.contextMenus.onClicked.addListener(function() {
     //handle context menu actions
    })
    

    “注册以在每次加载事件页面时接收您的扩展程序感兴趣的任何事件。事件页面将为您的扩展程序的每个新版本加载一次。之后它只会被加载以传递您已注册的事件这一般意味着你的事件监听器应该添加在事件页面的顶级范围内,否则在事件页面重新加载时它们可能不可用。如果你在安装或升级扩展时需要做一些初始化,监听到 runtime.onInstalled 事件。这是注册 declarativeWebRequest 规则、contextMenu 条目和其他此类一次性初始化的好地方。"

    https://developer.chrome.com/extensions/event_pages#best-practices

    【讨论】:

    • 感谢您的回复!在我创建这张票之前,我实际上有这样的代码。我只是认为在创建上下文菜单项之前创建单击事件似乎是倒退的。但我只是按照你的建议做了 - 将两个 contextMenus.create 项目留在了 onInstalled 中,并将 contextMenus.onClicked 移出了 onInstalled - 但我仍然得到相同的行为......这在控制台或网络选项卡中没有反馈。
    • 后台页面是否使用控制台? (可从 chrome://extensions 访问)
    • 好吧,我在试图从中运行它的页面上使用控制台。当我运行 contextMenus 项目时,background.js 文件是否在与我正在查看的页面不同的控制台中运行?我假设因为它是针对我正在查看的页面运行的,所以 console.log 会出现在该控制台上……但听起来这是一个不正确的假设。
    • 不,它是一个单独的控制台。
    • 好的,奇怪...当我单击“检查视图:背景页面”时,我会启动一个开发人员工具窗口,并且在那里看到错误。我想我回到了正轨 - 谢谢!但是现在我在 clickHandler 中的 sendMessage 的响应回调中什么也没得到。我在 content_scripts.js 中有这个,我在两个控制台中都看不到任何东西: chrome.extension.onMessage.addListener(function (request, sender, sendResponse) { console.log('here'); sendResponse('something') ; })
    猜你喜欢
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2016-10-25
    • 2020-10-07
    • 2013-09-03
    相关资源
    最近更新 更多