无法让 Chrome 上下文菜单项显示在 Chrome 扩展程序上

Can't get Chrome Context Menu item to show on Chrome Extension

我有一个 Chrome 扩展程序,可以在 Google 地图中打开一个 KML/KMZ 文件。当用户右键单击 KML 文档的 link 时,将触发扩展。但是上下文菜单没有出现。它使用后台脚本。这是 manifest.json:

{
  "manifest_version": 2,
  "name": "KML/KMZ Viewer",
  "version": "1.0.0",
  "description": "Can be used to view KML/KMZ Files.",
  "icons": {
    "19": "tiny.jpg",
    "24": "icon.png",
    "128": "image.png"
  },
  "permissions": [
    "tabs",
    "contextMenus",
    "activeTab",
    "background"
  ],
  "background": {
    "scripts": ["background.js"]
  }
}

这里是 background.js:

// Set up context menu at install time.
chrome.runtime.onInstalled.addListener(function() {
    menuCreate();
    console.log('Issued function');
});

// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);

// The onClicked callback function.
function onClickHandler(info, tab) {
    var url = info.selectionText;
    openWin(url);
};

function openWin(kml) {
    chrome.windows.create({"url":"http://www.nearby.org.uk/google/fake-kmlgadget.html?    up_kml_url="+kml+"&up_view_mode=earth&up_lat=&up_lng=&up_zoom=&up_earth_2d_fallback=1&up_earth_fly_from_space=1&up_earth_show_nav_controls=1&up_earth_show_buildings=1&up_earth_show_terrain=1&up_earth_show_roads=1&up_earth_show_borders=1&up_earth_sphere=earth&up_maps_streetview=1&up_maps_default_type=hybrid"});
}

function menuCreate() {
    chrome.contextMenus.create({"title": "Open KML/KMZ", "contexts": ["link"], "id": "kmlopen", "targetUrlPatterns": ["*.kml", "*.kmz"]});
    console.log('Function ran');
}

然而,当我右键单击 link 到 KML 或 KMZ 文件时,上下文菜单没有显示。根据 JavaScript 控制台,函数 运行。这是当我在 _generated_background_page.html 下手动 运行 chrome.contextMenus.create({"title": "Open KML/KMZ", "contexts": ["link"], "id": "kmlopen", "targetUrlPatterns": ["*.kml", "*.kmz"]}); 我得到菜单项的 kmlopenid 时控制台输出的内容。我究竟做错了什么? openWin(/*some url*/); 函数工作正常。

模式错误。

模式遵循标准 Match pattern 格式。

所以你应该使用模式

"targetUrlPatterns": ["*://*/*.kml", "*://*/*.kmz"]

不过,是mindful of query strings.