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

编程入门 行业动态 更新时间:2024-10-27 08:32:48
本文介绍了无法使Chrome上下文菜单项显示在Chrome扩展程序上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Chrome扩展程序,可以在Google Maps中打开KML/KMZ文件.当用户右键单击指向KML文档的链接时,触发扩展.但是不会显示上下文菜单.它使用背景脚本.这是 manifest.json :

{"manifest_version":2"name":"KML/KMZ Viewer","version":"1.0.0","description":可用于查看KML/KMZ文件.",图标":{"19":"tiny.jpg","24":"icon.png","128":"image.png"},权限":[标签","contextMenus","activeTab",背景"],背景": {脚本":["background.js"]}}

这是 background.js :

//在安装时设置上下文菜单.chrome.runtime.onInstalled.addListener(function(){menuCreate();console.log('已发布函数');});//添加点击事件chrome.contextMenus.onClicked.addListener(onClickHandler);//onClicked回调函数.函数onClickHandler(info,tab){var url = info.selectionText;openWin(url);};函数openWin(kml){chrome.windows.create({"url":"www.nearby.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_road_ = 1 = amp; amp_up; ear_map_up_map_samp; up;}函数menuCreate(){chrome.contextMenus.create({"title":打开KML/KMZ",上下文":[链接"],"id":"kmlopen","targetUrlPatterns":["* .kml","*.kmz]});console.log('Function ran');}

但是,当我右键单击指向KML或KMZ文件的链接时,上下文菜单不会显示.根据JavaScript控制台,功能已运行.这是我运行 chrome.contextMenus.create({"title":"Open KML/KMZ","contexts":["link"],"id":"kmlopen","targetUrlPatterns:[" * .kml," * .kmz]}); 在 _generation_background_page.html 下手动获得 kmlopen ,菜单项的ID .我究竟做错了什么? openWin(/* some url */); 函数可以正常工作.

解决方案

错误的模式.

模式遵循标准的匹配模式格式.

所以您应该使用模式

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

但是,请注意查询字符串.

I have a Chrome extension that opens a KML/KMZ file in Google Maps. The extension is triggered when the user right-clicks a link to the KML document. But the context menu does not appear. It uses a background script. Here is the 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"] } }

Here is the 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":"www.nearby.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'); }

Yet when I right-click on a link to a KML or KMZ file, the context menu doesn't show. According to the JavaScript console, the functions ran. This is what the console outputs when I run chrome.contextMenus.create({"title": "Open KML/KMZ", "contexts": ["link"], "id": "kmlopen", "targetUrlPatterns": ["*.kml", "*.kmz"]}); manually under _generated_background_page.html I get the kmlopen, the id of the menu item. What am I doing wrong? The openWin(/*some url*/); function works fine.

解决方案

Wrong pattern.

The patterns follow the standard Match pattern format.

So you should use the patterns

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

However, be mindful of query strings.

更多推荐

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

本文发布于:2023-08-06 13:11:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1312616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   菜单项   程序   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!