【问题标题】:Is that possible calling content script method by context menu item in Chrome extension?是否可以通过 Chrome 扩展中的上下文菜单项调用内容脚本方法?
【发布时间】:2013-01-05 08:12:51
【问题描述】:

我正在尝试使用上下文菜单项来调用我在内容脚本中编写的方法。
这可能吗?

正如我所尝试的,上下文菜单只能在后端做事。
例如

// A generic onclick callback function.
function genericOnClick(info, tab) {
console.log("item " + info.menuItemId + " was clicked");
console.log("info: " + JSON.stringify(info));
console.log("tab: " + JSON.stringify(tab));
}

// Create one test item for each context type.
var contexts = ["page","selection","link","editable","image","video",
            "audio"];
for (var i = 0; i < contexts.length; i++) {
var context = contexts[i];
var title = "Test '" + context + "' menu item";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
                                   "onclick": genericOnClick});
console.log("'" + context + "' item:" + id);
}

此示例无法在当前页面上记录信息,而是在后台页面上记录信息。

我有一个 内容脚本 可以在指定页面上生成内容:

var showInfo = function(){ var dialogBoxWrapper = document.createElement("div");
document.body.appendChild(dialogBoxWrapper);}

我需要通过上下文菜单调用这个函数。 我该怎么办?

【问题讨论】:

  • 您可以使用消息通信通过上下文菜单从后台页面间接调用内容脚本的方法
  • 感谢您的回复。你能给我举一个这个案例的简单例子吗?
  • 查看我的答案以获取示例演示

标签: google-chrome google-chrome-extension content-script


【解决方案1】:

您可以参考以下代码作为参考,单击上下文菜单时,将调用内容脚本中的函数。

示例演示

manifest.json

{
    "name": "Content to Context menu",
    "description": "http://stackoverflow.com/questions/14452777/is-that-possible-calling-content-script-method-by-context-menu-item-in-chrome-ex",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "content_scripts": [
        {
            "matches": [
                "<all_urls>"
            ],
            "js": [
                "myscript.js"
            ]
        }
    ],
    "permissions": [
        "contextMenus"
    ]
}

background.js

function genericOnClick(info, tab) {
    console.log("item " + info.menuItemId + " was clicked");
    console.log("info: " + JSON.stringify(info));
    console.log("tab: " + JSON.stringify(tab));

    //Add all you functional Logic here
    chrome.tabs.query({
        "active": true,
        "currentWindow": true
    }, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {
            "functiontoInvoke": "showInfo"
        });
    });
}

// Create one test item for each context type.
var contexts = ["page", "selection", "link", "editable", "image", "video",
    "audio"];
for (var i = 0; i < contexts.length; i++) {
    var context = contexts[i];
    var title = "Test '" + context + "' menu item";
    var id = chrome.contextMenus.create({
        "title": title,
        "contexts": [context],
        "onclick": genericOnClick
    });
    console.log("'" + context + "' item:" + id);
}

myscript.js

var showInfo = function () {
    console.log("Show Info is invoked");
}
var showAnotherInfo = function () {
    console.log("Show Another Info");
}
chrome.extension.onMessage.addListener(function (message, sender, callback) {
    if (message.functiontoInvoke == "showInfo") {
        showInfo();
    }
    if (message.functiontoInvoke == "showAnotherInfo") {
        showAnotherInfo();
    }
});

参考文献

【讨论】:

  • 你使用chrome.tabs.query(...)而不是仅仅使用chrome.tabs.sendMessage(tab.id, ...)有什么特别的原因吗?
猜你喜欢
  • 2014-01-27
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2015-03-09
  • 1970-01-01
  • 2011-11-02
相关资源
最近更新 更多