3

从后台脚本获取消息时,我遇到了问题。我的心都快炸了,哪里错了?

显现

{
    "name":"Some name",
    "version":"0.1",
    "manifest_version": 2,
    "description":"extension for Google Chrome",
    "permissions": ["tabs","http://*/*", "https://*/*", "<all_urls>","notifications","contextMenus","alarms","tabs","bookmarks"],
    "content_scripts":[
    {
        "matches":["<all_urls>"],
        "css":["style.css"],
        "js":["content.js","jquery.js"]
    }
    ],
    "background": {
        "scripts": ["background.js","jquery.js"],
        "persistent": false
    },
    "icons":{
        "16": "images/icon16.png",
        "48": "images/icon48.png",
        "128": "images/icon128.png"
    },
    "browser_action": {
        "default_icon": {
            "19": "images/icon19.png",           
            "38": "images/icon38.png"
        },
        "default_title": "Order Checker",
        "default_popup": "popup.html"
    }
} 

背景.js

chrome.tabs.getSelected(null, function(tab) {
    console.log('Loaded!');
    chrome.tabs.sendMessage(tab.id, {
        greeting: "hello"
    });
    console.log('Sended on '+tab.id+'...');
});

内容.js

chrome.extension.onMessage.addListener(function(msg, _, request) {
    console.log('Getting request...');
    console.log(msg.greeting);
});

正如预期的那样,在后台页面中,我收到了控制台 2 消息。但是在内容页面上我没有任何消息,所以我认为 content.js 中的代码有问题。我做错了什么?

4

1 回答 1

1

chrome.tabs.getSelected()已弃用Chrome 16,请改用chrome.tabs.query()

Problem in Your Script

chrome.tabs.getSelected() 返回当前选定选项卡的引用,在加载扩展后,您的 chrome://chrome/extensions/背景页面将向扩展页面发送消息,其中包含内容脚本do not execute

修改您的后台脚本以适应chrome.tabs.query(),您可以在网页控制台中看到消息!

参考

于 2013-01-05T18:12:29.380 回答