【问题标题】:Need to send message from a context menu to a content script需要将消息从上下文菜单发送到内容脚本
【发布时间】:2014-11-09 17:45:22
【问题描述】:

我正在编写一个小扩展,让我可以轻松地将链接提交到 reddit。 此扩展添加了一个新的上下文菜单(“提交页面”)。如果用户右键单击并选择此菜单,则会在另一个选项卡中打开 www.redddit.com/submit 页面,并提交触发菜单的页面。

我添加了上下文菜单:

contextMenu.js

// Setup where the menu is presents;
// A list of [context, context menu text, id]
var redditURL = 'http://www.reddit.com/submit';
var contexts = [["page", "Submit page", "id-submitPage"], ["link", "Submit link", "id-submitLink"], ["editable", "Submit text", "id-submitText"], ["image", "Submit image", "id-submitImage"]];

// Add all menus to their context
contexts.forEach(function(element) {
    chrome.contextMenus.create({
        "title" : element[1],
        "contexts" : [element[0]],
        "id" : element[2]
    });
});

// Add actions to menus
chrome.contextMenus.onClicked.addListener(function(info, tab) {
    var submittedURL = tab && tab.url;
    if (info["menuItemId"] == "id-submitPage") {
        chrome.tabs.create({
            "url" : redditURL
        }, function(tab) {
            // After we create the tab we also send a message to the content
            // script associated with the page to intercept our info
            console.log(submittedURL);
            chrome.tabs.sendMessage(tab.id, {
                "url" : submittedURL,
                "type" : "submitPage"
            });
        });
    }
});

正如您在 addListener 中看到的那样,我正在使用 chrome.tabs.sendMessage 将我提交的 URL 发送到与相关联的内容脚本:redditURL

内容脚本:contextMenu-RedditSubmit.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    alert('here');
    console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
});

还有清单文件:

...
    "background": {
        "scripts": ["contextMenu.js"],
        "persistent": false
    },
    "content_scripts": [
        {
            "matches": ["http://www.reddit.com/submit"],
            "js": ["contextMenu-RedditSubmit.js"],
            "run_at": "document_start"
        }
    ],
...

问题是在 contextMenu-RedditSubmit.js 内容脚本中没有收到我的消息。我既看不到 console.log 也看不到警报。有什么建议吗?

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension


    【解决方案1】:

    您的消息在事件 document_start 内容脚本执行之前发送。

    为确保它正常工作,请切换到使用programmatic injection

    chrome.tabs.create({
      "url" : redditURL
    }, function(tab) {
      // After we create the tab we also send a message to the content
      // script associated with the page to intercept our info
      chrome.tabs.executeScript(
        tab.id,
        {file: "contextMenu-RedditSubmit.js"},
        function() {
          // Here, it is guaranteed that the script finished executing
          //  (or there was an error)
          chrome.tabs.sendMessage(tab.id, {
            "url" : submittedURL,
            "type" : "submitPage"
          });
        }
      );
    });
    

    【讨论】:

    • 还是不行。我还修改了 manifest.json 并添加了编程注入所需的权限。
    • 好吧,尝试添加错误检查。向sendMessage 添加回调并检查chrome.runtime.lastError
    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多